// you’re reading...

Project Euler Solutions

Project Euler Problem 75 Solution

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4.00 out of 5)
Loading ... Loading ...

Problem Description

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 ≤ 2,000,000 can exactly one integer sided right angle triangle be formed?

Analysis

This is an extension to the Problem 9 solution.

Solution

Runs < 3 seconds in Python.

from Euler import gcd
 
limit = 2000000
sqrt_limit = int(limit**.5)
tx = [0]*limit
for i in xrange(1, sqrt_limit, 2):
  for j in xrange(2, sqrt_limit, 2):
    if gcd(i, j) == 1: 
      sum = abs(j*j - i*i) + 2*i*j + i*i + j*j
      for s in range(sum, limit, sum):
        tx[s]+=1
 
print "Answer to PE75 = ", tx.count(1)

Comments

Discussion

One comment for “Project Euler Problem 75 Solution”

  1. Either the limit has changed on the euler page or you have used a wrong number.
    Nevertheless, the limit has to be 1500000(1,5 * 10^6)

    Posted by Klemens Nanni | January 28, 2010, 11:16 am

Post a comment