问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3879 访问。

对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”。

给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False

输入: 28

输出: True

解释: 28 = 1 + 2 + 4 + 7 + 14

注意:输入的数字 n 不会超过 100,000,000. (1e8)


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.

Input: 28

Output: True

Explanation: 28 = 1 + 2 + 4 + 7 + 14

Note: The input number n will not exceed 100,000,000. (1e8)


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3879 访问。

public class Program {

    public static void Main(string[] args) {
var num = 6;
var res = CheckPerfectNumber(num);
Console.WriteLine(res); num = 496;
res = CheckPerfectNumber2(num);
Console.WriteLine(res); Console.ReadKey();
} private static bool CheckPerfectNumber(int num) {
//28 = 1 + 2 + 4 + 7 + 14
//以根号 28 = 5.29 为分水岭
//若 2 能被 28 整除,28 / 2 = 14
//则 2 和 14 都需要计入计算和的范围之内
//边界处理
if(num == 1) return false;
//1 总是因子,提前 + 1
var sum = 1;
//从 2 到 Math.Sqrt(num)
for(var i = 2; i <= Math.Sqrt(num); i++) {
//若整除,则被除数和商均计入累计和
if(num % i == 0) {
sum += i + num / i;
}
}
//满足完美数定义时返回true,否则返回false
return sum == num;
} private static bool CheckPerfectNumber2(int num) {
//题目范围内只包含这 5 个完美数
//套用强哥的一句话,我从未见过如此厚颜无耻之人
return num == 6 || num == 28 ||
num == 496 || num == 8128 ||
num == 33550336;
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3879 访问。

True
True

分析:

显而易见,CheckPerfectNumber 的时间复杂度为:  ,CheckPerfectNumber2 的时间复杂度为:  。

C#LeetCode刷题之#507-完美数(Perfect Number)的更多相关文章

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

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

  2. leetcode刷题笔记-1. 两数之和(java实现)

    题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...

  3. C#LeetCode刷题之#202-快乐数(Happy Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3856 访问. 编写一个算法来判断一个数是不是"快乐数& ...

  4. #leetcode刷题之路15-三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  5. #leetcode刷题之路12-整数转罗马数字

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值I 1V 5X 10L 50C 100D 500M 1000 例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 ...

  6. #leetcode刷题之路29- 两数相除

    给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符.返回被除数 dividend 除以除数 divisor 得到的商. 示例 1:输入: ...

  7. #leetcode刷题之路1-两数之和

    给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符.返回被除数 dividend 除以除数 divisor 得到的商. 示例 1:输入: ...

  8. C#LeetCode刷题之#9-回文数(Palindrome Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3840 访问. 判断一个整数是否是回文数.回文数是指正序(从左向右 ...

  9. leetcode刷题第二天<两数相加>

    题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...

随机推荐

  1. 媳妇儿喜欢玩某音中的动漫特效,那我就用python做一个图片转化软件。

    ​    最近某音上的动漫特效特别火,很多人都玩着动漫肖像,我媳妇儿也不例外.看着她这么喜欢这个特效,我决定做一个图片处理工具,这样媳妇儿的动漫头像就有着落了.编码    为了快速实现我们的目标,我们 ...

  2. [Qt2D绘图]-02坐标系统&&抗锯齿渲染

    本节的内容可以在帮助中通过Coordinate System关键字查看. 或者入门可以看<Qt Creator 快速入门>这本书.强烈推荐入门使用.下面的内容为本书的阅读笔记,喜欢的可以买 ...

  3. Python Ethical Hacking - NETWORK_SCANNER(1)

    NETWORK_SCANNER Discover all devices on the network. Display their IP address. Display their MAC add ...

  4. vue : 使用stylus less (包括sublime插件支持)

    版本: vue 2.5.2 webpack 3.6.0 先说stylus. 用npm装个包. npm install stylus stylus-loader --save-dev 然后在.vue文件 ...

  5. 3.TCP协议

    一.TCP协议特点和报文段格式 面向连接的传输层协议 每一条TCP连接只能有两个端点 TCP提供可靠交付的服务,无差错,不丢失,不重复,按序到达 全双工通信 -> 发送缓冲:准备发送的数据&am ...

  6. .Net Core 常见错误解决记录

    Error: String or binary data would be truncated. The statement has been terminated 数据库出错原因: 表字段创建的太短 ...

  7. python的常用模块

    一.random随机数模块 使用随机数模块需要导入随机数模块import random 1.random.random() 生成[0,1)之间的随机小数 2.random.randint(a,b) 生 ...

  8. sql数据管理语句

    一.数据管理 1.增加数据 INSERT INTO student VALUES(1,'张三','男',20); -- 插入所有字段.一定依次按顺序插入 -- 注意不能少或多字段值 如只需要插入部分字 ...

  9. map,reduce,filter基础实现

    #coding=gbk from operator import add # 导入加法 # map 函数名 , 序列对象 print(list(map(str,range(5)))) print(li ...

  10. 线程_ThreadLocal

    import threading # 创建ThreadLocal对象 house = threading.local() def process_paper(): user = house.user ...