Thread: Project Euler Solved but want help improving the code Newbie

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    8

    Project Euler Solved but want help improving the code Newbie

    Hey a friend introduced me to Project Euler and having fun solving there problems. I'm a really newbie and only been programming for 5 days now so please explain everything like you would to child. If you dont know what Project Euler is, its math problems solved mainly with programming (google it). Just done Problem 3:

    The prime factors of 13195 are 5, 7, 13 and 29.

    What is the largest prime factor of the number 600851475143 ?

    Solved it but there are some changes I would like to make to improve it, that I require your help for.

    First I would like to add in x % 2 into the condition for the second FOR command ie replace y <= x with y <= x % 2. I thought I could simply replace the term but that didnt work also have tried creating a new integer and writing that equal to x%2 after the first IF command but that didnt work either.

    Secondly at the moment the only way to see the prime factors is that the output only shows the prime factors once and non prime factors several times. I would like the code to show only the prime factors or even better the highest prime factor. I think I can do that with an ELSE command if I can get the x % 2 to work but how would you do it.

    Thirdly I am using code blocks 8.02 as recommended in the tutorials the output only seems to show a certain number of outputs after that it deletes the top number to place the last number. How can I remove this and show all outputs?

    Finally this is my first time using tags I have read the sticky but apologies if I get this wrong.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
        long long int num=600851475143LL;
    
        for ( int x=1 ; x<num ; x++)
        if ( num%x==0 )
    
        for ( int y = 2; y <= x ; y++)
        if ( x % y == 0 )
        cout<< x <<endl;
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    First of all indent the code like this:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
        long long int num=600851475143LL;
    
        for ( int x=1 ; x<num ; x++)
            if ( num % x == 0 )
                for ( int y = 2; y <= x ; y++)
                    if ( x % y == 0 )
                         cout<< x <<endl;
    }
    So you tried this:
    Code:
        for ( int x=1 ; x < num ; x++)
            if ( num % x == 0 )
                for ( int y = 2; y <= x % 2; y++)
    ...
    That is a valid code. Don't know if its the logic you want though. What you do is check y from 2 to the x modulo 2. Is that the correct logic? I am kind of bored to search how the algorithm works. But if you tell me with words I ll give you the right code

    So, why do you print what you print if it is not the prime factor? You confuse me a bit on this part. I would guess that you try to find the prime factors and print only those.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by C_ntua View Post
    So you tried this:
    Code:
        for ( int x=1 ; x < num ; x++)
            if ( num % x == 0 )
                for ( int y = 2; y <= x % 2; y++)
    ...
    The maximum value of x%2 is 1. So the second loop here would never even loop, since y=2 is already more than 1.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    I suspect he wants:
        for ( int x=1 ; x < num ; x++)
            if ( num % x == 0 && x % 2 != 0)
                for ( int y = 2; y <= x; y++)
    ...
    The symbol && is the logical AND. SO both num % x ==0 AND x % 2 !=0 has to be true. Meaning x has to be even.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, no point in going all the way to "num", sqrt(num) is the largest number that can evenly divisible in num - of course you have the other part of that, so a *b, either a or b must be less or equal to sqrt(n).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Project Euler Question
    By Head In Jar Man in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2009, 02:59 PM
  2. want to start contributing c code to a project
    By stabu in forum C Programming
    Replies: 8
    Last Post: 12-18-2008, 09:15 PM
  3. Newbie Source Code question. Hlp Plz?
    By BOOGIEMAN in forum Game Programming
    Replies: 4
    Last Post: 12-14-2001, 04:30 AM