Rohan G Class running Fibonacci
Week 5 Class running Fibonacci
/*
* 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);
public class FiboForLoop extends Fibo {
public FiboForLoop() {
this(10);
}
public FiboForLoop(int nth) {
this.size = nth;
this.list = new ArrayList<>();
//initialize fibonacci and time mvc
this.init();
}
protected void init(){
this.name = "For Loop";
this.setData(0);
this.setData(1);
for(int i = 2; i <= this.size; ++i){
this.setData(this.list.get(i-2)+this.list.get(i-1));
}
}
static public void main(String[] args) {
FiboForLoop fib = new FiboForLoop();
fib.print();
}
}
FiboForLoop.main(null);
public class FiboWhileLoop extends Fibo {
public FiboWhileLoop() {
this(10);
}
public FiboWhileLoop(int nth) {
this.size = nth;
this.list = new ArrayList<>();
//initialize fibonacci and time mvc
this.init();
}
protected void init(){
this.name = "While Loop";
this.setData(0);
this.setData(1);
int i = 2;
while(i <= this.size){
this.setData(this.list.get(i-2)+this.list.get(i-1));
i++;
}
}
static public void main(String[] args) {
FiboWhileLoop fib = new FiboWhileLoop();
fib.print();
}
}
FiboWhileLoop.main(null);
public class FiboRecursiveLoop extends Fibo {
public FiboRecursiveLoop() {
this(10);
}
public FiboRecursiveLoop(int nth) {
this.size = nth;
this.list = new ArrayList<>();
//initialize fibonacci and time mvc
this.init();
}
protected void init(){
this.name = "Recursion";
for(int n = 0; n < this.size; n++){
this.setData(this.calc(n));
}
}
public int calc(int n) {
if (n < 2) {
return n;
}
return this.calc(n-2) + this.calc(n-1);
}
static public void main(String[] args) {
FiboRecursiveLoop fib = new FiboRecursiveLoop();
fib.print();
}
}
FiboRecursiveLoop.main(null);
College Board Standards
- Skill 1.B:Determine code that would be used to complete code segments (ie For, While, Recursion)
- For the For-Loop and While-Loop implementation/method for fibo I used an initial line to iterate through the sequence but the foor loop uses a counter while the while is automated. For the recursive method, I used a line that called the function repeadetly until it stops at the right number. ### Skill 4.C: Determine if two or more code segments yield equivalent results (be sure to Discuss how you know results are the same)
- All 4 of the code segments implemented in my fibonacci algorithm all yield the same printed results. I know the results are the same because I can compare the results from each method. ### Skill 5.A: Describe the behavior of a given segment of program code (describe the difference in recursion versus for & while loops, perhaps add timing to determine speed)
- The for-loop, while-loop, and recursion methods all take around the same amount of time around 90,000 nanoseconds to execute. However, the Stream method takes a lot more time at 7,545,500 nanoseconds to execute. I used System.nanoTime() in order to determine the time.