peoject euler 108
题目
原题
Diophantine reciprocals I
In the following equation x, y, and n are positive integers.
1/x + 1/y = 1/n
For n = 4 there are exactly three distinct solutions:
1/5 + 1/20 = 1/4
1/6 + 1/12 = 1/4
1/8 + 1/8 = 1/4
What is the least value of n for which the number of distinct solutions exceeds one-thousand?
NOTE: This problem is an easier version of Problem 110; it is strongly advised that you solve this one first.
简单翻译
丢番图的倒数
如下所示的等式
1/x + 1/y = 1/n
对于n=4,有三组正整数解:
1/5 + 1/20 = 1/4
1/6 + 1/12 = 1/4
1/8 + 1/8 = 1/7
请计算最小的n值使得解的数量超过1000组
注意:本题是第110题的简化版本,强烈建议先解决本题
思路
本思路的源网站:源地址
通过等式可得:x y都大于n,所以将等式转换如下:
1 / (n + r) + 1/(n + s) = 1 / n
最终转换可得:
n * n = s * r
问题转变为n * n有多少因数的问题
已知一个数的约数个数有公式如下:
pn为N的质因数
d(N)即为N的因数个数,例如
24 = 2 * 2 * 2 * 3
d(N) = (3 + 1) * (1 + 1) = 8
24的因数为:1,2,3,4,6,8,12,24,共8个
那么对于n*n:
python程序
def problem108(limit):
#3*3*3*3*3*3*3 = 2187,已经大于2000了,所以质数列表只取到17
primelist = [2, 3, 5, 7, 11, 13, 17]
#获取输入参数的约数数量
def NoDSquared(number):
nod = 1;
exponent = 0;
remain = number;
for i in range(0, len(primelist)):
if (primelist[i] * primelist[i] > number):
return nod * 3
exponent = 1
while (remain % primelist[i] == 0):
exponent += 2
remain = remain / primelist[i]
nod *= exponent
# If there is no remainder, return the count
if (remain == 1):
return nod
return nod
n = 1
result = 0
while (1):
if ((NoDSquared(n) + 1) / 2 > limit - 1):
result = n
break
n += 1
print result