Thread: Alex's exercise Chapter 6 Ex3

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    3

    Alex's exercise Chapter 6 Ex3

    Code:
    /*Design a program that finds all numbers from 1 to 1000 whose prime factors, when added
    together, sum up to a prime number (for example, 12 has prime factors of 2, 2, and 3, which
    sum to 7, which is prime). Implement the code for that algorithm.*/
    
    #include <iostream>
    
    using namespace std;
    
    int primes(int n);
    bool isPrime(int divisor);
    bool check(int result);
    
    int main()
    {
    
        // Check numbers from 1 to 1000
        for (int i = 2; i <= 1000; ++i)
        {
            int result = primes(i);
    
            if (check(result))
            {
                cout << i << endl;
            }
        }
    }
    
    bool isPrime(int divisor)
    {
        for (int j = 2; j <= divisor/2; j++)
        {
            if (divisor % j == 0)
                return false;
        }
        return true;
    }
    
    int primes(int i)
    {
        int p = i;
        int divisor = 2;
    
        int sum = 0;
    
        while (p != 1)
        {
            cout << "p: " << p << endl;
            cout << "le divisor: " << divisor << endl;
            if (isPrime(divisor))
            {
                if (p % divisor == 0)
                {
                    p = p / divisor;
                    sum += divisor;
                }
                else
                {
                    divisor += 1;
                }
            }
    
        }
    
    
    
        return sum;
    }
    
    bool check(int result)
    {
        for (int i = 2; i <= result / 2; i++)
        {
            if (result % i == 0)
                return false;
        }
        return true;
    }
    My code does not do what is asked.

    Could anyone check it up?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Define "does not do"

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    3
    Quote Originally Posted by Shakti View Post
    Define "does not do"
    The program should print numbers whose factors sum up to prime numbers. However, there is something wrong with int primes(n) function.

    I get into infinite loop and print statements show that p : 5 and divisor : 4 and it stays like that in that loop. I cannot find a flaw in my logic.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Valentas View Post
    The program should print numbers whose factors sum up to prime numbers. However, there is something wrong with int primes(n) function.

    I get into infinite loop and print statements show that p : 5 and divisor : 4 and it stays like that in that loop. I cannot find a flaw in my logic.
    Use a debugger like gdb or what's built in to you IDE to step through loop, and verify what each variable each iteration. If you have a logic error, this will find it.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-20-2012, 07:09 AM
  2. fault in eBook Alex?
    By Reinmaster7 in forum C++ Programming
    Replies: 13
    Last Post: 05-06-2012, 11:31 PM
  3. Replies: 2
    Last Post: 04-02-2012, 05:42 AM
  4. chapter 4 K&R, getch() and ungetch()
    By blob84 in forum C Programming
    Replies: 1
    Last Post: 04-28-2011, 04:38 AM
  5. K&R chapter 8
    By Tool in forum C Programming
    Replies: 9
    Last Post: 02-22-2010, 02:05 PM