Download code

From LiteratePrograms

Jump to: navigation, search

Back to Fibonacci_numbers_(Java)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

FibonacciIterative.java

 1 /* Copyright (c) 2008 the authors listed at the following URL, and/or
 2 the authors of referenced articles or incorporated external code:
 3 http://en.literateprograms.org/Fibonacci_numbers_(Java)?action=history&offset=20081124071032
 4 
 5 Permission is hereby granted, free of charge, to any person obtaining
 6 a copy of this software and associated documentation files (the
 7 "Software"), to deal in the Software without restriction, including
 8 without limitation the rights to use, copy, modify, merge, publish,
 9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 
24 Retrieved from: http://en.literateprograms.org/Fibonacci_numbers_(Java)?oldid=15481
25 */
26 
27 public class FibonacciIterative {
28 	public static int fib(int n) {
29                 int prev1=0, prev2=1;
30                 for(int i=0; i<n; i++) {
31                     int savePrev1 = prev1;
32                     prev1 = prev2;
33                     prev2 = savePrev1 + prev2;
34                 }
35                 return prev1;
36 	}
37 
38         public static void main(String[] args) {
39 	    for (int i=0; i<=46; i++)
40 	        System.out.print(fib(i)+", ");
41 	}
42 
43 }
44 


FibonacciMemoized.java

 1 /* Copyright (c) 2008 the authors listed at the following URL, and/or
 2 the authors of referenced articles or incorporated external code:
 3 http://en.literateprograms.org/Fibonacci_numbers_(Java)?action=history&offset=20081124071032
 4 
 5 Permission is hereby granted, free of charge, to any person obtaining
 6 a copy of this software and associated documentation files (the
 7 "Software"), to deal in the Software without restriction, including
 8 without limitation the rights to use, copy, modify, merge, publish,
 9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 
24 Retrieved from: http://en.literateprograms.org/Fibonacci_numbers_(Java)?oldid=15481
25 */
26 
27 import java.math.BigInteger;

28 import java.util.ArrayList;

29 
30 public class FibonacciMemoized {
31         
32        private static ArrayList<BigInteger> fibCache = new ArrayList<BigInteger>();
33        static {
34              fibCache.add(BigInteger.ZERO);
35              fibCache.add(BigInteger.ONE);
36        }
37 
38 
39        public static BigInteger fib(int n) {
40               if (n >= fibCache.size()) {
41                   fibCache.add(n, fib(n-1).add(fib(n-2)));
42               }
43               return fibCache.get(n);
44        }
45 
46         public static void main(String[] args) {
47 	    for (int i=0; i<=46; i++)
48 	        System.out.print(fib(i)+", ");
49 	}
50 
51 }
52 


Fibonacci.java

 1 /* Copyright (c) 2008 the authors listed at the following URL, and/or
 2 the authors of referenced articles or incorporated external code:
 3 http://en.literateprograms.org/Fibonacci_numbers_(Java)?action=history&offset=20081124071032
 4 
 5 Permission is hereby granted, free of charge, to any person obtaining
 6 a copy of this software and associated documentation files (the
 7 "Software"), to deal in the Software without restriction, including
 8 without limitation the rights to use, copy, modify, merge, publish,
 9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 
24 Retrieved from: http://en.literateprograms.org/Fibonacci_numbers_(Java)?oldid=15481
25 */
26 
27 public class Fibonacci {
28 	public static int fib(int n) {
29                 if (n < 2) {
30                    return n;
31                 }
32                 else {
33 		   return fib(n-1)+fib(n-2);
34                 }
35 	}
36 
37         public static void main(String[] args) {
38 	    for (int i=0; i<=46; i++)
39 	        System.out.print(fib(i)+", ");
40 	}
41     
42 }
43 


Views
Personal tools