Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In a pythagorean triplet,
a^2 + b^2 = c^2,
there exist one (a,b,c)
for which a+b+c = 1000.
for this a,b,c find abc.

I am attaching my codes. I think my logic is correct but not getting the answer. I am getting error "Time Limit exceeded". Earlier also I have faced the same problem whenever i used large numbers in the for loop. Is there any limit to the number that we enter in the range function?Is there any other method to do such task, code with large numbers?

Thanks in advance.

What I have tried:

Python
for a in range(1,1000):
    for b in range(1,1000):
        for c in range(1,1000):
            if a**2 + b**2 == c**2:
                if a+b+c == 1000:
                    print(a*b*c)
Posted
Updated 2-Jul-19 10:56am

Your approach is an unrefined "brute force" method, which uses nested loops. So the code inside all three loops gets executed 1000 times for c, which gets executed 1000 times for b, which gets executed 1000 times for a: which means that to work out the number of times the first if test is executed is simple, just multipole 1000 by 1000 by 1000: 1,000,000,000 - one billion times. No matter how fast your processor, no matter how quick the code inside all three loops is, that's going to take significant time. And if it finds a result on the first try - which it won't, but bear with me - it will still do the remaining 999,999,999 iterations before it finished.

Think about the algorithm better: is there a way you can reduce the number of values significantly? Well yes. For starters you can eliminate the inner loop because you know that a + b + c = 1000, therefore c = 1000 - (a + b). For every combination of a and b, c is a fixed value so you don't need to loop at all! Suddenly, you are down to 1,000,000 tests, and they are simpler. Add code to exit if you find the right result and that chops out more loops.

And that's before you start using actual formulae to solve it: Pythagorean triple - Wikipedia[^]

Seriously - this is your homework, not ours: dump teh brute force approach, and think about more intelligent solutions. And the quickest solution is absolutely simple:
Python
print "375^2 + 200^2 = 425^2\n"
That won't get you any marks, though. :laugh:
 
Share this answer
 
Comments
CPallini 2-Jul-19 2:23am    
5.
May I suggest that you stop wasting your time with these mathematical problems, as they are not teaching you programming. Go to The Python Tutorial — Python 3.7.4rc1 documentation[^] and learn the actual language.
 
Share this answer
 
Quote:
am getting error "Time Limit exceeded".

This is a challenge to you from a challenge site, the answer is never the most brut force attack, like your solution.
Quote:
Is there any limit to the number that we enter in the range function?Is there any other method to do such task, code with large numbers?

Your problem is not here, the problem is that you did not do any analyze.
a, b, c are not free independent variables, they are linked, constrained, you have to take advantage of the constraints to reduce solutions search.
Think about it: when you know a and b, how many values are possible for c ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900