#include <iostream>
usingnamespace std;
bool isPrime (int sum);
bool isDivisible (int sum, int divisor);
int primefactors_search (int number);
int main()
{
//Variables
int primsum;
//main loop checks if the sum of the uime factors is a prime number
for (int i = 1; i < 100; i++)
{
primsum = (primefactors_search(i));
if (isPrime(primsum) == true)
{
cout << i << endl;
}
}
}
bool isPrime (int sum)
//Function checking for prime numbers
{
for (int i = 2; i < sum; i++)
{
if (isDivisible ( sum, i ) )
{
returnfalse;
}
}
returntrue;
}
bool isDivisible (int sum, int divisor)
//Subfunction of isPrime
{
return sum % divisor == 0 ;
}
int primefactors_search (int number)
//find prime factors and add them up
{
int a=0;
{
for (int i = 1; i <= number; i++)
{
if (isPrime(i)== true)
{
if ((number%i) == 0)
{
a= a+i;
return a;
}
}
}
}
}
However the program doesnt do what it should...
Can you help me and show me where i went wrong?
Any help will be much appreciated.
int primefactors_search (int number)
//find prime factors and add them up
{
int a=0;
for (int i = 1; i <= number; i++)
if (isPrime(i)== true)
if ((number%i) == 0)
a= a+i;
return a;
}
Problem is i'm getting wrong output. The program outputs 8, for example whose prime factors are 2,2 and 2, and sum up to 6, which is NOT a prime number.
I fixed the return a issue in the last paragraph.
Also "fixed" the (1) prime factor issue.
Could someone please compile this and show me what part is wrong?
#include <iostream>
usingnamespace std;
bool isPrime (int sum);
bool isDivisible (int sum, int divisor);
int primefactors_search (int number);
int main()
{
//Variables
int primsum;
//main loop checks if the sum of the uime factors is a prime number
for (int i = 1; i < 100; i++)
{
primsum = (primefactors_search(i));
if (isPrime(primsum) == true)
{
cout << i << endl;
}
}
}
bool isPrime (int sum)
//Function checking for prime numbers
{
for (int i = 2; i < sum; i++)
{
if (isDivisible ( sum, i ) )
{
returnfalse;
}
}
returntrue;
}
bool isDivisible (int sum, int divisor)
//Subfunction of isPrime
{
return sum % divisor == 0 ;
}
int primefactors_search (int number)
//find prime factors and add them up
{
int a=0;
for (int i = 2; i < number; i++)
{
if (isPrime(i)== true)
{
if ((number%i) == 0)
{
a= a+i;
}
}
}
return a;
}
the problem is that you are checking each prime factor only once. for instance if the number is 8, then the sum (a) will 2 and not 2+2+2. Since 2 is prime, the program will output 8. Hope I am clear.
in theory it should be easy since my last function returns a prime factor, so i could just use this function on my number over and over until isPrime(i) becomes true. however it seems i am too dumb to write the proper code for it.
You need to modify your function primefactors_search. instead of:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int primefactors_search (int number)
//find prime factors and add them up
{
int a=0;
for (int i = 2; i < number; i++)
{
if (isPrime(i)== true)
{
if ((number%i) == 0)
{
a= a+i;
}
}
}
return a;
}
try something like the following. (please note this code is just to give you a direction and it may contain lots of bugs)
int primefactors_search (int number)
//find prime factors and add them up
{
int a=0;
while (number != 1)
{
int found = 0;
for (int i=2;i<=number && !found;i++)
if (isPrime(i)== true)
{
if ((number%i) == 0)
{
a= a+i;
found = 1;
number /= i;
}
}
}
return a;
}
my code simply does the following:
1. finds a prime factor of the number "N".
2. adds it to the sum. divides N by that prime factor (so that it doesnt get repeated in the next iteration).
3. repeats the steps 1 & 2 till N = 1.