# Thread: Appropriate usage of functions

1. ## 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. 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. Ok, thanks.