首页 > 解决方案 > 我正在尝试编写与拉格朗日四平方定理相关的东西,我该如何优化我的代码,因为它不能按时通过所有测试

问题描述

任务是:给定一个正整数 n,在 a² + b² + c² + d² = n 和 b + 3c + 5d = 的非负整数上找到有序元组 (a, b, c, d, e) 的数量e²。

n = int(input())
    count = 0
    for e in range(3 * math.floor(math.sqrt(math.sqrt(n))) + 2):
        for c in range(math.floor(math.sqrt(n)) + 1):
            for b in range(math.floor(math.sqrt(n)) + 1):
                if (e**2-b-3*c)/5 == int((e**2-b-3*c)/5) and (e**2-b-3*c) >= 0:
                    for a in range(math.floor(math.sqrt(n)) + 1):
                        if a**2 +(26*b**2+6*b*c-2*e**2*b+34*c**2-6*e**2*c+e**4)/25 == n:
                            count += 1
    print(count)

标签: python-3.xperformancemathoptimization

解决方案


推荐阅读