CodeWorm

Tidbits of code and other items


Google
 
[home]
[java]

How to use JavaScript with a Java Applet

  • The code to my applet is listed below and the source to the JavaScript can be viewed by simply viewing the the source for this webpage.

Find the value of the Fibonacci Number

 

Recursive Fibonacci Calculation

/* The Nth Fibonacci number is defined as the sum of the (N-1)th Fibonacci number plus the (N-2)th Fibonacci number. The exception is that if N is less than or equal to 2, the Nth Fibonacci number is defined to be 1.
*/

public class Fibonacci extends java.applet.Applet {
  public long N;

  private long fib(long n) {
    if (n <= 2)
      return 1;
    else
      return fib(n - 1) + fib(n - 2);
  }

  public void Fibonacci (long n) {
    N = fib(n);
  }
}