Version without JavaScript, read-only: workshops, quizzes and running code are not available here. Full version · Catalogue · Sign in
Sequence
Fibonacci sequence
Core exercise
Write fibonacci(int n) that returns the n-th term, with fibonacci(0) = 0 and fibonacci(1) = 1.
A workshop goes here: you write and run code in it. It requires JavaScript.
Starting code
Exercices.java
public class Exercices {
public static void main(String[] args) {
for (int i = 0; i <= 10; i++) {
System.out.print(fibonacci(i) + " ");
}
System.out.println();
}
public static long fibonacci(int n) {
// TODO
return 0;
}
}
The core exercise is validated in the workshop, which requires JavaScript.
Hint 1
Iterative version: two variables that shift along at each iteration. Recursive version: elegant, but it recomputes the same terms thousands of times: try fibonacci(45) to see.