如何提高Euler#39项目解决方案的效率?

2024-06-01 09:42:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用了暴力的方法,并试图提高效率,尽我所能,但我只能得到一个周长680后1小时。我需要到1000。你知道吗

这就是我想解决的问题:

If p is the perimeter of a right angle triangle with integral length > sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p ≤ 1000, is the number of solutions maximised?

https://projecteuler.net/problem=39

在我的解决方案中,我为每一行添加了注释来解释它的作用。你知道吗

我用的是python3。你知道吗

import math

# Initialisation of variables:
max = 0
ans = 0

# 'i' represents the perimeter. 'c' represents the hypotenuse length. 'a' and 'b' are both sides lengths:

# This outer loop iterates through all the perimeters from 1 to 1000.
for i in range(1, 1001):
    # Resets the count and arrA values.
    count = 0
    arrA = []
    # This loop iterates through all the hypotenuse values and makes sure its less than half of the perimeter:
    for c in range(1, i // 2):
        # This loop iterates through all the b values where b is lesser than (perimeter - hypotenuse):
        for b in range(1, i - c):
            # This loop iterates through all the a values where 'a' is lesser than (perimeter - hypotenuse - other side + 1):
            for a in range(1, i - c - b + 1):
                # Makes sure that all sides add up to the perimeter and that hypotenuse is lesser than the sum of the 2 other sides.
                if a + b + c != i or a + b <= c:
                    continue
                # Makes sure no instances are being repeated by and b being switched:
                if b in arrA:
                    break
                # Checks if its a right angled triangle:
                if math.sqrt(math.pow(a, 2) + math.pow(b, 2)) == c:
                    #Adds to the number of matches for the current perimeter.
                    count += 1
                    # Adds a to arrA so b can check later:
                    arrA.append(a)
                    # Extra output line for testing purposes (not needed):
                    print([f'{a:4}', f'{b:4}', f'{c:4}', f'{i:4}', f'{count:4}'])
    # checks if the current count was a new maximum or equal to previous maximum:
    if count >= max:
        max = count
        ans = i

#Prints final output:
print(f'Final Answer: {ans}')

以下代码与上述代码完全相同,但没有注释以便于阅读:

import math

max = 0
ans = 0

for i in range(1, 1001):
    count = 0
    arrA = []
    for c in range(1, i // 2):
        for b in range(1, i - c):
            for a in range(1, i - c - b + 1):
                if a + b + c != i or a + b <= c:
                    continue
                if b in arrA:
                    break
                if math.sqrt(math.pow(a, 2) + math.pow(b, 2)) == c:
                    count += 1
                    arrA.append(a)
                    print([f'{a:4}', f'{b:4}', f'{c:4}', f'{i:4}', f'{count:4}'])

    if count >= max:
        max = count
        ans = i

print(f'Final Answer: {ans}')

它甚至在2个小时内都没有完成,但它的周长达到了800,所以我知道它正在工作,只是速度非常慢。你知道吗

任何帮助都将不胜感激。你知道吗


Tags: andoftheinforifiscount