Hi Mek,

Still having no luck with this despite your help, thanks.

This is the full, original C++ code that I wrote:

- Code: Select all
`/*`

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;

}

Outputs like this when I run the above in xcode:

- Code: Select all
`1 has prime factors that add together to equal a prime number!`

The sum of the prime factors are: 0

2 has prime factors that add together to equal a prime number!

The sum of the prime factors are: 2

3 has prime factors that add together to equal a prime number!

The sum of the prime factors are: 3

5 has prime factors that add together to equal a prime number!

The sum of the prime factors are: 5

6 has prime factors that add together to equal a prime number!

The sum of the prime factors are: 5

7 has prime factors that add together to equal a prime number!

The sum of the prime factors are: 7

.....etc etc etc.....

I am going over my python code again and again, plus looking at your snippet. Made some changes, but still can't seem to find the source of the zero output.

*EDIT* Ok, I made some changes to my program to find out why I get no ouput. Seems I am stuck in an infinite loop:

- Code: Select all
`import math`

def get_sum_of_prime_factors(number):

x = 2

sum_of_prime_factors = 0

while True:

if (number % x == 0):

number = number / x

sum_of_prime_factors += x

print "HELLO"

else:

x += 1

print "GOODBYE" #I am stuck in this loop here

if (x <= number):

break

def sum_of_prime_factors_is_prime(number):

for i in range(2, int(math.sqrt(number)+1)):

if (number % i == 0):

return False

return True

for i in range(1, 50):

if (sum_of_prime_factors_is_prime(get_sum_of_prime_factors(i))):

print "{} has prime factors that add together to equal a prime number".format(i)

print "The sum of prime factors are {}".format(get_sum_of_prime_factors(i))

else:

print "Hello program, can you hear me?!?!" #this should run AT LEAST, however, this line never runs!

Output:

- Code: Select all
`GOODBYE`

GOODBYE

GOODBYE

GOODBYE

GOODBYE

GOODBYE

...and on and on and on...