Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Become a Java Expert!

Join 148,802 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,171 people online right now. Registration is fast and FREE... Join Now!





Fibonacci series

Fibonacci sequence is a sequence of numbers defined by f1 = 1 f2 = 1 fn = fn-1 + fn-2 First ten terms 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

Submitted By: mukesh_ranjan18
Actions:
Rating:
Views: 21,610

Language: Java

Last Modified: August 1, 2006

Snippet


  1. package com.gpt;
  2.  
  3.  
  4. import javax.swing.JOptionPane;
  5.  
  6.  /*
  7.     This program computes Fibonacci numbers using a recursive
  8.     method.
  9.   */
  10.  public class Fibonacci
  11. { 
  12.     public static void main(String[] args)
  13.     { 
  14.        String input = JOptionPane.showInputDialog("Enter n: ");
  15.        int n = Integer.parseInt(input);
  16.  
  17.        for (int i = 1; i <= n; i++)
  18.        {
  19.           int f = fib(i);
  20.           System.out.println("fib(" + i + ") = " + f);
  21.        }
  22.        System.exit(0);
  23.        
  24.     }
  25.  
  26.     /**
  27.        Computes a Fibonacci number.
  28.        @param n an integer
  29.        @return the nth Fibonacci number
  30.     */
  31.     public static int fib(int n)
  32.     { 
  33.        if (n <= 2)
  34.             return 1;
  35.        else
  36.             return fib(n - 1) + fib(n - 2);
  37.     }
  38.  }

Copy & Paste


Comments


r_m_mady 2007-11-23 09:29:15

i want it without recargane

neotrumatrix 2008-04-26 00:12:23

If the goal is to return the series till n then using recursion is a waste of time. Instead use an array and store the previous values. import javax.swing.JOptionPane; class Fibonnaci { public static void main(String args[] { String input = JOptionPane.showInputDialog("Enter n: "); int n = Integer.parseInt(input); int[] arr = new arr[n]; arr[0]=1; arr[1]=1; for(int i=2; i

neotrumatrix 2008-04-26 00:14:06

for(int i=2; i

neotrumatrix 2008-04-26 00:15:28

I tried :( Code doesn't seem to come in comments ! anyways the logic is to make use of the previous values instead of finding out the values everytime.

herefishyfishy 2008-05-01 18:50:17

There is a way to do this without recursion... there is what is called a "Closed Form". This means that the nth number in this sequence can be expressed in terms of n.

JeroenFM 2008-06-30 03:05:13

Closed form: public long fib(int n) { double termA = Math.pow((1+Math.sqrt(5))/2, n); double termB = Math.pow((1-Math.sqrt(5))/2, n); double factor = 1 / Math.sqrt(5); return Math.round(factor * (termA - termB)); }

JeroenFM 2008-07-03 05:06:56

I should also note that my implementation assumes f1 = 0 and f2=1

mostyfriedman 2008-10-24 19:24:49

good job, doing fibonacci using recursion is not really recommended though to use with large numbers..


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month