Last active
August 7, 2023 05:40
-
-
Save tusqasi/efb185fdff93ee1312d39421919af2c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
public class factorial { | |
private static double sqrt5 = Math.sqrt(5); | |
private static double Phi = (1 - sqrt5) / 2; | |
private static double phi = (1 + sqrt5) / 2; | |
public static boolean isInteger(String s) { | |
return isInteger(s, 10); | |
} | |
public static boolean isInteger(String s, int radix) { | |
if (s.isEmpty()) | |
return false; | |
for (int i = 0; i < s.length(); i++) { | |
if (i == 0 && s.charAt(i) == '-') { | |
if (s.length() == 1) | |
return false; | |
else | |
continue; | |
} | |
if (Character.digit(s.charAt(i), radix) < 0) | |
return false; | |
} | |
return true; | |
} | |
public record fib_out(long fib, HashMap<Long, Long> memo) { | |
} | |
public static long fib_binet(long n) { | |
return Math.round(Math.pow(Phi, n) - Math.pow(phi, n) / sqrt5); | |
} | |
public static fib_out fib_memo(long n, HashMap<Long, Long> memo) { | |
if (memo.containsKey(n)) { | |
return new fib_out(memo.get(n), memo); | |
} | |
if (n < 1) { | |
return new fib_out(1, memo); | |
} | |
fib_out val_1 = fib_memo(n - 1, memo); | |
val_1.memo.put(n - 1, val_1.fib); | |
fib_out val_2 = fib_memo(n - 2, val_1.memo); | |
val_2.memo.put(n - 2, val_2.fib); | |
return new fib_out(val_1.fib + val_2.fib, val_2.memo); | |
} | |
public static long fib(long n) { | |
if (n < 1) { | |
return 1; | |
} | |
return fib(n - 1) + fib(n - 2); | |
} | |
enum Speed { | |
fast, | |
instant, | |
slow | |
} | |
public static void main(String[] args) { | |
int n = 50; | |
Speed speed = Speed.slow; | |
if (args.length == 1 && isInteger(args[0])) { | |
n = Integer.parseInt(args[0]); | |
System.out.println("Finding Fibonacci numbers with no optimizations till: " + n); | |
} else if (args.length == 1 && (args[0].equals("-f") || args[0].equals("--fast"))) { | |
System.out.println("Finding Fibonacci numbers with memoization till: " + n); | |
speed = Speed.fast; | |
} else if (args.length == 1 && (args[0].equals("-i") || args[0].equals("--instant"))) { | |
System.out.println("Finding Fibonacci numbers instantly till: " + n); | |
speed = Speed.instant; | |
} else if (args.length == 2 && (args[0].equals("-f") || args[0].equals("--fast"))) { | |
n = Integer.parseInt(args[1]); | |
System.out.println("Finding Fibonacci numbers with memoization till: " + n); | |
speed = Speed.fast; | |
} else if (args.length == 2 && (args[0].equals("-i") || args[0].equals("--instant"))) { | |
n = Integer.parseInt(args[1]); | |
System.out.println("Finding Fibonacci numbers instantly till: " + n); | |
speed = Speed.instant; | |
} | |
System.out.println("fibonacci number, time taken in nanosecs to find the number "); | |
HashMap<Long, Long> memo = new HashMap<Long, Long>(); | |
for (int i = 0; i < n; i++) { | |
long start = System.nanoTime(); | |
switch (speed) { | |
case fast: | |
System.out.println(i + ", " + fib_memo(i, memo).fib + "," + (System.nanoTime() - start)); | |
break; | |
case slow: | |
System.out.println(fib(i) + "," + (System.nanoTime() - start)); | |
break; | |
case instant: | |
System.out.println(fib_binet(i) + "," + (System.nanoTime() - start)); | |
break; | |
default: | |
System.out.println(fib(i) + "," + (System.nanoTime() - start)); | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CPU: AMD Ryzen 5 4600H with Radeon Graphics
Memory: 15363MiB