Results 1 to 6 of 6

Math Help - Project Euler 3

  1. #1
    Newbie
    Joined
    Jan 2013
    From
    England
    Posts
    3

    Project Euler 3

    Hello, I am really confused on this problem on projecteuler. Its so annoying!!! And its only problem 3! Im using C++ and I am really confused on how to find a technique to find whether a number is prime. My method at the moment is increasing the number that the number I am dividing it by. (600851475143). This method isnt working because I cant really test to see whether a number has more than one decimal point. (Like when 600851475143 / 3. I can use a floating point number because it cannot fit a number that big. Also rounding in C++ isnt that accurate.) Heres an example of some code that I use.

    for (int i=0; i <99999; i++);
    //code to divide number

    Sorry if this isnt that clear... I just need some hints to push me in the right direction.

    Thanks!
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    Apr 2012
    From
    Erewhon
    Posts
    159
    Thanks
    104

    Re: Project Euler 3

    Quote Originally Posted by Hazique35 View Post
    Hello, I am really confused on this problem on projecteuler. Its so annoying!!! And its only problem 3! Im using C++ and I am really confused on how to find a technique to find whether a number is prime. My method at the moment is increasing the number that the number I am dividing it by. (600851475143). This method isnt working because I cant really test to see whether a number has more than one decimal point. (Like when 600851475143 / 3. I can use a floating point number because it cannot fit a number that big. Also rounding in C++ isnt that accurate.) Heres an example of some code that I use.

    for (int i=0; i <99999; i++);
    //code to divide number

    Sorry if this isnt that clear... I just need some hints to push me in the right direction.

    Thanks!
    Use double (the default floating type for most compilers). That will allow 14 digits of integer arithmetic.

    .
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Jan 2013
    From
    England
    Posts
    3

    Re: Project Euler 3

    hmmm... Ive tried that. (probably made a mistake). Ill try it again though.

    Thanks!
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Jan 2013
    From
    England
    Posts
    3

    Re: Project Euler 3

    I'm trying double and it is big enough to hold the number, but Im still having trouble creating the for loop.
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Senior Member
    Joined
    Jan 2008
    From
    UK
    Posts
    484
    Thanks
    65

    Re: Project Euler 3

    Post your code if you're still stuck.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Bar0n janvdl's Avatar
    Joined
    Apr 2007
    From
    South Africa
    Posts
    1,630
    Thanks
    6

    Re: Project Euler 3

    Quote Originally Posted by Hazique35 View Post
    Hello, I am really confused on this problem on projecteuler. Its so annoying!!! And its only problem 3! Im using C++ and I am really confused on how to find a technique to find whether a number is prime. My method at the moment is increasing the number that the number I am dividing it by. (600851475143). This method isnt working because I cant really test to see whether a number has more than one decimal point. (Like when 600851475143 / 3. I can use a floating point number because it cannot fit a number that big. Also rounding in C++ isnt that accurate.) Heres an example of some code that I use.

    for (int i=0; i <99999; i++);
    //code to divide number

    Sorry if this isnt that clear... I just need some hints to push me in the right direction.

    Thanks!
    I have a feeling that your way of checking primes might be inefficient. Perhaps not, but you never know. I've attached my code.

    Use unsigned long longs for the big numbers. Your number returns 0, i.e. false. It's not a prime.

    Code:
    #include <iostream>
    #include <math.h>
    #include <stdio.h>
    using namespace std;
    
    bool isPrime(unsigned long long n)
    {
        unsigned long long root = floor(sqrt(n)) + 1;
    
        for (unsigned long long i = 2; i <= root; i++)
        {
            if (n % i == 0)
                return false;
        }
        return true;
    }
    
    int main()
    {
        bool Prime = isPrime(600851475143ULL);
        cout << Prime << endl;
        return 0;
    }
    Last edited by janvdl; January 20th 2013 at 06:58 AM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Euler path and Euler circuit problem
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: May 19th 2010, 08:18 PM
  2. Replies: 0
    Last Post: February 20th 2010, 08:26 AM
  3. Replies: 0
    Last Post: September 17th 2009, 06:44 PM
  4. Project Euler
    Posted in the Math Software Forum
    Replies: 34
    Last Post: May 2nd 2009, 09:10 AM
  5. Project - I need some serious help.
    Posted in the Calculus Forum
    Replies: 4
    Last Post: December 17th 2007, 10:03 PM

Search Tags


/mathhelpforum @mathhelpforum