Sunday, February 10, 2013

Fibonacci Sequence:

Fibonacci Sequence:This example shows how to generate Fibonacci series.Please check the Execute logs how it executes .

/* Fibonacci Sequence: The first two Fibonacci numbers are 0 and 1 and then the next Fibonacci number is found by adding the previous two together  */

// Setup the initial variables to hold the old number, current number, and fibonacci number
Integer oldNumber = 0;
Integer currentNumber = 1;
Integer fibonacciNumber = 0;

// use a loop to output all the fibonacci numbers between 0 and 100
while (fibonacciNumber < 100) {
     
      System.Debug(fibonacciNumber);  // output the Fibonacci number

      // calculate the next fibonacci number by adding the
      // current number to the old number
      fibonacciNumber= currentNumber + oldNumber;
     
      // now set the new values of the old and current numbers
      oldNumber = currentNumber;
      currentNumber = fibonacciNumber;
}


No comments:

Post a Comment