Python Day 3: Finding Fun Numbers
数学界有个关于 1729的故事, the Ramanujan Number.
一天 Hardy 去医院看 Ramanujan, 他说来的路上搭了一辆牌照很没有意思的士, 1729. Ramanujan 说: 不,这个数非常有意思; it is the smallest number expressible as the sum of two cubes in two different ways. (这是一个可以用两个立方和表达的最小数)
1729 = 13 + 123 = 93 + 103
我们今天用 python 来找一下有趣的数, the sum of divisors' square number(因子平方和完全平方数), and perfect number (完美数).
[caption id="attachment_1416" align="alignnone" width="1024"] imageGraphicMama-team / Pixabay[/caption]
Perfect Number: a perfect number is a positive integer that is equal to the sum of its proper positive divisors (除自己以外的因子和相加等于自己)
Example: the proper divisor of 6 is 1,2,3 and 6 = 1+2+3.
Python code:
#we want to find all the perfect number within 1000.
sieve= [1]*(1000+1)
n = 2
while n <= 1000:
# check n
if sieve[n] == n:
print(n, "is a perfect number")
# add n to all k * n where k > 1
kn = 2 * n
while kn <= 1000:
sieve[kn] += n
kn += n
n += 1
Result:
6 is a perfect number
28 is a perfect number
496 is a perfect number
We can see 6,28 and 496 are perfect numbers.
[caption id="attachment_1415" align="alignnone" width="1024"] imageInspiredImages / Pixabay[/caption]
The sum of divisors' square number
Suppose we want to find some number such that the Sum of the squares of the number's divisors is a perfect square number
Example:
The divisors of 42 are 1,2,3,6,7,14,21,42
the square of divisors are 1,4,9,36,49, 196, 441, 1764
Sum of the square of divisors are 1+4+9+ 36+49+196+441+1764=2500
2500=500 * 500 is a perfect square
Python Code:
def list_squared(m,n):
list=[]
for i in range(m,n+1):
sum=0
s_list=[]
for j in range(1,int(i**0.5)+1):
if i%j==0:
div=i/j
sum+=j**2
if j!=div:
sum+=div**2
sqt=sum**0.5
if int(sqt)==sqt:
s_list=[i,sum]
list.append(s_list)
return list
list_squared(42,250)
Output:
[[42, 2500.0], [246, 84100.0]]
We can see 42, and 246 satisfy the conditions in the range (42,250)
我喜欢数论,因为它赋予了每一个数字特殊的意义 :) , 最近两年,朋友和我都会对已经过去的和未来要来得年纪有一些敏感和伤感. 但我看这些数的时候,每一个都那么特别,都那么美:
27, the Cube age 立方年,
28, the Perfect age 完美年,
29, a Gaussian age 高斯年,
30, a smallest 2,3,5, divisor age 因子年,
31, a PRIME age 最好年 ,
32, a Binary age 二进制年!!!
I like my number theory classmate JOHNES said:
Appreciated every little things in life, like Tuesdays, it only comes once a week!
要感恩小事情哦, 像星期二,一周才一次 :)
Happy Studying! 🐼
网友评论