MnMWizard

Fibo assignment

Dec 22nd, 2017
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. //Mason Marnell - Fibo
  2.  
  3. /*
  4. * To change this license header, choose License Headers in Project Properties.
  5. * To change this template file, choose Tools | Templates
  6. * and open the template in the editor.
  7. */
  8.  
  9. import static java.lang.System.in;
  10. import java.util.Scanner;
  11.  
  12.  
  13. class Fibanaci {
  14.  
  15. public static long finoGreedy(long data)
  16. {
  17. int n1 = 1;
  18. int n2 = 2;
  19. int nextnum = n1 + n2;
  20. for (int i = 3; i < data; i++) {
  21. nextnum = n1 + n2;
  22. n1 = n2;
  23. n2 = nextnum;
  24. }
  25. return nextnum;
  26. }
  27. public static long fiboRecursion(long data)
  28. {
  29. if(data == 1) return 1;
  30. if(data == 2) return 1;
  31. return fiboRecursion(data-1) + fiboRecursion(data-2);
  32. }
  33.  
  34. public static void main(String[] args) {
  35. Scanner keyb = new Scanner(in);
  36. System.out.println("Enter which fibonaci number you want");
  37. int num = keyb.nextInt();
  38. System.out.println(finoGreedy(num));
  39. System.out.println(fiboRecursion(num));
  40. }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment