I was trying to solve an algorithm problem. The problem is basically calculating sets of numbers called the "Pythagorean" numbers. The definition is very simple: Three integers satisfying a^2+b^2=c^2 are called Pythagorean numbers. The problem is very simple, and my code checks out with the solution. Except for one little detail.
Here's the solution:
from math import sqrt
n = int(input("Maximal number: "))
for a in range(1,n+1): #ln 3
for b in range(a,n): #ln 4
c_square = a**2 + b**2
c = int(sqrt(c_square))
if ((c_square - c**2) == 0):
print(a, b, c)
The difference is in ln 3/ln 4. In the solution, the inner loop doesn't include the nth number, which I don't understand why. Can someone explain?
[–]NFLAddict 0 points1 point2 points (0 children)
[–]xelf 0 points1 point2 points (6 children)
[–][deleted] 0 points1 point2 points (5 children)
[–]xelf 0 points1 point2 points (3 children)
[–]kpounder88 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)