A Java program that prints successive Fibonacci numbers and their prime factorizations
Here is the file Primes.java:
// Calculate and print factors of successive Fibonacci numbers.
public class Primes
{
public static void main (String[] args) {
int f1 = 1, f2 = 2, n = 3;
int f, f3, lim, nextdiv, divisor;
boolean nodiv;
while (n <= 45) { // find factors of nth number
System.out.print("n: " + n + "\t");
System.out.print("F(n): " + f2 + " ");
if (n < 40) System.out.print("\t");
System.out.print("\tFactors: ");
f = f2;
while (f%2 == 0) { // check 2 as divisor
f = f/2;
System.out.print(2 + " ");
}
// all other divisors
nextdiv = 3;
while (f != 1) {
// set lim > sqrt(f)
lim = 2;
while (lim*lim < f)
lim = lim*2;
nodiv = true; // no divisors so far
divisor = nextdiv;
while (nodiv && (divisor < lim)) {
while (f%divisor == 0) {
f = f/divisor;
System.out.print(divisor + " ");
nodiv = false;
}
divisor = divisor + 2;
}
nextdiv = divisor;
if (nodiv) { // no divisor found
System.out.print(f);
f = 1;
}
} // end "while (f != 1)"
// move on to next number
System.out.println();
n = n + 1;
f3 = f1 + f2;
f1 = f2;
f2 = f3;
} // end "while (n <= 40)"
} // end of main
}
Here is the output when the Java program is run:
n: 3 F(n): 2 Factors: 2
n: 4 F(n): 3 Factors: 3
n: 5 F(n): 5 Factors: 5
n: 6 F(n): 8 Factors: 2 2 2
n: 7 F(n): 13 Factors: 13
n: 8 F(n): 21 Factors: 3 7
n: 9 F(n): 34 Factors: 2 17
n: 10 F(n): 55 Factors: 5 11
n: 11 F(n): 89 Factors: 89
n: 12 F(n): 144 Factors: 2 2 2 2 3 3
n: 13 F(n): 233 Factors: 233
n: 14 F(n): 377 Factors: 13 29
n: 15 F(n): 610 Factors: 2 5 61
n: 16 F(n): 987 Factors: 3 7 47
n: 17 F(n): 1597 Factors: 1597
n: 18 F(n): 2584 Factors: 2 2 2 17 19
n: 19 F(n): 4181 Factors: 37 113
n: 20 F(n): 6765 Factors: 3 5 11 41
n: 21 F(n): 10946 Factors: 2 13 421
n: 22 F(n): 17711 Factors: 89 199
n: 23 F(n): 28657 Factors: 28657
n: 24 F(n): 46368 Factors: 2 2 2 2 2 3 3 7 23
n: 25 F(n): 75025 Factors: 5 5 3001
n: 26 F(n): 121393 Factors: 233 521
n: 27 F(n): 196418 Factors: 2 17 53 109
n: 28 F(n): 317811 Factors: 3 13 29 281
n: 29 F(n): 514229 Factors: 514229
n: 30 F(n): 832040 Factors: 2 2 2 5 11 31 61
n: 31 F(n): 1346269 Factors: 557 2417
n: 32 F(n): 2178309 Factors: 3 7 47 2207
n: 33 F(n): 3524578 Factors: 2 89 19801
n: 34 F(n): 5702887 Factors: 1597 3571
n: 35 F(n): 9227465 Factors: 5 13 141961
n: 36 F(n): 14930352 Factors: 2 2 2 2 3 3 3 17 19 107
n: 37 F(n): 24157817 Factors: 73 149 2221
n: 38 F(n): 39088169 Factors: 37 113 9349
n: 39 F(n): 63245986 Factors: 2 233 135721
n: 40 F(n): 102334155 Factors: 3 5 7 11 41 2161
n: 41 F(n): 165580141 Factors: 2789 59369
n: 42 F(n): 267914296 Factors: 2 2 2 13 29 211 421
n: 43 F(n): 433494437 Factors: 433494437
n: 44 F(n): 701408733 Factors: 3 43 89 199 307
n: 45 F(n): 1134903170 Factors: 2 5 17 61 109441
Here is a long
version of the same program, with n going up to 90
(before the long integers overflow), and
here is the output.
Revision date: 2001-01-25.
(Please use ISO
8601, the International Standard.)