Identify Smith Numbers
Link:
https://www.hackerrank.com/challenges/identify-smith-numbers
def sum_digits(n):
return sum(int(x) for x in str(n)) def prime_factors(n):
factors = []
for i in xrange(2, n):
if i*i > n:
break
elif n % i == 0: # 短除法核心
while n % i == 0:
factors.append(i)
n /= i
if n > 1:
factors.append(n)
return factors n = int(raw_input()) factors = prime_factors(n)
print '' if len(factors) > 1 and sum_digits(n) == sum(sum_digits(x) for x in factors) else ''
本题
“数论” -- “质因子分解”
学习到
如何理解(读)代码
哪里是代码的核心,哪里是代码的边缘可变的、灵活的
比如 n % i == 0 这里就是“短除法”的判断核心
而if i * i > n, 这种就是减少判断次数的外围
if和while层叠的顺序也是灵活可变的
算法整体
get了《算法导论》
Identify Smith Numbers的更多相关文章
- POJ 1142 Smith Numbers(史密斯数)
Description 题目描述 While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Leh ...
- Smith Numbers - PC110706
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10042.html 原创:Smit ...
- poj 1142 Smith Numbers
Description While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh U ...
- Smith Numbers POJ - 1142 (暴力+分治)
题意:给定一个N,求一个大于N的最小的Smith Numbers,Smith Numbers是一个合数,且分解质因数之后上质因子每一位上的数字之和 等于 其本身每一位数字之和(别的博客偷的题意) 思路 ...
- POJ 1142:Smith Numbers(分解质因数)
Smith Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submiss ...
- poj1142 Smith Numbers
Poj1142 Smith Numbers Smith Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13854 ...
- UVA 10042 Smith Numbers(数论)
Smith Numbers Background While skimming his phone directory in 1982, Albert Wilansky, a mathematicia ...
- A - Smith Numbers POJ
While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,no ...
- Smith Numbers(分解质因数)
Smith Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14173 Accepted: 4838 De ...
随机推荐
- an error occured during the file system check
打开虚拟机的时候,报错: 出错原因: 我之前修改了/etc/fstab文件, 原先/etc/fstab文件中有一行是这样的: LABEL=/i01 /u01 ...
- DOCKER功能练习
都是书上的示例,慢慢进入..
- 原型模式 - OK
原型模式(Prototype),用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 简单说来原型模式就是从一个对象再创建另外一个可定制的对象,而且不需知道任何创建的细节. 原型模式UML ...
- JVM JMM
- Activity生命周期,状态保存恢复(经典)
一.整体框架 二.main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- BZOJ3016: [Usaco2012 Nov]Clumsy Cows
3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 71 Solved: 52[Submi ...
- Android数据库信息显示在listview上
Key Points: 1.使用SimpleCursorAdapter将Android数据库信息显示在listview上 adapter = new SimpleCursorAdapter(this, ...
- testng跑失败用例重试
testng 提高用例通过率,失败用例要重新运行一次 步骤: 1.新建一个Retry 类,implements IRetryAnalyzer接口,这个类里面确定重跑次数,以及分析每次失败是否需要重新运 ...
- mysql 获取当前时间戳
mysql 获取当前时间为select now() 运行结果: 2012-09-05 17:24:15 mysql 获取当前时间戳为select unix_timestamp(now()) 运行结 ...
- (转)iOS7界面设计规范(1) - UI基础 - 为iOS7而设计
今天开个新坑.其实老早就想做这事儿了.记得前一两年,苹果官方还会在开发者中心提供中文的HIG(Human Interface Guideline),后来给没了:网上能够找到的中文版本不知是官方还是同行 ...