I’m working on the Project Euler 25. I worked out how to do Fibonacci and I’m using BigInteger. My program seems to be running for an infinite loop (or so I think). Could it be that it is taking a long time or is it actually going into infinite loop? Can someone point me in the correct direction so I can fix it?
import java.math.BigInteger;
public class Problem25 {
public static void main(String[] args) {
getTerm(0);
}
public static void getTerm(int start) {
BigInteger var = BigInteger.ZERO;
BigInteger var2 = BigInteger.valueOf(start);
int counter = 0;
while(true) {
BigInteger temp = var.add(var2);
var = var2;
var2 = temp;
counter++;
if(var.toString().length() > 1000) {
System.out.print(counter);
}
}
}
}
EDIT: Sorry people. I thought, I had break; but thanks for your responses.
You have no condition for terminating the loop:
So it is an infinite loop. You have two (or even more) options:
while(statement)what is the condition to continue with the loop for another round.break;statement to stop the loop if a certain condition is evaluated as true.