Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
This is my code for project euler #9. it is meant to find the pythagorean triplet for which a+b+c=1000 and then print the product of a*b*c. however my code just runs forever with no answer being printed. any help would be appreciated.

Java
public class Problem8 {

	public static void main(String[] args) {
		
		for(int a=1, b=1;;)
		{ double c= Math.sqrt((a*a) + (b*b));
		
		if ((a+b+c) == 1000){
			System.out.println(a*b*c);
			return;}
		a++;
		c= Math.sqrt((a*a) + (b*b));
		if ((a+b+c) == 1000){
			System.out.println(a*b*c);
			return;}
		b++;
		c= Math.sqrt((a*a) + (b*b));
		if ((a+b+c) == 1000){
			System.out.println(a*b*c);
			return;}
		
		}
		

	}

}
Posted

This is due to the way you're incrementing the variables - a and b will always be either the same number, or consecutive numbers.
plain
First iteration: (1, 1); (2, 1); (2, 2);
Second iteration: (2, 2); (3, 2); (3, 3);
...


Since the Pythagorean triplet which sums to 1000 doesn't involve consecutive numbers, your code will never find the answer. Since you haven't specified an upper bound for the loop, it will keep running forever.

Try a nested loop, and specify sensible upper-bounds. For example:
C#
for (int a = 1; a < 1000; a++)
{
    for (int b = a; b < 1000; b++)
    {
        ...
    }
}
 
Share this answer
 
Comments
Patrice T 24-Jul-15 16:40pm    
Rather Brute Force
Richard Deeming 24-Jul-15 16:43pm    
Yes, but at least it works! :)

If we give the OP all of the answers, then he won't learn anything, which would defeat the purpose of the Project Euler challenges.
Patrice T 24-Jul-15 16:51pm    
Sure.
But in this case, I am not sure the OP knows what its code is doing.
see what I say in solution 4
If you explicitly write some "infinite" loop, it will be executed "infinitely".

Your loop just assigns 1 to a and b and repeats unconditionally, because there is no condition. :-) So what else could you expect? Please see:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html[^].

—SA
 
Share this answer
 
v2
Comments
Member 11861229 24-Jul-15 12:27pm    
But the if statements are supposed to print an answer and then end the code with the return statement
Sergey Alexandrovich Kryukov 24-Jul-15 12:29pm    
Sorry, it's enough.
—SA
Member 11861229 24-Jul-15 12:32pm    
so why does it run continuously without an answer
Sergey Alexandrovich Kryukov 24-Jul-15 12:35pm    
Please stop it. The answer to your second question is more than obvious.

If you want to go in for programming, learn programming. In this case, execute your code under the debugger and see what's going on, maybe it can help you. But it's apparent to an unarmed eye. I answered to your original question, so please first accept the answer formally.

—SA
You know that the pythagorean triplet follows the rule 3-4-5, so start with those numbers. Repeat your loops by incrementing the values which ensure that each side remains an integer. When they sum to 1000 or greater you stop.
 
Share this answer
 
Obviously you don't know what your code is really doing.

My only advice is learn to use a debugger and use it on your code, you will learn a lot this way. You will see with time that this 'Debug' Knowledge is priceless.

If you don't have a Debugger handy, Try to follow by hand how a,b and c evolve as your program run.

By the way, First thing to do is doing the mathematical analyse, you will see that all solutions follow a given curve, this curve should be the starting point of your programming.

You may not like my answer today, give it a try and you will see if I am wrong.
 
Share this answer
 
v3

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