Python 秒杀“Ruby 太慢了”问题,pythonruby,感觉程序员是不是应该多考
Python 秒杀“Ruby 太慢了”问题,pythonruby,感觉程序员是不是应该多考
感觉程序员是不是应该多考虑算法,少拼 CPU ?
按照这个思想,尝试用一点自己能想到的算法来解决这个问题。
时间仓促,定有不足,求拍砖!
硬件:Intel(R) Pentium(R) CPU G620 @ 2.60GHz
计算范围:1 ~ 0xffffffffffffffff
时间:0.436818122864 秒
import math, timeX = 1Y = 0xffffffffffffffff # 64 bitspalindromes = []def isPalindrome(n): s = str(n) return s == s[::-1]def genPalindrome(n): s_ = str(n) # 123 _s = s_[::-1] # 321 # 123 -> 12321 p = int(s_ + _s[1:]) # 12321 pp = p * p # 12321 * 12321 if pp > Y: return False if pp >= X and isPalindrome(pp): palindromes.append(p) # 123 -> 123321 p = int(s_ + _s) # 123321 pp = p * p # 123321 * 123321 if pp >= X and pp <= Y and isPalindrome(pp): palindromes.append(p) return Truedef getStart(n): s = str(int(math.sqrt(n))) l = len(s) return int(s[:l/2+l%2]) # 1 -> 1, 12 -> 1, 123 -> 12, 1234 -> 12st = time.time()i = getStart(X)while genPalindrome(i): i += 1palindromes.sort()for p in palindromes: print '%d(%d)' % (p * p, p)print time.time() - st#该片段来自于http://byrx.net
相关内容
- 一个论坛自动灌水机的例子,论坛灌水机例子,python初学
- python通过wxPython打开一个音频文件并播放的代码,pyth
- Fair and Square,fairandsquare,时间:1e14(18s左
- python 多线程实例,python多线程,# _*_ coding
- hyper超运算,让计算机死了吧,hyper死了,def hyper(n,
- 使用Python写一个贪吃蛇AI,python贪吃蛇ai,不久前在网上
- 使用reportlab生成中文表格pdf,reportlabpdf,导师的一个需求
- 将n*n的矩阵顺时针旋转90°,矩阵顺时针90,def rotate(l
- python核心编程第六章题目:python代码实现:设计一个
- 经典分石头问题,经典分石头,有n个整数,将这堆数分
评论关闭