Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

039 会计算重复 #4

Open
zgxkbtl opened this issue Dec 17, 2021 · 4 comments
Open

039 会计算重复 #4

zgxkbtl opened this issue Dec 17, 2021 · 4 comments

Comments

@zgxkbtl
Copy link

zgxkbtl commented Dec 17, 2021

https://pe.metaquant.org/pe039.html

对于同一个p,计算a和b可能重复,加个大小判断就可以

def number_of_solution(p):
    num = 0
    for a in range(1,p//3):
        if (p**2-2*p*a)%(2*p-2*a) == 0:
            if a > (p**2-2*p*a)//(2*p-2*a): break  # bug fixed here
            num += 1
    return num

def main(n=1000):
    d = {p:number_of_solution(p) for p in range(2,n+1,2)}
    ans = max(d,key=d.get)
    return ans
@sorrowise
Copy link
Owner

感谢回复,但我在题解中已经假设,a <= b < c, 所以才得到a的取值为[1,p//3),所以a并不会大于b。

@zgxkbtl
Copy link
Author

zgxkbtl commented Dec 18, 2021

当 p=2520 的时候,p/3=840,number_of_solution在代码运行过程中会把{720 756 1044}, {756 720 1044} 当成两组不同的解。

a , b 钧小于 p//3 是有可能出现的。

@wang1zhen
Copy link

是的,将上限设置为p/(2+Sqrt[2])是更好的行为

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@sorrowise @zgxkbtl @wang1zhen and others