Constants

  • x1: first number in sequence
  • x2: second number in sequence
  • x3: sum on x1 and x2
  • n: amount of numbers in sequence
  • i: count

For Loop

  • Loops sequence (fibonacci math) FOR as long as the max number is greater than or equal to the count (i).
// For Loop
public class fibo {
 
	public static void main(String[] args) 
	{
		// n = the number of numbers in sequence
		 int n = 25; 
		 int x1 = 0;
		 int x2 = 1;
		 
	        System.out.print(n+ " numbers in sequence:\n");
 
	        for (int i = 1; i <= n; ++i)
	        {
	            System.out.print(x1 + " ");
	      
				// x1 = first number, x2 = second number, x3 is the sum of 1 and two.
	            int x3 = x1 + x2;
	            x1 = x2;
	            x2 = x3;
	        }
	}
}
fibo.main(null);
25 numbers in sequence:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 

While Loop

  • Loops sequence (fibonacci math) WHILE the max number is greater than or equal to the count (i).
// While Loop
class fibo {
    public static void main(String[] args) {
        try {
            int x1 = 0, x2 = 1, n = 25;

            System.out.print(n+ " numbers in sequence:\n");
            int i = 0;

            while (i < n) {

                System.out.print(x1 + " ");

                int x3 = x2 + x1;
                x1 = x2;
                x2 = x3;
                i = i + 1;
            }


        } catch (Exception e) {  
            System.out.println("Input must be an integer  " + e);
        }
        
    }
}
fibo.main(null)
25 numbers in sequence:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 

Input

  • Uses a while loop for takes in the users INPUT instead of a pre-registered max number.
// Input
class fibo {
    public static void main(String[] args) {
        Scanner input;

        input = new Scanner(System.in);
        System.out.print("Type in amount of numbers for sequence  -->  ");
        try {
            int n = input.nextInt();
            int x1 = 0, x2 = 1;
            System.out.print(n + "\n");

            int i = 0;

            while (i < n) {

                System.out.print(x1 + " ");

                int x3 = x2 + x1;
                x1 = x2;
                x2 = x3;
                i = i + 1;
            }


        } catch (Exception e) {  
            System.out.println("Input must be an integer  " + e);
        }
        
    }
}
fibo.main(null)
Type in amount of numbers for sequence  -->  25
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 

Recursion

  • Recalls itself (RECURSES) (fibonacci math) for as long as the max number is greater than the count (i).
// Recursion
public class fibo{
	public static int fibonacciRecursion(int x){
	if(x == 0){
		return 0;
	}
	if(x == 1 || x == 2){
		return 1;
	}
	return fibonacciRecursion(x-2) + fibonacciRecursion(x-1);
	}
    public static void main(String args[]) {
	int n = 25;
	System.out.print(n + " numbers in sequence:\n");
	for(int i = 0; i < n; i++){
			System.out.print(fibonacciRecursion(i) + " ");
		}
	}
}
fibo.main(null)
25 numbers in sequence:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 

Summary

Run Times

  • For Loop: 0.1 seconds
  • While Loop: 0.1 seconds
  • Input: 0.1 seconds disregarding user
  • Recursion: 0.1 seconds

Difference between For Loop and While Loop

  • A For Loop will loop for an already predetermined amount of times.
  • A While Loop will continue to loop until a condition is no longer met.

Using Extends

/*
 * Creator: Nighthawk Coding Society
 * Mini Lab Name: Fibonacci sequence, featuring a Stream Algorithm
 * 
 */

import java.util.ArrayList;  
import java.util.HashMap;
import java.util.stream.Stream;

/* Objective will require changing to abstract class with one or more abstract methods below */
public class Fibo {
    String name;  // name or title of method
    int size;  // nth sequence
    int hashID;  // counter for hashIDs in hash map
    ArrayList<Long> list;   // captures current Fibonacci sequence
    HashMap<Integer, Object> hash;  // captures each sequence leading to final result

    /*
     Zero parameter constructor uses Telescoping technique to allow setting of the required value nth
     @param: none
     */
    public Fibo() {
        this(20); // telescope to avoid code duplication, using default as 20
    }

    /*
     Construct the nth fibonacci number
     @param: nth number, the value is constrained to 92 because of overflow in a long
     */
    public Fibo(int nth) {
        this.size = nth;
        this.list = new ArrayList<>();
        this.hashID = 0;
        this.hash = new HashMap<>();
        //initialize fibonacci and time mvc
        this.init();
    }

    /*
     This Method should be "abstract"
     Leave method as protected, as it is only authorized to extender of the class
     Make new class that extends and defines init()
     Inside references within this class would change from this to super
     Repeat process using for, while, recursion
     */
    protected void init() {
        this.name = "Stream";
        Stream.iterate(new long[]{0, 1}, f -> new long[]{f[1], f[0] + f[1]})
            .limit(this.size)
            .forEach(f -> this.setData(f[0]) );
    }

    /*
     Number is added to fibonacci sequence, current state of "list" is added to hash for hashID "num"
     */
    public void setData(long num) {
        list.add(num);
        hash.put(this.hashID++, list.clone());
    }

    /*
     Custom Getter to return last element in fibonacci sequence
     */
    public long getNth() {
        return list.get(this.size - 1);
    }

    /*
     Custom Getter to return last fibonacci sequence in HashMap
     */
    public Object getNthSeq(int i) {
        return hash.get(i);
    }

    /*
     Console/Terminal supported print method
     */
    public void print() {
        System.out.println("Init method = " + this.name);
        System.out.println("fibonacci Number " + this.size + " = " + this.getNth());
        System.out.println("fibonacci List = " + this.list);
        System.out.println("fibonacci Hashmap = " + this.hash);
        for (int i=0 ; i<this.size; i++ ) {
            System.out.println("fibonacci Sequence " + (i+1) + " = " + this.getNthSeq(i));
        }
    }

    /*
    Tester class method.  If this becomes abstract you will not be able to test it directly ...
    Change this method to  call "main" class of each of the extended classes
     */
    static public void main(String[] args) {
        Fibo fib = new Fibo();
        fib.print();
    }
}
Fibo.main(null);
Init method = Stream
fibonacci Number 20 = 4181
fibonacci List = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]
fibonacci Hashmap = {0=[0], 1=[0, 1], 2=[0, 1, 1], 3=[0, 1, 1, 2], 4=[0, 1, 1, 2, 3], 5=[0, 1, 1, 2, 3, 5], 6=[0, 1, 1, 2, 3, 5, 8], 7=[0, 1, 1, 2, 3, 5, 8, 13], 8=[0, 1, 1, 2, 3, 5, 8, 13, 21], 9=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34], 10=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55], 11=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89], 12=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144], 13=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233], 14=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], 15=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610], 16=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987], 17=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597], 18=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584], 19=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]}
fibonacci Sequence 1 = [0]
fibonacci Sequence 2 = [0, 1]
fibonacci Sequence 3 = [0, 1, 1]
fibonacci Sequence 4 = [0, 1, 1, 2]
fibonacci Sequence 5 = [0, 1, 1, 2, 3]
fibonacci Sequence 6 = [0, 1, 1, 2, 3, 5]
fibonacci Sequence 7 = [0, 1, 1, 2, 3, 5, 8]
fibonacci Sequence 8 = [0, 1, 1, 2, 3, 5, 8, 13]
fibonacci Sequence 9 = [0, 1, 1, 2, 3, 5, 8, 13, 21]
fibonacci Sequence 10 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
fibonacci Sequence 11 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
fibonacci Sequence 12 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
fibonacci Sequence 13 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
fibonacci Sequence 14 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]
fibonacci Sequence 15 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
fibonacci Sequence 16 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
fibonacci Sequence 17 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
fibonacci Sequence 18 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]
fibonacci Sequence 19 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584]
fibonacci Sequence 20 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]

While Loop Extends

public class FiboWhile extends Fibo {

    public FiboWhile() {
        this(10); // telescope to avoid code duplication, using default
    }
  
    public FiboWhile(int nth) {
        this.size = nth;
        this.list = new ArrayList<>();
        this.hashID = 0;
        this.hash = new HashMap<>();
        //initialize fibonacci and time mvc
        this.init();
    }
  
    static public void main(String[] args) {
        FiboWhile wFib  = new FiboWhile();
        wFib.print();
    }
  
    // Custom init using while loop instead
    protected void init() {
        this.name = "While Loop";
        int i = 0;
        // new long array with only 0
        long[] f = new long[] {0,1};
        //while i is less than the length desired
        while (i < this.size) {
            // sets data first, then adds to f
            this.setData(f[0]);
            f = new long[] {f[1], f[0] + f[1]};
            // adds to counter
            i++;
        }
    }
  }
  
  FiboWhile.main(null)
Init method = While Loop
fibonacci Number 10 = 34
fibonacci List = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
fibonacci Hashmap = {0=[0], 1=[0, 1], 2=[0, 1, 1], 3=[0, 1, 1, 2], 4=[0, 1, 1, 2, 3], 5=[0, 1, 1, 2, 3, 5], 6=[0, 1, 1, 2, 3, 5, 8], 7=[0, 1, 1, 2, 3, 5, 8, 13], 8=[0, 1, 1, 2, 3, 5, 8, 13, 21], 9=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]}
fibonacci Sequence 1 = [0]
fibonacci Sequence 2 = [0, 1]
fibonacci Sequence 3 = [0, 1, 1]
fibonacci Sequence 4 = [0, 1, 1, 2]
fibonacci Sequence 5 = [0, 1, 1, 2, 3]
fibonacci Sequence 6 = [0, 1, 1, 2, 3, 5]
fibonacci Sequence 7 = [0, 1, 1, 2, 3, 5, 8]
fibonacci Sequence 8 = [0, 1, 1, 2, 3, 5, 8, 13]
fibonacci Sequence 9 = [0, 1, 1, 2, 3, 5, 8, 13, 21]
fibonacci Sequence 10 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Recursion Extends

public class FiboRecur extends Fibo {

    public FiboRecur() {
        this(10); // telescope to avoid code duplication, using default
    }
  
    public FiboRecur(int nth) {
        this.size = nth;
        this.list = new ArrayList<>();
        this.hashID = 0;
        this.hash = new HashMap<>();
        //initialize fibonacci and time mvc
        this.init();
    }
  
    static public void main(String[] args) {
        FiboRecur rFib  = new FiboRecur();
        rFib.print();
    }
  
    // Recursion function, using int i as a counter
    protected void recur(long[] f, int i) {
        // Sets data using beginning value
        this.setData(f[0]);
        // Cretes new long
        f = new long[]{f[1], f[0] + f[1]};
        // Adds to counter
        i++;
        if (i < this.size) {
            // Recusion continues if size limit has not been reached
            this.recur(f, i);
        }
        
    }
  
    // Custom init using recursion loop instead
    protected void init() {
        this.name = "Recursion";
        // Uses int i as a counter for recusion, also creates initial long[]
        int i = 0;
        long[] f = new long[] {0,1};
        // input long and counter i into recursion function
        this.recur(f, i);
    }
  }
  
  FiboRecur.main(null)
Init method = Recursion
fibonacci Number 10 = 34
fibonacci List = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
fibonacci Hashmap = {0=[0], 1=[0, 1], 2=[0, 1, 1], 3=[0, 1, 1, 2], 4=[0, 1, 1, 2, 3], 5=[0, 1, 1, 2, 3, 5], 6=[0, 1, 1, 2, 3, 5, 8], 7=[0, 1, 1, 2, 3, 5, 8, 13], 8=[0, 1, 1, 2, 3, 5, 8, 13, 21], 9=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]}
fibonacci Sequence 1 = [0]
fibonacci Sequence 2 = [0, 1]
fibonacci Sequence 3 = [0, 1, 1]
fibonacci Sequence 4 = [0, 1, 1, 2]
fibonacci Sequence 5 = [0, 1, 1, 2, 3]
fibonacci Sequence 6 = [0, 1, 1, 2, 3, 5]
fibonacci Sequence 7 = [0, 1, 1, 2, 3, 5, 8]
fibonacci Sequence 8 = [0, 1, 1, 2, 3, 5, 8, 13]
fibonacci Sequence 9 = [0, 1, 1, 2, 3, 5, 8, 13, 21]
fibonacci Sequence 10 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

For Loop Extends

public class FiboFor extends Fibo {

    public FiboFor() {
        this(10); // telescope to avoid code duplication, using default
    }

    public FiboFor(int nth) {
        this.size = nth;
        this.list = new ArrayList<>();
        this.hashID = 0;
        this.hash = new HashMap<>();
        //initialize fibonacci and time mvc
        this.init();
    }

    static public void main(String[] args) {
        FiboRecur rFib  = new FiboRecur();
        rFib.print();
    }

    // Custom init using for loop instead
    protected void init() {
        this.name = "For loop";
        // creates initial long[]
        long[] f = new long[] {0,1};
        
        // iterates through until reaches desired length
        for (int i = 0; i < this.size; i++) {
            // sets data, then updates f
            this.setData(f[0]);
            f = new long[] {f[1], f[0] + f[1]};
        }
    }
}

FiboFor.main(null)
Init method = Recursion
fibonacci Number 10 = 34
fibonacci List = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
fibonacci Hashmap = {0=[0], 1=[0, 1], 2=[0, 1, 1], 3=[0, 1, 1, 2], 4=[0, 1, 1, 2, 3], 5=[0, 1, 1, 2, 3, 5], 6=[0, 1, 1, 2, 3, 5, 8], 7=[0, 1, 1, 2, 3, 5, 8, 13], 8=[0, 1, 1, 2, 3, 5, 8, 13, 21], 9=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]}
fibonacci Sequence 1 = [0]
fibonacci Sequence 2 = [0, 1]
fibonacci Sequence 3 = [0, 1, 1]
fibonacci Sequence 4 = [0, 1, 1, 2]
fibonacci Sequence 5 = [0, 1, 1, 2, 3]
fibonacci Sequence 6 = [0, 1, 1, 2, 3, 5]
fibonacci Sequence 7 = [0, 1, 1, 2, 3, 5, 8]
fibonacci Sequence 8 = [0, 1, 1, 2, 3, 5, 8, 13]
fibonacci Sequence 9 = [0, 1, 1, 2, 3, 5, 8, 13, 21]
fibonacci Sequence 10 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]