Thread: Appropriate usage of functions

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128

    Appropriate usage of functions

    Hi everyone,

    I have just finished the practice problem 3 from chapter 7 and I have commented it in at the top. I did this so I could quickly look at what the problem was without switching back over to pdf. This is it:

    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>
    #include <cmath>
    
    int getSumOfPrimeFactors(int number);
    bool sumOfPrimeFactorsIsPrime(int number);
    
    int main()
    {
        for (int i = 1; i <= 1000; i++)
        {
            if (sumOfPrimeFactorsIsPrime(getSumOfPrimeFactors(i)))
    /*
    BREAKS DOWN LIKE THIS (FOR MY SELF REFERENCE!):
    if (sumOfPrimeFactorsIsPrime(getSumOfPrimeFactors(i))) - i is substituted with loop variable, 1 through 1000
    --- if (sumOfPrimeFactorsIsPrime(getSumOfPrimeFactors(12))) - sum of prime factors for 12 are 2 x 2 x 3, so added, 2 + 2 + 3 = 7
    ------ if (sumOfPrimeFactorsIsPrime(7)) - function works out whether 7 is a prime number or not, returns true or false
    --------- if (true)
    ------------print number
    */
            {
                std::cout << i << " has prime factors that add together to equal a prime number!" << std::endl;
                std::cout << "The sum of the prime factors are: " << getSumOfPrimeFactors(i) << std::endl << std::endl;
            }
        }
    }
    
    
    int getSumOfPrimeFactors(int number) //gets the prime factors of the number (using variable x)
    {    
        int x = 2;
        int sumOfPrimeFactors = 0;
        
        while (x <= number)
        {
            if (number % x == 0)
            {
                number = number / x;
                sumOfPrimeFactors = sumOfPrimeFactors + x; //adds together the prime factors of the number
            }
            else
            {
                x++;
            }
        }
        
        return sumOfPrimeFactors; //returns the value of the prime factors added together to be used in function sumOfPrimeFactorsIsPrime(int number)
    }
    
    bool sumOfPrimeFactorsIsPrime(int number) //works out if a number is prime or not, returns true or false accordingly
    {
        //divide each test number from isPrime(i) by up-to-or-equal the square root to find prime
        for ( int i = 2; i <= sqrt(number); i++)
        {
            if (number % i == 0)
            {
                return false;
            }
        }
        return true;
    }
    First I have to find the prime factors, then add them up, then test whether the resulting number is a prime or not. I couldn't think of another way of doing this without nesting some functions together as they all rely on each other, like a chain.

    It seems to work ok, but my question is whether this is the sort of thing you would be expected to do with functions in a problem like this (if that makes sense).

    1) Should more be in 'int main()' rather than all dumped into functions?
    2) Is nesting functions a standard thing to do in cases like this?

    Any advice on how I can improve on things are always welcome. Thanks.

    p.s. I apologise for the poor thread title, couldn't think on how to describe this exactly.

  2. #2
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    That's what functions are supposed to do. Usually to shorten the code or make things look more clear without having to dump the whole code in main. I didn't test it, but they seem fine.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Ok, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 10-08-2012, 06:48 AM
  2. Help with functions usage on project plz
    By LightYear in forum C Programming
    Replies: 15
    Last Post: 04-10-2010, 11:07 AM
  3. do {} 13% CPU usage ?!?!?!?
    By military genius in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2009, 08:57 PM
  4. VC++ 6 - CPU Usage Help
    By solem1 in forum C Programming
    Replies: 0
    Last Post: 09-08-2009, 11:16 PM
  5. Usage of functions
    By Gordon in forum Windows Programming
    Replies: 7
    Last Post: 04-17-2007, 05:58 AM