Fibonacci
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(10); // 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();
}
/*
Fibo is a parent class that has all the functions for the the loops.
all it really does is tell the function how to create the sequence (init), how to put
it in the list (setData), and how to retrieve a number/sequence (getNth, getNthSeq).
then there's the main method that creates fib, which is an instance of the Fibo class
*/
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);
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)
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";
// 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)
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)