We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have the number 700821472143. I'm trying to apply Fermat's factorisation to find the greatest prime factor of it.
Here's my code so far:
long n = 700821472143L;
long[] results = new long[2];
boolean isInteger(float num) {
return num%1==0;
}
void run() {
for (int i=0; i < n; i++) {
if(isInteger(sqrt(n+i*i))) {
results[0] = (long)sqrt(n+i*i)+i;
results[1] = (long)sqrt(n+i*i)-i;
break;
}
}
}
void setup() {
run();
println(results[0] + " " + results[1]);
}
Now the problem with the above is that I can't even get this to give me the two factors of the form of (x-y)*(x+y) which make up my big value. There seem to be unavoidable rounding errors from Processing. I get 837,694 and 836,608. If I multiply those two numbers together on my calculator I don't get 700821472143... I get 700821501952 . How can I avoid these errors?
Answers
float
instead ofdouble
.long
value's precision, use methods fromclass
Math instead:https://Docs.Oracle.com/javase/8/docs/api/java/lang/Math.html
final double sqrtN = Math.sqrt(i*i + n)
Thanks!