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 ...
随机推荐
- Repeater绑定数据库
前台: <table width="> <tr> <td class="tr1"> <asp:Label Text=" ...
- UCOS 内存管理理解 创建任务
OS_MEM *OSMemCreate (void *addr, INT32U nblks, INT32U blksize, INT8U *err) { ................... ...
- ISO7816标准IO通讯方面的需求
以下需求适用于符合ISO7816的Reader的测试:换句话说只要Reader能通过以下指令,就基本符合了ISO7816标准,具体需求为: 1 概述 本文档主要描述CDCAS系统中用到的CA证书的格式 ...
- linux下的shell和脚本
1.各种Unix shell linux下的shell基本是从unix环境中的shell发展而来,贴一下wiki:其中我们常用的,可归类为Bourne Shell(/usr/bin/sh或/bin/s ...
- eclipse,tomcat部署web项目,以及本地文件访问
1.直接把项目复制到Tomcat安装目录的webapps目录中,这是最简单的一种Tomcat项目部署的方法,也是初学者最常用的方法. 2.在tomcat安装目录中有一个conf文件夹,打开此文件夹,其 ...
- IOS深入学习(12)之Archiving
1 前言 本文介绍的是一个归档解档方法,也是编码和解码时候所做的事情,和如何进行,编码和归档其实就是将对象关系转化为字节流并且归档为特殊的文件,解码和解档是逆过程. 英文原文:http://blog. ...
- Struts2小结
Struts 2是在WebWork2基础发展而来的. 注意:struts 2和struts 1在代码风格上几乎不一样. Struts 2 相比Struts 1的优点: 1.在软件设计上Struts 2 ...
- pyqt cvs保存
# -*- coding: utf-8 -*-__author__ = 'Administrator'import sys, csvfrom PyQt4 import QtGui, QtCore cl ...
- maven java.lang.OutOfMemoryError:PermGEn space
配置环境变量: JAVA_OPTS-Xms512m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m MAVEN_OPTS-Xms256m -Xmx ...
- ibatis使用--SqlMapClient对象
SqlMapClient对象 这个对象是iBatis操作数据库的接口(执行CRUD等操作),它也可以执行事务管理等操作.这个类是我们使用iBATIS的最主要的类.它是线程安全的.通常,将它定义为单例. ...