[LeetCode] 507. Perfect Number 完美数字
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.
Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not.
Example:
Input: 28
Output: True
Explanation: 28 = 1 + 2 + 4 + 7 + 14
Note: The input number n will not exceed 100,000,000. (1e8)
定义Perfect Number是一个正整数,它等于除了它自己之外的所有正除数的总和。现在,给定一个整数n,编写一个函数,当它是一个完美数字时返回true,否则返回false。
解法:直接解就可以。注意处理num是1的时候。还有技巧是,只计算num/2就可以,如果能整除就把除数和结果都累加。
Java:
public class Solution {
public boolean checkPerfectNumber(int num) {
if (num == 1) return false; int sum = 0;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
sum += i + num / i;
}
}
sum++; return sum == num;
}
}
Python:
class Solution(object):
def checkPerfectNumber(self, num):
"""
:type num: int
:rtype: bool
"""
if num <= 0: return False
ans, SQRT = 0, int(num ** 0.5)
ans = sum(i + num//i for i in range(1, SQRT+1) if not num % i)
if num == SQRT ** 2: ans -= SQRT
return ans - num == num
Python:
class Solution(object):
def checkPerfectNumber(self, num):
"""
:type num: int
:rtype: bool
"""
if num == 1:
return False
i = 2
s = 1
while i * i < num:
if num % i == 0:
s += i
s += num / i
i += 1 return True if s == num else False
C++:
class Solution {
public:
bool checkPerfectNumber(int num) {
if (num == 1) return false;
int sum = 1;
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) sum += (i + num / i);
if (i * i == num) sum -= i;
if (sum > num) return false;
}
return sum == num;
}
};
C++:
class Solution {
public:
bool checkPerfectNumber(int num) {
return num==6 || num==28 || num==496 || num==8128 || num==33550336;
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 507. Perfect Number 完美数字的更多相关文章
- [LeetCode] Perfect Number 完美数字
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- 507 Perfect Number 完美数
对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”.给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False示例:输入: 28输出: True解释: ...
- 【leetcode】507. Perfect Number
problem 507. Perfect Number solution: /* class Solution { public: bool checkPerfectNumber(int num) { ...
- 【LeetCode】507. Perfect Number 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 507. Perfect Number
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- 507. Perfect Number 因数求和
[抄题]: We define the Perfect Number is a positive integer that is equal to the sum of all its positiv ...
- [LeetCode] 65. Valid Number 验证数字
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- LeetCode 268. Missing Number缺失数字 (C++/Java)
题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...
- LeetCode算法题-Perfect Number(Java实现)
这是悦乐书的第249次更新,第262篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第116题(顺位题号是507).我们定义Perfect Number是一个正整数,它等于 ...
随机推荐
- 项目Alpha冲刺总结随笔
班级:软件工程1916|W 作业:项目Alpha冲刺 团队名称:SkyReach 目标:完成项目Alpha版本 项目Github地址 团队博客汇总 队员学号 队员姓名 个人博客地址 备注 221600 ...
- Ubuntu 14.04.2升级openssh7.9
环境:Ubuntu 14.04.2 目的:openssh版本6.6升级为openssh7.9 准备以下3个包 http://www.zlib.net/ https://www.openssl.org/ ...
- java中异步调用注意
Future接口是Java标准API的一部分,在java.util.concurrent包中.Future接口是Java线程Future模式的实现,可以来进行异步计算. 有了Future就可以进行三段 ...
- go 学习 (五):goroutine 协程
一.goroutine 基础 定义 使用者分配足够多的任务,系统能自动帮助使用者把任务分配到 CPU 上,让这些任务尽量并发运作,此机制在Go中称作 goroutine goroutine 是 Go语 ...
- jaeger 使用scylladb作为后端存储
scylladb 是一个不错的apache Cassandra 替代,而且兼容很不错,今天在尝试过yugabyte 之后放弃了,因为在进行jaeger 创建 Cassandra schema 的时候碰 ...
- facl
file access control lists 文件的额外赋权机制,针对性的对某用户对文件的权限进行处理 setfacl 指定空权限
- A simple dispiction of dijkstra
前言 \(SPFA\)算法由于它上限 \(O(NM) = O(VE)\)的时间复杂度,被卡掉的几率很大.在算法竞赛中,我们需要一个更稳定的算法:\(dijkstra\). 什么是\(dijkstra\ ...
- 什么是uni-app?
uni-app 是一个使用 Vue.js 开发所有前端应用的框架,开发者编写一套代码,可发布到iOS.Android.H5.以及各种小程序(微信/支付宝/百度/头条/QQ/钉钉)等多个平台. 即使不跨 ...
- Spyder汉化问题
首先感谢李增海大神,以下内容来源于http://www.lizenghai.com 必备条件 1.已安装Spyder 2.Spyder版本在3.X以上 Spyder安装: 1.anaconda下,co ...
- GoCN每日新闻(2019-10-01)
GoCN每日新闻(2019-10-01) GoCN每日新闻(2019-10-01) 1. 我依然爱着 GOPATH https://divan.dev/posts/gopath/ 2. Go 代码注释 ...