Skip to content

Instantly share code, notes, and snippets.

@AmaranthLIS
Last active October 4, 2018 09:55
Show Gist options
  • Save AmaranthLIS/4d032cfbdf7daf03752e96538c4894b8 to your computer and use it in GitHub Desktop.
Save AmaranthLIS/4d032cfbdf7daf03752e96538c4894b8 to your computer and use it in GitHub Desktop.
calculate Fibonacci
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