Skip to content

Instantly share code, notes, and snippets.

@hugot
Last active September 7, 2017 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugot/de8e24767e80142f6b4d766ac1c8f185 to your computer and use it in GitHub Desktop.
Save hugot/de8e24767e80142f6b4d766ac1c8f185 to your computer and use it in GitHub Desktop.
package nl.hva.dmci.ict.se.datastructures.fibonacci;
import java.math.BigInteger;
import java.util.function.Consumer;
import edu.princeton.cs.algs4.Stopwatch;
public class FastFibonacci {
static double timeStart;
static int fiboNumber = 0;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
final int SIZE;
if (args.length == 0) {
SIZE = 100;
} else {
SIZE = Integer.parseInt(args[0]);
}
Stopwatch stopwatch = new Stopwatch();
FibjesMachine fibonator = new FibjesMachine(SIZE);
System.out.printf(" n %15s ( lap \t total)\n", "fib(n)");
timeStart = stopwatch.elapsedTime();
fibonator.iterate((BigInteger fibje) -> {
double timeEnd = stopwatch.elapsedTime();
double lapTime = timeEnd - timeStart;
fiboNumber++;
System.out.printf("%2d %15d (%.3f \t %.3f)\n", fibje, fiboNumber, lapTime, timeEnd);
timeStart = stopwatch.elapsedTime();
});
}
private static class FibjesMachine {
BigInteger[] fibjes;
int nextFibjeIdx;
public FibjesMachine(int fibjesAmount){
fibjes = new BigInteger[fibjesAmount];
nextFibjeIdx = -1;
}
public void iterate(Consumer<BigInteger> con){
for(int i=0; i < fibjes.length; i++){
con.accept(nextFibje());
}
}
public BigInteger nextFibje(){
nextFibjeIdx++;
if(nextFibjeIdx < 2 ){
fibjes[nextFibjeIdx] = BigInteger.valueOf(1);
return BigInteger.valueOf(1);
}
else {
fibjes[nextFibjeIdx] = fibjes[nextFibjeIdx - 2].add(fibjes[nextFibjeIdx -1]);
return fibjes[nextFibjeIdx];
}
}
}
}
package nl.hva.dmci.ict.se.datastructures.fibonacci;
import edu.princeton.cs.algs4.Stopwatch;
/**
* A small program that calculates Fibonacci numbers.
* This program is used by students to investigate the Fibonacci algorithm.
*
* @author Huub van Thienen
* @author Nico Tromp
*/
public class Fibonacci {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
final int SIZE;
if (args.length == 0) {
SIZE = 100;
} else {
SIZE = Integer.parseInt(args[0]);
}
Stopwatch stopwatch = new Stopwatch();
System.out.printf(" n %15s ( lap \t total)\n", "fib(n)");
for (int n = 1; n <= SIZE; n++) {
double timeStart = stopwatch.elapsedTime();
long fiboNumber = fib(n);
double timeEnd = stopwatch.elapsedTime();
double lapTime = timeEnd - timeStart;
System.out.printf("%2d %15d (%.3f \t %.3f)\n", n, fiboNumber, lapTime, timeEnd);
}
}
/**
* Berekent het n-de Fibonacci-getal.
*
* @param n Het hoeveelste Fibonacci-getal
* @return De waarde van het Fibonacco-getal
*/
public static long fib(int n) {
if (n < 2) {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
}
}
@Bramsterdam
Copy link

lekker bezig pik

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment