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 完美数字的更多相关文章

  1. [LeetCode] Perfect Number 完美数字

    We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...

  2. 507 Perfect Number 完美数

    对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”.给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False示例:输入: 28输出: True解释: ...

  3. 【leetcode】507. Perfect Number

    problem 507. Perfect Number solution: /* class Solution { public: bool checkPerfectNumber(int num) { ...

  4. 【LeetCode】507. Perfect Number 解题报告(Python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 507. Perfect Number

    We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...

  6. 507. Perfect Number 因数求和

    [抄题]: We define the Perfect Number is a positive integer that is equal to the sum of all its positiv ...

  7. [LeetCode] 65. Valid Number 验证数字

    Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...

  8. 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 ...

  9. LeetCode算法题-Perfect Number(Java实现)

    这是悦乐书的第249次更新,第262篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第116题(顺位题号是507).我们定义Perfect Number是一个正整数,它等于 ...

随机推荐

  1. P4560 [IOI2014]Wall 砖墙

    题目描述 给定一个长度为 nn且初始值全为 00的序列.你需要支持以下两种操作: Add L, R, hL,R,h:将序列 [L, R][L,R]内所有值小于 hh的元素都赋为 hh,此时不改变高度大 ...

  2. 决策树——ID3

    参考网址:https://www.cnblogs.com/further-further-further/p/9429257.html ID3算法 最优决策树生成 -- coding: utf-8 - ...

  3. 持续集成学习8 jenkins权限控制

    一.总体配置 1.系统管理---> Configure Global Security 2.配置基于角色授权 创建角色 ----> 分配角色 代表着所有以dev-开头的 job全部都分配给 ...

  4. 洛谷 CF448D Multiplication Table

    目录 题目 思路 \(Code\) 题目 CF448D Multiplication Table 思路 二分答案.这个矩阵的每一排都是递增的,所以二分\(ans\),去计算有多少个数等于\(ans\) ...

  5. 转载:四两拨千斤:借助Spark GraphX将QQ千亿关系链计算提速20倍

    四两拨千斤:借助Spark GraphX将QQ千亿关系链计算提速20倍 时间 2016-07-22 16:57:00 炼数成金 相似文章 (5) 原文  http://www.dataguru.cn/ ...

  6. mapreduce中reduce没有执行

    hadoop执行mapreduce过程reduce不执行原因 1.如果你的map过程中没有context.write()是不执行reduce过程的:2.如果你的map过程中context.write( ...

  7. SpringBoot聚合项目打包

    1.打包遇到的问题: 子模块打包时,每次打包出来的jar都只有3k左右,没办法运行,目测应该是依赖的jar没有打包进来! 2.解决办法: 2.1.主项目的pom.xml里面不需要打包配置 2.2.在需 ...

  8. eclipse juno 怎么安装maven

    步骤如下: 1.下载maven的bin,在apache官方网站可以下载. 2.下载下来之后,解压,找个路径放进去, 把bin的位置设在环境变量里,新建环境变量MAVEN_HOME. 3.在PATH里加 ...

  9. Python调用win32 API绘制正弦波

    Python调用win32 API新建窗口与直接创建窗口的流程相同 流程:注册窗口→创建窗口→显示窗口→更新窗口→消息循环 代码: # -*- coding: utf-8 -*- import win ...

  10. Linux下WebLogic的启动、停止和后台运行的方法

    Linux下WebLogic的启动.停止和后台运行的方法 进入目录:/home/weblogic/user_projects/domains/base_domain/bin 查看目录下的命令,如图: ...