Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
50 50 problem
12-09-2009, 09:43 AM (This post was last modified: 12-09-2009 09:46 AM by codecaine.)
Post: #1
50 50 problem
This seems like a easy program. I think they have a glitch on there site because they say my value is invalid. But I tested each function to make sure the returns were accurate. The link to this problem is here http://www.cstutoringcenter.com/problems....php?id=50

I attached my sourcecode at the bottom. I used my Extended Math Class Tongue

In honor of our 50th problem, what is the sum of the 50th Fibonacci number, 50th prime number and the 50th odd number?

Recall that the Fibonacci sequence looks like:

1 1 2 3 5 8 13 .....
the prime sequence looks like:

2 3 5 7 11 13 .....
and we all know the sequence of odd numbers which is trivial.

Main program
Code:
public class MathTest
{
    public static void main(String[] args)
    {
        final long ODD50 = MathExtended.oddPos(50); //holds 50th odd number
        final long FIB = MathExtended.fib(50); //holds 50th fibonacci number
        
        final int MAX = 49; //Hold the max value to count up too. Since I am starting at the primeIndex at 3 I took one from max because 2 is the only even prime number
        long result = 0; //hold the sum of 50th odd, fib, and prime number
        int count = 1; //hold how count of each encounter of prime number
        long primeIndex = 3; //the primeIndex is checked by isPrime to check for prime numbers
        
        //loop to get the 50th prime number
        while(count != MAX)
        {
            if(MathExtended.isPrime(primeIndex))
                count++;
            primeIndex += 2;
        }
        System.out.println("primeIndex " + primeIndex);
        System.out.println("fib " + FIB);
        System.out.println("Odd " + ODD50);
        result = ODD50 + FIB + primeIndex; //holds the sum as stated above
        System.out.println("Sum = " + result); //print the result
    }
}

My extended math class
Code:
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;
}

}

Without syntax highlighting etc code looks sloopy on here. You are better of downloading the zip.


Attached File(s)
.zip  MathExtended.zip (Size: 4.65 KB / Downloads: 0)
Visit this user's website Find all posts by this user
Quote this message in a reply
12-09-2009, 09:58 AM
Post: #2
RE: 50 50 problem
Good Code Big Grin a
and i think we should get syntax highlighting code tags

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
12-09-2009, 10:04 AM
Post: #3
RE: 50 50 problem
Yea anything with a decent amount of code looks like crap lol
Visit this user's website Find all posts by this user
Quote this message in a reply
12-10-2009, 03:54 AM
Post: #4
RE: 50 50 problem
I must say very good code codecaine. Good math class though !
@ Back_track
You are right about syntax highlighting. I am going to search for addon for that.
I am testing some addons and i am going to improve some things soon.

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: