Project Euler Problem #75

Problem #75 concerns the perimeters of right triangles with integer side lengths. The problem reads:

Project Euler Problem 75: Singular integer right triangles
It turns out that 12 cm is the smallest length of wire that can be bent to form an integer sided right angle triangle in exactly one way, but there are many more examples.
12 cm: (3,4,5)
24 cm: (6,8,10)
30 cm: (5,12,13)
36 cm: (9,12,15)
40 cm: (8,15,17)
48 cm: (12,16,20)
In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided right angle triangle, and other lengths allow more than one solution to be found; for example, using 120 cm it is possible to form exactly three different integer sided right angle triangles.
120 cm: (30,40,50), (20,48,52), (24,45,51)
Given that L is the length of the wire, for how many values of L ≤ 1,500,000 can exactly one integer sided right angle triangle be formed?

My solution for this problem is quite bad, as it involves lots of brute force. My solution is very similar to my solution for Problem #9, so I may only provide a sketch of a solution here. Here is my solution:

Solution #1: Brute Force Approach

We know that a general Pythagorean triple in the integers can always be written in the form d(m^2-n^2), d(2mn), d(m^2+n^2). Thus, the perimeter of a general Pythagorean right triangle is d(2m^2+2mn) = 2md(m+n). Thus, for a given length L, it suffices to count the number of solutions to 2md(m+n) = L. This can be done by listing the factors of L to do casework on d. From this point, casework can be done by noting that m<m+n. Here is an implementation of this approach in Python 2.7. It is very similar to my solution for Problem #9.

 '''
 Author: Walker Kroubalkian
 General Pythagorean Triple Approach to Project Euler Problem #75
 '''
 
 import time
 from math import sqrt
 
 def listFactors(n):
     primes = []
     exponents = []
     c = 2
     while(c<=sqrt(n)):
         if(n%c==0):
             primes.append(c)
             t = 0
             while(n%c==0):
                 n/=c
                 t+=1
             exponents.append(t)
         c+=1
     if(n>1):
         primes.append(n)
         exponents.append(1)
     return sorted(listFactorsGivenFactorization(primes, exponents))
 
 def listFactorsGivenFactorization(p,e):
     total = []
     if(len(p) >= 1):
         for x in range(e[0]+1):
             total.append(p[0]**x)
     else:
         return [1]
     previousTotal = listFactorsGivenFactorization(p[1:], e[1:])
     
     actualTotal = []
     for a in total:
         for b in previousTotal:
             actualTotal.append(a*b)
     
     return actualTotal
 
 def countPythagoreanTriples(n):
     if(n%2==1):
         return -1
     found = []
     possibleD = listFactors(n/2)
     n/=2
     for d in possibleD:
         newProduct = n/d
         lowerBound = int(sqrt(newProduct/2))
         upperBound = int(sqrt(newProduct))
         for m in range(lowerBound, upperBound+1):
             if(m>0 and newProduct%m == 0):
                 o = newProduct/m-m
                 if(0<o<m):
                     triple = sorted([d*(m*m-o*o), 2*d*m*o, d*(m*m+o*o)])
                     if triple not in found:
                         found.append(triple)
                         if(len(found)>1):
                             return 2
     
     return len(found)
 
 def projectEulerProblemSeventyFive(n):
     total = 1
     for c in range(13,n+1):
         v = countPythagoreanTriples(c)
         if(v==1):
             total+=1
     return total
 
 start = time.time()
 print projectEulerProblemSeventyFive(1500000)
 print ("--- %s seconds ---" % (time.time()-start))
 
 '''
 Prints
 
 161667
 --- 37.5037260056 seconds ---
 
 for input of n = 1500000
 ''' 

That may look like a lot of code, but most of it is copy pasted from Problem #9. That code took over 37 seconds to run, so I will definitely need to come back to this problem at some point to look for a more efficient solution.

Thanks for reading! See you tomorrow.

Published by Walker Kroubalkian

My name is Walker Kroubalkian. I really enjoy math, computer science, and hiking.

Leave a comment

Design a site like this with WordPress.com
Get started