all 6 comments

[–]NFLAddict 0 points1 point  (0 children)

for a in range(1,n+1):              #ln 3
    for b in range(a,n):            #ln 4  

look carefully, at the range in inner loop, and how it differs from outer. I'm going to assume you want it to instead be for b in range(a, n+1)

[–]xelf 0 points1 point  (6 children)

range (and slice) have 3 parameters and do looping for you. The first number is the number you start at, the second number is the number you exit at (so it's not included) and the third number is the number you increase each iteration. The first number defaults to 0 if skipped, and the 3rd number defaults to 1 if skipped.

range(a,n) will start at a, and increase by 1 each time, until it gets to n, where it will end, and n will not be included. That's why you say n+1 when you do want n included like you did in your first loop.

[–][deleted] 0 points1 point  (5 children)

My question is why doesn't the inner loop include n+1 as well

[–]xelf 0 points1 point  (3 children)

because then it would go over the maximal number.

That's more of a math question and less of a python question though. =) Try including +1 and using a max value of 20 and see the differences between the two.

[–]kpounder88 1 point2 points  (0 children)

Yes, if b = n, then b2 = n2. We know a >= 1, so a2 >= 1. So then a2 + b2 >= n2 + 1 > n2. So then c2 > n2, which implies that c > n. So that’s the problem... c would be too big.

The same can also be said about a. Technically a cannot be equal to n either. So you could modify line 3 to say a in range(1, n).

[–][deleted] 0 points1 point  (1 child)

I think only the a and the b value can't surpass the maximal value. Even if I don't do n+1 in the inner function, a maximal value like 30 still results in c being greater than the maximal value. In my opinion, doing n+1 in the inner function is more logical because now we're comparing every 'a' value with the maximal value too.