blob: bfe04aaded4fc4e69ef48d780ccc1a0a3f853dd0 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Brian Carlstrom9f30b382011-08-28 22:41:38 -070016
17class Fibonacci {
18
19 static int fibonacci(int n) {
20 if (n == 0) {
21 return 0;
22 }
23 int x = 1;
24 int y = 1;
25 for (int i = 3; i <= n; i++) {
26 int z = x + y;
27 x = y;
28 y = z;
29 }
30 return y;
31 }
32
33 public static void main(String[] args) {
34 try {
35 if (args.length == 1) {
36 int x = Integer.parseInt(args[0]);
37 int y = fibonacci(x); /* to warm up cache */
38 System.out.printf("fibonacci(%d)=%d\n", x, y);
Brian Carlstromc2282522011-09-17 10:33:14 -070039 y = fibonacci(x + 1);
40 System.out.printf("fibonacci(%d)=%d\n", x + 1, y);
Brian Carlstrom9f30b382011-08-28 22:41:38 -070041 }
42 } catch (NumberFormatException ex) {}
43 }
44}