Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fibonacci sequence challenge
12-15-2009, 09:11 AM (This post was last modified: 12-15-2009 09:12 AM by codecaine.)
Post: #1
fibonacci sequence challenge
Challenge found here: http://www.cstutoringcenter.com/problems....php?id=13

Given the first few numbers of the Fibonacci sequence:

1 1 2 3 5 8 13 21 ...
What is the first Fibonacci number to contain 15 digits?

Give the actual 15 digit number for the answer.

Answer: 117669030460994

My extended math class with advance math functions
Code:
import java.util.*;

public class MathExtended {

    //returns true if the number is prime and false if the number not prime
    public static boolean isPrime(long num)
    {
        final long HALF_MAX = num / 2;
        if(num == 2)
            return true;
        if(num < 2)
            return false;
        if((num % 2) == 0)
            return false;
        for(int x = 3; x < HALF_MAX; x = x + 2 )
            if( (num % x) == 0)
                return false;
            return true;
    }
    

    //returns true if a number is a perfect number else return false
    public static boolean isPerfect(long number)
    {
        final long i = MathExtended.sumFactors(number); //see below
        if (i == number)
            return true;
        else
            return false;
    }

    //returns the sum of factors of a number
    public static long sumFactors(long number)
    {
        long factor = 0, sum = 0;
        for (int i = 1; i < number; i++){ //don't include the number itself, only its factors
            if ((number % i) == 0){
                factor = i;
                sum += factor;
            }
        }
        return sum;
    }

    // Evaluate n!
    public static long factorial( long n )
    {
        if( n <= 1 )     // base case
            return 1;
        else
            return n * factorial( n - 1 );
    }

    //returns fibonacci
    public static long fib(long n)
    {
        if (n < 2)
            return(1);
        return( fib(n-2) + fib(n-1) );
    }

    //return nth odd number given
    public static long oddPos(long num)
    {
        final int STEP = 2; //increments value by 2
        long value = -1; //holds odd numbers
        for(long x = 0; x < num; x++)
        {
            value += 2;
        }
        return value;
    }

    //return nth odd number given
    public static long evenPos(long num)
    {
        final int STEP = 2; //increments value by 2
        long value = 0; //holds even numbers
        for(long x = 0; x < num; x++)
        {
            value += 2;
        }
        return value;
    }
    
    //returns a list of fibonacci sequence
    public static List fibSequence(long n)
    {
         List list = new ArrayList();
         long a=0,b=1;
         for (long i=0;i<n;i++){
              list.add((long)a);
              a=a+b;
              b=a-b;
         }
         return list;
    }

    //returns the sum of fibonacci sequence
    public static long fibSum(long n)
    {
         long a=0,b=1;
         long sum = 0;
         for (int i=0;i<n;i++){
              sum += a;
              sum = sum + a;
              a=a+b;
              b=a-b;
          }
         return sum;
    }

    //returns a list of fibonacci sequence
    public static void fibSequencePrint(long n)
    {
         long a=0,b=1;
         System.out.println("Fibonacci Sequence:");
         for (long i=0;i<n;i++)
         {
              System.out.println(a);
              a=a+b;
              b=a-b;
         }
    }    

}

//main program
Code:
import java.util.*;

public class Main {
    public static void main(String[] args) {
        final int SIZE = 70;
        List list = MathExtended.fibSequence(SIZE); //return fibonacci sequence
        for(int x = 0; x < SIZE; x++) //loop through fibonacci sequence
        {
            if( (list.get(x).toString().length()) == 15) //check value length at giving list index
            {
                //print sequence number and its index then exit the program
                System.out.println("First number to containt 15 digits in the sequence is: " + list.get(x) + " at index " + x);
                System.exit(0);
            }
        }
        //if the program makes it to this point there was no number with the giving length
        System.out.println("There was no value witht he length of " + SIZE);
    }
}


Attached File(s)
.zip  fib sequence length.zip (Size: 5.52 KB / Downloads: 2)
Visit this user's website Find all posts by this user
Quote this message in a reply
12-15-2009, 08:07 PM
Post: #2
RE: fibonacci sequence challenge
I can see that your class is growing !
You did some great coding there !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: