Task: First 9 prime Fibonacci number





Java: import java.math.BigInteger;

public class PrimeFibonacciGenerator {
    private PrimeGenerator primeGen = new PrimeGenerator(10000, 200000);
    private BigInteger f0 = BigInteger.ZERO;
    private BigInteger f1 = BigInteger.ONE;
    private int index = 0;

    public static void main(String[] args) {
        PrimeFibonacciGenerator gen = new PrimeFibonacciGenerator();
        long start = System.currentTimeMillis();
        for (int i = 1; i <= 26; ++i) {
            BigInteger f = gen.next();
            System.out.printf("%d: F(%d) = %s\n", i, gen.index - 1, toString(f));
        }
        long finish = System.currentTimeMillis();
        System.out.printf("elapsed time: %g seconds\n", (finish - start)/1000.0);
    }

    private PrimeFibonacciGenerator() {
        for (int i = 0; i < 2; ++i)
            primeGen.nextPrime();
    }

    private BigInteger next() {
        for (;;) {
            if (index > 4) {
                int p = primeGen.nextPrime();
                for (; p > index; ++index)
                    nextFibonacci();
            }
            ++index;
            BigInteger f = nextFibonacci();
            if (f.isProbablePrime(30))
                return f;
        }
    }

    private BigInteger nextFibonacci() {
        BigInteger result = f0;
        BigInteger f = f0.add(f1);
        f0 = f1;
        f1 = f;
        return result;
    }

    private static String toString(BigInteger f) {
        String str = f.toString();
        if (str.length() > 40) {
            StringBuilder s = new StringBuilder(str.substring(0, 20));
            s.append("...");
            s.append(str.substring(str.length() - 20));
            s.append(" (");
            s.append(str.length());
            s.append(" digits)");
            str = s.toString();
        }
        return str;
    }
}





COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. PRIME-FIBONACCI.
       
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 FIBONACCI-VARS.
          03 FIB                PIC 9(6).
          03 FIB-B              PIC 9(6).
          03 FIB-C              PIC 9(6).
          03 FIB-OUT            PIC Z(5)9.
       01 PRIME-VARS.   
          03 PRIME-FLAG         PIC X.
             88 PRIME           VALUE 'X'.
          03 DSOR               PIC 9(4).
          03 DSOR-SQ            PIC 9(6).
          03 DIV-RSLT           PIC 9(6)V9(3).
          03 FILLER             REDEFINES DIV-RSLT.
             05 FILLER          PIC 9(6).
             05 FILLER          PIC 9(3).
                88 DIVISIBLE    VALUE ZERO.
          
       PROCEDURE DIVISION.
       BEGIN.
           MOVE 1 TO FIB, FIB-B.
           PERFORM FIND-PRIME-FIBONACCI 9 TIMES.
           STOP RUN.
       
       FIND-PRIME-FIBONACCI.
           ADD FIB, FIB-B GIVING FIB-C.
           MOVE FIB-B TO FIB.
           MOVE FIB-C TO FIB-B.
           PERFORM CHECK-PRIME.
           IF NOT PRIME, GO TO FIND-PRIME-FIBONACCI.
           MOVE FIB TO FIB-OUT.
           DISPLAY FIB-OUT.
       
       CHECK-PRIME SECTION.
       BEGIN.
           MOVE SPACE TO PRIME-FLAG.
           IF FIB IS LESS THAN 5, GO TO TRIVIAL-PRIME.
           DIVIDE FIB BY 2 GIVING DIV-RSLT.
           IF DIVISIBLE, GO TO DONE.
           DIVIDE FIB BY 3 GIVING DIV-RSLT.
           IF DIVISIBLE, GO TO DONE.
           MOVE 5 TO DSOR.
           MOVE 25 TO DSOR-SQ.
           MOVE 'X' TO PRIME-FLAG.
           PERFORM TEST-DIVISOR 
           UNTIL NOT PRIME OR DSOR-SQ IS GREATER THAN FIB.
           GO TO DONE.
           
       TEST-DIVISOR.
           DIVIDE FIB BY DSOR GIVING DIV-RSLT.
           IF DIVISIBLE, MOVE SPACE TO PRIME-FLAG.
           ADD 2 TO DSOR.
           DIVIDE FIB BY DSOR GIVING DIV-RSLT.
           IF DIVISIBLE, MOVE SPACE TO PRIME-FLAG.
           ADD 4 TO DSOR.
           MULTIPLY DSOR BY DSOR GIVING DSOR-SQ.
       
       TRIVIAL-PRIME.
           IF FIB IS EQUAL TO 2 OR 3, MOVE 'X' TO PRIME-FLAG.
       DONE.
           EXIT.