Download code

Jump to: navigation, search

Back to Fibonacci_numbers_(Java)

Download for Windows: zip

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

Fibonacci.java

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Fibonacci_numbers_(Java)?oldid=18538
14 */
15 
16 public class Fibonacci {
17 	public static int fib(int n) {
18                 if (n < 2) {
19                    return n;
20                 }
21                 else {
22 		   return fib(n-1)+fib(n-2);
23                 }
24 	}
25 
26         public static void main(String[] args) {
27 	    for (int i=0; i<=46; i++)
28 	        System.out.print(fib(i)+", ");
29 	}    
30 }
31 


hijacker
hijacker
hijacker
hijacker

FibonacciMemoized.java

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Fibonacci_numbers_(Java)?oldid=18538
14 */
15 
16 import java.math.BigInteger;
17 import java.util.ArrayList;
18 
19 public class FibonacciMemoized {
20         
21        private static ArrayList<BigInteger> fibCache = new ArrayList<BigInteger>();
22        static {
23              fibCache.add(BigInteger.ZERO);
24              fibCache.add(BigInteger.ONE);
25        }
26 
27        public static BigInteger fib(int n) {
28               if (n >= fibCache.size()) {
29                   fibCache.add(n, fib(n-1).add(fib(n-2)));
30               }
31               return fibCache.get(n);
32        }
33 
34         public static void main(String[] args) {
35 	    for (int i=0; i<=46; i++)
36 	        System.out.print(fib(i)+", ");
37 	}
38 }


hijacker
hijacker
hijacker
hijacker

FibonacciIterative.java

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Fibonacci_numbers_(Java)?oldid=18538
14 */
15 
16 public class FibonacciIterative {
17 	public static int fib(int n) {
18                 int prev1=0, prev2=1;
19                 for(int i=0; i<n; i++) {
20                     int savePrev1 = prev1;
21                     prev1 = prev2;
22                     prev2 = savePrev1 + prev2;
23                 }
24                 return prev1;
25 	}
26 
27         public static void main(String[] args) {
28 	    for (int i=0; i<=46; i++)
29 	        System.out.print(fib(i)+", ");
30 	}
31 }


hijacker
hijacker
hijacker
hijacker