## Pythagorean triplets in python

This is the place for queries that don't fit in any of the other categories.

### Pythagorean triplets in python

Hi everyone. I have been having some trouble creating a list of the Pythagorean square numbers up to 1,000. The problem I have is that I have repeat square numbers which I thought I had found a way to get around. Please see the code:

Code: Select all
`m=range(1,1001)m_squared=[]for i in m:    if i**2 < 1001:        m_squared.append(i**2)n_squared = list(m_squared)  ##m_squared and n_squared are now square numbers between 1 and 1000for i in m_squared:    for j in n_squared:        if (i + j) in m_squared:            print (i+j)           ##print square number    n_squared.pop(0)               ## avoid crossing the same pair of square numbers twice`

I thought my last line would have gotten rid of the repeat squares - but it doesn't seem to be doing anything - but why?
Person1001

Posts: 7
Joined: Thu Jul 04, 2013 10:14 am

### Re: Pythagorean triplets in python

In fact the last line does work however the number '625' is repeating twice - that's the only number that is repeating twice - how bizarre?
Person1001

Posts: 7
Joined: Thu Jul 04, 2013 10:14 am

### Re: Pythagorean triplets in python

There is a lost of waste in your code
Code: Select all
`m=range(1,1001)m_squared=[]for i in m:    if i**2 < 1001:        m_squared.append(i**2)`

You are doing the same calcualtion twice (i**2) when it is a result you want to keep.
You throw away lots of results in the if condition.

Code: Select all
`m = range(1, 1001)m_squared = []wasted_loops = 0for i in m:    if i ** 2 < 1001:        m_squared.append(i ** 2)    else:        wasted_loops += 1print wasted_loops`
Code: Select all
`969`

969 calulations are done that are not wanted out of 1000

Compare this code
Code: Select all
`squares = []i = 1while True:    result = i * i    if result < 1001:        squares.append(result)        i += 1    else:        break`

It only does the calculation once and only loops the neccessary times until it reaches greater then 1000
To prove that
Code: Select all
`squares = []i = 1loop_count = 0while True:    result = i * i    if result < 1001:        squares.append(result)        i += 1    else:        break    loop_count += 1print loop_count`

Code: Select all
`31`

But does it give the same result ?
Code: Select all
`m = range(1, 1001)m_squared = []for i in m:    if i ** 2 < 1001:        m_squared.append(i ** 2)squares = []i = 1while True:    result = i * i    if result < 1001:        squares.append(result)        i += 1    else:        breakprint m_squared == squares`

Code: Select all
`True`
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1443
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Pythagorean triplets in python

n_squared.pop(0)

use

n_squared.remove(j)

(or maybe i)
___________________
Bob Rashkin

rrashkin

Posts: 35
Joined: Tue Feb 12, 2013 4:47 pm

### Re: Pythagorean triplets in python

n_squared.pop(0)

use

n_squared.remove(j)

(or maybe i)

No that doesn't work rrashkin
Person1001

Posts: 7
Joined: Thu Jul 04, 2013 10:14 am

### Re: Pythagorean triplets in python

Thanks Yoriz. Do you have an answer to the original question though?

Yoriz wrote:T,, ,,
Person1001

Posts: 7
Joined: Thu Jul 04, 2013 10:14 am

### Re: Pythagorean triplets in python

You want to avoid crossing the same pair of square numbers twice but the number 625 appears twice as it from two differant pairs
Code: Select all
`49 + 576 = 625225 + 400 = 625`
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1443
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Pythagorean triplets in python

Yoriz wrote:You want to avoid crossing the same pair of square numbers twice but the number 625 appears twice as it from two differant pairs
Code: Select all
`49 + 576 = 625225 + 400 = 625`

Cleverly spotted. Thanks Yoriz
Person1001

Posts: 7
Joined: Thu Jul 04, 2013 10:14 am