I just started to learn Python and started to make some problems just to help build my skills, but I am very fixated on this issue.
Make a list containing all positive integers up to 1000, the squares of which can be expressed as the sum of two squares (i, e., Integers p, for which p ^ 2 = m ^ 2 + n ^ 2, where m and n are integers numbers greater than 0.)
Tips: There are several approaches. You may find it helpful to have a list of all square numbers. An operator may be helpful.
Here is the code I came up with so far:
numbers=xrange(1001) numbers_squared=[x**2 for x in numbers] a=[] for x in numbers_squared: for b in numbers_squared: if (x+b)**.5 <= 1001: a.append(x+b) print a
The problem I am facing is that Python takes many years to perform these calculations (I waited about ten minutes and still printed the numbers). Any hints on how to solve this problem would be greatly appreciated.
ps The main thing is to use lists. Hints would also be more appreciated than the decision itself.
Thanks!
Dizzle
source share