project euler 108

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值