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)


所有得因子之和等于该数字的数被成为完美数。给定要给数字,判断其是否为完美数

乍看题目比较简单,就直接写呗

     public static boolean checkPerfectNumber(int num) {
if(num == 1) return false;
int sum = 0;
for(int i = 1; i < num; i++)
if(num % i == 0)
sum += i;
return sum == num;
}

可是提交了一下,发现超时,这时候就需要做一定的优化了。就像以前求素数的方法,对输入的数求平方根

    public boolean checkPerfectNumber(int num) {
if(num == 1) return false;
int sum = 1;
for(int i = 2; i < Math.sqrt(num); i++)
if(num % i == 0)
{
sum += i;
sum += num/i;
} return sum == num;
}

507. Perfect Number的更多相关文章

  1. 【leetcode】507. Perfect Number

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

  2. 507. Perfect Number 因数求和

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

  3. [LeetCode] 507. Perfect Number 完美数字

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

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

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

  5. 507 Perfect Number 完美数

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

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

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

  7. [LeetCode] Perfect Number 完美数字

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

  8. [Swift]LeetCode507. 完美数 | Perfect Number

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

  9. LeetCode Perfect Number

    原题链接在这里:https://leetcode.com/problems/perfect-number/#/description 题目: We define the Perfect Number ...

随机推荐

  1. 编译虚拟机jvm——openjdk的编译

    java只所以被推广,实际上很大原因是因为本身是跨平台的,很大作用是因为虚拟机的关系. 一般情况下开发人员不需要关注虚拟机内部实现就可以日常开发了,但是有时候涉及到性能的时候就需要了解虚拟机的实现机制 ...

  2. "逃离北京"的这些年 2

    一  找工作第二阶段 我为了保险,在辞职信还特别写了:特此提前一个月提出辞职. 果然是搞金融的,C公司在我提交辞职信后,一周内就让我整理好工作资料,办好辞职手续. 没关系,都是要走的人.早点离开也是好 ...

  3. 安装 Qt 及所需 gcc 等

    嫌麻烦,下载离线安装包一次性装好 Qt 及 Qt Creator (Community) Qt 安装包下载地址 http://download.qt.io/official_releases/qt/ ...

  4. 异常处理-try catch

    一:try catch是什么 try catch是java程序设计中处理异常的重要组成部分 异常是程序中的一些错误,有些异常需要做处理,有些则不需要捕获处理,异常是针对方法来说的,抛出.声明抛出.捕获 ...

  5. codeblocks+mbedtls库配置

    网上都没有找到window下mbedtls的相关配置,或许是太简单了.希望可以帮助那些像我这样的小白一枚. 下载 github的下载:https://github.com/ARMmbed/mbedtl ...

  6. Python中的选择排序

    选择排序 选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大 ...

  7. VMware12提示 已将该虚拟机配置为使用 64 位客户机操作系统。但是,无法执行 64 位操作。

    VMware12提示 已将该虚拟机配置为使用 64 位客户机操作系统.但是,无法执行 64 位操作. 此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态 解决办法: 下载LeoM ...

  8. Yacc 与 Lex 快速入门(词法分析和语法分析)

    我们知道,高级语言,一般的如c,Java等是不能直接运行的,它们需要经过编译成机器认识的语言.即编译器的工作. 编译器工作流程:词法分析.语法分析.语义分析.IR(中间代码,intermediate ...

  9. include、include_once、require、require_once其区别

    1.include: 载入文件.未找到文件,则产生E_WARNING 级别的警告错误,脚本继续运行. 2.include_once: 与include 语句作用相同,区别只是如果该文件已经被包含过,则 ...

  10. Python初识--基础

    碎碎念 最初想写一些机器学习算法的实现类的文章,但发现自己工作经验不足,即使写出来,也只是一些应用场景十分狭隘的小实验: 了解到身边有些朋友和同学也想了解学习一下脚本语言Python(毕竟是后起之秀) ...