Last active
October 4, 2018 09:55
-
-
Save AmaranthLIS/4d032cfbdf7daf03752e96538c4894b8 to your computer and use it in GitHub Desktop.
calculate Fibonacci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Fibonacci { | |
public static long fibonacci(int n) { | |
if (n <= 1) return n; | |
else return fibonacci(n-1) + fibonacci(n-2); | |
} | |
public static void main(String[] args) { | |
int n = Integer.parseInt(args[0]); | |
for (int i = 1; i <= n; i++) | |
System.out.print(i + ": " + fibonacci(i)); | |
} | |
//or simple example | |
/* | |
int n = 100, t1 = 0, t2 = 1; | |
System.out.print("Upto " + n + ": "); | |
while (t1 <= n) { | |
System.out.print(t1 + ", "); | |
int sum = t1 + t2; | |
t1 = t2; | |
t2 = sum; | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment