Thread: Prime numbers and factors

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    18

    Prime numbers and factors

    Okay, so the following code is "supposed" to print numbers 1-1000 whose prime factors sum up to be a prime number. What it DOES print is seemingly random numbers that follow no pattern (primes/not primes). Here's my code:

    Code:
    #include <iostream>                     //Print numbers 1-1000 whose prime factors added together form a prime number
    bool isPrime(int number);
    bool isDivisible(int number, int divisor);
    int sum_of_pf(int number);
    
    
    using namespace std;
    
    
    int main()
    {
        for(int i=1;i<=1000;i++)
        {
          int S=sum_of_pf(i);
          if(isPrime(S))
          {
              cout<<i<<endl;
          }
        }
        cin.ignore();
        cin.get();
    }
    
    
    bool isPrime(int number)
    {
        for(int i=2; i<number; i++)
        {
            if(isDivisible(number, i))
            {
                return false;
            }
        }
        return true;
    }
    
    
    bool isDivisible(int number, int divisor)
    {
        return number%divisor==0;
    }
    
    
    int sum_of_pf(int number)
    {
        int sum=0;
        int aux=number;
        for(int i=2;i<=number/2;i++)
        {
            while(aux%i==0)
            {
                sum+=i;
                aux/=i;
            }
        }
        return sum;
    }
    Can someone point out my error(s)?

  2. #2
    Registered User
    Join Date
    Nov 2013
    Posts
    18
    Awful lot of crickets around here....

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    bool
    Code:
     isPrime(int number)
    Code:
    {
        for(int i=1; i<number; i++)
        {
            if(isDivisible(number, i))
            {
                return false;
            }
        }
    returntrue;
    }

    That was your issue....

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    18
    I'm afraid I don't understand what you're telling me to do. The function isPrime is working properly.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Shouldn't sum_of_pf() be only adding up the numbers that are prime as indicated by isPrime()?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are checking whether S is prime, but then printing i.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    hornet 07 when i changed int 2 to int 1 everything was working fine...

  8. #8
    Registered User
    Join Date
    Nov 2013
    Posts
    18
    @rcgldr
    sum_of_pf() adds the prime factors of a number, not the prime numbers.

    @tabstop
    Well, if S(sum of i's prime factors) is a prime number, then I should print the number i. That is what the exercise is saying.

    @jocdrew21
    I changed 2 into 1. No difference. Besides, when trying to establish if a number is prime, you disregard 1 as a possible divisor.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I've looked at the output of your program for the first 50 natural numbers, and it looks correct (other than for 1, whose sum of prime factors is not a prime number).

    Here's what I did: instead of merely printing out the final output, I changed the output to include the prime factors for each number, even those whose prime factors sum isn't prime. Then, for those whose prime factors sum is prime, I added some output to note the fact. Visually, I can find no fault with the result. Examining your code, I don't like the fact that sum_of_pf returns incorrect results for prime numbers, but that doesn't actually affect your final output since isPrime(0) (incorrectly) returns true.

    EDIT:
    Also, algorithmic optimisations are possible, but I'm ignoring them for the moment.
    Last edited by laserlight; 11-17-2013 at 09:58 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by hornet07 View Post
    sum_of_pf() adds the prime factors of a number, not the prime numbers.
    OK, I wasn't sure if multiples of a prime number were to be included in the sum. For example if the number is 16, is the sum supposed to be 2 or is it supposed to be 2+2+2+2 = 8 ? Based on the wiki definition of prime factor (as opposed to prime factorization), it should be 2:

    Prime factor - Wikipedia, the free encyclopedia
    Last edited by rcgldr; 11-17-2013 at 04:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime Factors
    By mgcpovoleri in forum C++ Programming
    Replies: 6
    Last Post: 06-29-2012, 02:43 PM
  2. Replies: 1
    Last Post: 03-16-2012, 02:07 AM
  3. Replies: 3
    Last Post: 02-19-2009, 10:32 PM
  4. Prime Factors
    By Adrian in forum C++ Programming
    Replies: 3
    Last Post: 10-02-2007, 09:01 PM