JAVA
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* Created by Dan Walker
*
* This explores the impact of having (in)efficient code. The naive fibonacci code uses
* recursion to work out the previous two entries. This is how fibonacci numbers are
* defined, so this makes sense. However, try and go much past 40 and the time limit
* will be exceeded (try this code on your own computer, it will take a long time to
* work out the 50th fibonacci number!)
*
* The second method is much more efficient - each result is recorded in a Map as it is
* worked out. This map is checked first for an existing value, if this is not found
* then the value is calculated and added to the map. See how far you can calculate now!
* Comment out the NaiveFibonacci line and Uncomment the following line.
*
* Updated 27/12/2017 - instead of double (why did I even use that xD),
* NaiveFibonacci and CleverFibonacci use long. Added BigInteger version to prevent
* overflow. Interface updated to a generic type so that each method can return a
* type that extends Number.
*
*
*/
import java.util.*;
import java.math.BigInteger;
public class Program
{
public static void main(String[] args) {
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run