也玩有道难题的双立方数问题:Python 版解法

发现大家都在玩这个,用 python 写了一个,计算一百万之内的数字还是飞快的(包括结果打印)。

#coding: utf-8

"""
第二道算法题(500分)

题目要求:双倍超立方数是指一个正整数可以正好被拆分为两种不同的a^3+b^3的方式,
其中a,b均为整数且0<a<=b。对于任何一个指定的 int n, 返回所有的小于等于n的双倍超立方数的个数。
"""

def find_double_cublic_number(max):
    cubs 
= [pow(i,3for i in range(1, int(round(pow(max, 1./3))))]
    
#print cubs
    sum_dict = {}
    result_list 
= []
    length 
= len(cubs)
    
for i in range(length):
        
for j in range(i + 1, length):
            sum 
= cubs[i] + cubs[j]
            
if sum_dict.has_key(sum) and sum <= max:
                result_list.append(sum)
                
print "%d^3 + %d^3 = %d" % (sum_dict[sum][0], sum_dict[sum][1], sum)
                
print "%d^3 + %d^3 = %d" % (int(round(pow(cubs[i], 1./3))), int(round(pow(cubs[j], 1./3))), sum)
            
else:
                sum_dict[sum] 
= (int(round(pow(cubs[i], 1./3))), int(round(pow(cubs[j], 1./3))))
    
    
return len(result_list)

if __name__ == '__main__':
    n 
= find_double_cublic_number(1000000)
    
print "--------------------------------\n"
    
print n, "results found."

    

 

输出结果:

 

>pythonw -"double_cubic_number.py"
1^3 + 12^3 = 1729
9^3 + 10^3 = 1729
2^3 + 16^3 = 4104
9^3 + 15^3 = 4104
2^3 + 34^3 = 39312
15^3 + 33^3 = 39312
9^3 + 34^3 = 40033
16^3 + 33^3 = 40033
2^3 + 24^3 = 13832
18^3 + 20^3 = 13832
4^3 + 32^3 = 32832
18^3 + 30^3 = 32832
10^3 + 27^3 = 20683
19^3 + 24^3 = 20683
9^3 + 58^3 = 195841
22^3 + 57^3 = 195841
3^3 + 60^3 = 216027
22^3 + 59^3 = 216027
17^3 + 55^3 = 171288
24^3 + 54^3 = 171288
17^3 + 39^3 = 64232
26^3 + 36^3 = 64232
3^3 + 36^3 = 46683
27^3 + 30^3 = 46683
6^3 + 48^3 = 110808
27^3 + 45^3 = 110808
8^3 + 53^3 = 149389
29^3 + 50^3 = 149389
4^3 + 68^3 = 314496
30^3 + 66^3 = 314496
11^3 + 93^3 = 805688
30^3 + 92^3 = 805688
12^3 + 40^3 = 65728
31^3 + 33^3 = 65728
18^3 + 68^3 = 320264
32^3 + 66^3 = 320264
20^3 + 97^3 = 920673
33^3 + 96^3 = 920673
4^3 + 48^3 = 110656
36^3 + 40^3 = 110656
8^3 + 64^3 = 262656
36^3 + 60^3 = 262656
12^3 + 51^3 = 134379
38^3 + 43^3 = 134379
20^3 + 54^3 = 165464
38^3 + 48^3 = 165464
17^3 + 76^3 = 443889
38^3 + 73^3 = 443889
2^3 + 89^3 = 704977
41^3 + 86^3 = 704977
5^3 + 60^3 = 216125
45^3 + 50^3 = 216125
10^3 + 80^3 = 513000
45^3 + 75^3 = 513000
5^3 + 76^3 = 439101
48^3 + 69^3 = 439101
30^3 + 67^3 = 327763
51^3 + 58^3 = 327763
34^3 + 78^3 = 513856
52^3 + 72^3 = 513856
6^3 + 72^3 = 373464
54^3 + 60^3 = 373464
15^3 + 80^3 = 515375
54^3 + 71^3 = 515375
12^3 + 96^3 = 886464
54^3 + 90^3 = 886464
42^3 + 69^3 = 402597
56^3 + 61^3 = 402597
30^3 + 81^3 = 558441
57^3 + 72^3 = 558441
35^3 + 98^3 = 984067
59^3 + 92^3 = 984067
29^3 + 99^3 = 994688
60^3 + 92^3 = 994688
24^3 + 80^3 = 525824
62^3 + 66^3 = 525824
7^3 + 84^3 = 593047
63^3 + 70^3 = 593047
23^3 + 94^3 = 842751
63^3 + 84^3 = 842751
24^3 + 98^3 = 955016
63^3 + 89^3 = 955016
51^3 + 82^3 = 684019
64^3 + 75^3 = 684019
8^3 + 96^3 = 885248
72^3 + 80^3 = 885248
--------------------------------

43 results found.
>Exit code: 0

 

posted on 2009-06-04 13:54  NeilChen  阅读(3480)  评论(24编辑  收藏  举报

导航