Main.java revision a74ba83a53cc2f077f1b3090e9a78f207108ed3b
12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/*
22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project
32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License.
62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at
72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software
112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and
142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License.
152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */
169f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom
179f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstromclass Fibonacci {
189f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom
199f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom    static int fibonacci(int n) {
209f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom        if (n == 0) {
219f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom            return 0;
229f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom        }
239f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom        int x = 1;
249f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom        int y = 1;
259f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom        for (int i = 3; i <= n; i++) {
269f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom            int z = x + y;
279f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom            x = y;
289f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom            y = z;
299f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom        }
309f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom        return y;
319f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom    }
329f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom
339f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom    public static void main(String[] args) {
34a74ba83a53cc2f077f1b3090e9a78f207108ed3bBrian Carlstrom        String arg = (args.length > 0) ? args[0] : "10";
359f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom        try {
36a74ba83a53cc2f077f1b3090e9a78f207108ed3bBrian Carlstrom            int x = Integer.parseInt(arg);
37a74ba83a53cc2f077f1b3090e9a78f207108ed3bBrian Carlstrom            int y = fibonacci(x); /* to warm up cache */
38a74ba83a53cc2f077f1b3090e9a78f207108ed3bBrian Carlstrom            System.out.printf("fibonacci(%d)=%d\n", x, y);
39a74ba83a53cc2f077f1b3090e9a78f207108ed3bBrian Carlstrom            y = fibonacci(x + 1);
40a74ba83a53cc2f077f1b3090e9a78f207108ed3bBrian Carlstrom            System.out.printf("fibonacci(%d)=%d\n", x + 1, y);
41a74ba83a53cc2f077f1b3090e9a78f207108ed3bBrian Carlstrom        } catch (NumberFormatException ex) {
42a74ba83a53cc2f077f1b3090e9a78f207108ed3bBrian Carlstrom            System.err.println(ex);
43a74ba83a53cc2f077f1b3090e9a78f207108ed3bBrian Carlstrom            System.exit(1);
44a74ba83a53cc2f077f1b3090e9a78f207108ed3bBrian Carlstrom        }
459f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom    }
469f30b38d74990286ce27c3a45368f73dbe3638f0Brian Carlstrom}
47