1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17class Main {
18
19/*
20    // Iterative version
21    static int fibonacci(int n) {
22        if (n == 0) {
23            return 0;
24        }
25        int x = 1;
26        int y = 1;
27        for (int i = 3; i <= n; i++) {
28            int z = x + y;
29            x = y;
30            y = z;
31        }
32        return y;
33    }
34*/
35
36   // Recursive version
37   static int fibonacci(int n) {
38        if ((n == 0) || (n == 1)) {
39            return n;
40        } else {
41            return fibonacci(n - 1) + (fibonacci(n - 2));
42        }
43    }
44
45    public static void main(String[] args) {
46        String arg = (args.length > 0) ? args[0] : "10";
47        try {
48            int x = Integer.parseInt(arg);
49            int y = fibonacci(x);
50            System.out.printf("fibonacci(%d)=%d\n", x, y);
51            y = fibonacci(x + 1);
52            System.out.printf("fibonacci(%d)=%d\n", x + 1, y);
53        } catch (NumberFormatException ex) {
54            System.err.println(ex);
55            System.exit(1);
56        }
57    }
58}
59