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.
I'm pretty satisfied with what I did, but I want a review. Can I make it better? If I have a lot to improve on, examples would be great.
#include <iostream>
#include <string>
//function prototype
int PrimeFactor(int number);
bool isPrime(int PrimeOrNot);
int main() {
int i;
int PrintThePrime=0;
//here we take the numbers from 2-1000 instead of 0-1000 because 0 is not normal number and not a prime number and number one is both so we considor it like none and just gonna ignore this two folks
for (i = 2; i < 1000; i++) {
if (isPrime(i)) { //check if the number already a prime , cause if he is we do not need waist performance and run the all function =)
std::cout << "Already a Prime :" << i << std::endl;
}
//here is begin the fun =)
else {
PrintThePrime = PrimeFactor(i); //function that perform some things and return THE SUM FACTORS OF THE NUMBER.
//check if the return number is a prime.
if (isPrime(PrintThePrime)){
std::cout << i << ": "; //print the original number before the performance of prime tree.
std::cout << PrintThePrime << std::endl; //the actuall Factor sum of the original number.
}
}
}
return 0;
}
int PrimeFactor(int number)
{
int i;
int SecondHalf;
int sum = 0;
for (i = 2; i < number; i++) // the divider of the loop
{
if (i == 4 || i == 8 || i == 6){ //purpose if the number not divisible by 2 and 3 there is no purpose to check if they divisible by 4|6|8 and that because every number from here is divisible by 2 or 3.
continue; //another note take a look and see that divide by 9 is either not needble cause every number that divides by 9 divides by 3
} //but he is not here cause the loop never gonna reach the number 9 , numbers 4|8|6 can be reachable if the original number is not divisible by 2|3 but divisble by 5|7.
if (number % i == 0) { //just check what ever divider is gonna be used
number = number / i; //first half of the number
SecondHalf = number*i / number; //second half of the number
if (isPrime(number)) //check if the first half of the number is now a prime
sum = sum + number;
else //if the first half is not an prime the divider of teh loop gonna be reset and the loop start again but now from the loop prespective the origin number is the first_half
i = 1; //that gonna be divided to two halfs.
sum = sum + SecondHalf; //second half gonna be always putted into the sum and that cause it the divider and the dividers that used in this loop is only primes =)
}
}
return sum; //and here fun is comming to end , but wait there is a bit more =)
}
//function that called a lot and check if the number is prime.
bool isPrime(int PrimeOrNot)
{
int i;
for (i = 2; i < PrimeOrNot; i++) //take a number and divide him from 2 to it self-1 , and that cause prime can be divided only by 1 and itself.
{
if (PrimeOrNot % i == 0) //if the number that divided is dividable by on of the loop number , he is not a prime and at that point the loop can finish.
return false; //the indicator of that , that the number is not a prime.
}
return true; //if the number is goin through the all loop and not dividable by anyone of the number he is a prime , take a look and see something tricky a bit the number 2 is gonna be a prime
//just because he is not answer to the loop condition , but it alright he is really a prime.
}