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);
}
}