이경수 선생님의 수학실험실

Problem 39(Integer right triangles) 본문

Project Euler

Problem 39(Integer right triangles)

(이경수) 2019. 6. 16. 13:16

Problem 39(Integer right triangles)

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?

 

In Python:

import time

def ispythatriple(p, i, j):
    if i ** 2 + j ** 2 == (p - (i + j)) ** 2:
        return True
    else:
        return False

startTime = time.time()
maxCount = 0
maxP = 0
for p in range(3, 1001):
    count = 0
    for i in range(1, p // 3 + 1):
        for j in range(i, (p - i) // 2 + 1):
            if ispythatriple(p, i, j) is True:
                count += 1
    if count > maxCount:
        maxCount = count
        maxP = p
print(maxP)
print(time.time() - startTime, "seconds")

Run time: 36.53164219856262 seconds

 

In Java:

package project_euler31_40;

public class Euler39 {
	public static Boolean ispythatriple(int p, int i, int j) {
		if (Math.pow(i, 2) + Math.pow(j, 2) == Math.pow(p - (i + j), 2)){
			return true;
		}
		return false;
	}
	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		int count = 0;
		int maxCount = 0;
		int maxP = 0;
		for (int p = 3; p < 1001; p++) {
			count = 0;
			for (int i = 1; i < Math.floorDiv(p, 3) + 1; i++) {
				for (int j = i; j < Math.floorDiv(p - i, 2) + 1; j++) {
					if (ispythatriple(p, i, j) == true) {
						count ++;
					}
				}
			}
			if (count > maxCount) {
				maxCount = count;
				maxP = p;
			}
		}
		System.out.println(maxP);
		long endTime = System.currentTimeMillis();
		System.out.println((double)(endTime - startTime)/(double)1000 + "seconds");
	}
}

Run time: 0.14seconds

 

Solution: 840

'Project Euler' 카테고리의 다른 글

Problem 41(Pandigital prime)  (0) 2019.06.16
Problem 40(Champernowne's constant)  (0) 2019.06.16
Problem 38(Pandigital multiples)  (0) 2019.06.15
Problem 37(Truncatable primes)  (0) 2019.06.06
Problem 36(Double-base palindromes)  (0) 2019.06.06
Comments