Pythagorean triplets in python

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

Pythagorean triplets in python

Postby Person1001 » Fri Jul 05, 2013 6:59 pm

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 1000

for 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

Postby Person1001 » Fri Jul 05, 2013 7:08 pm

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

Postby Yoriz » Fri Jul 05, 2013 7:46 pm

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 = 0
for i in m:
    if i ** 2 < 1001:
        m_squared.append(i ** 2)
    else:
        wasted_loops += 1

print 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 = 1
while 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 = 1
loop_count = 0
while True:
    result = i * i
    if result < 1001:
        squares.append(result)
        i += 1
    else:
        break
    loop_count += 1

print 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 = 1
while True:
    result = i * i
    if result < 1001:
        squares.append(result)
        i += 1
    else:
        break

print m_squared == squares

Code: Select all
True
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1026
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Pythagorean triplets in python

Postby rrashkin » Fri Jul 05, 2013 8:18 pm

instead of

n_squared.pop(0)


use

n_squared.remove(j)

(or maybe i)
___________________
Bob Rashkin
User avatar
rrashkin
 
Posts: 35
Joined: Tue Feb 12, 2013 4:47 pm

Re: Pythagorean triplets in python

Postby Person1001 » Fri Jul 05, 2013 8:24 pm

rrashkin wrote:instead of

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

Postby Person1001 » Fri Jul 05, 2013 8:25 pm

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

Postby Yoriz » Fri Jul 05, 2013 8:35 pm

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 = 625
225 + 400 = 625
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1026
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Pythagorean triplets in python

Postby Person1001 » Fri Jul 05, 2013 8:37 pm

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 = 625
225 + 400 = 625


Cleverly spotted. Thanks Yoriz :)
Person1001
 
Posts: 7
Joined: Thu Jul 04, 2013 10:14 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], scoutboy, ZeSta and 2 guests