I'm posting the latest version now; the output seems fine except for how it's showing 1000 as 31 (the sum of the factors is 41, though, which seems to be correct).
#include <iostream>
#include <math.h>
// function prototypes
bool isDivisible (int number, int divisor);
bool isPrime (int number);
void factorize_and_sum(int number);
usingnamespace std;
int main ()
{
for (int i = 0; i <= 1000; i++)
{
if (!isPrime(i))
{
factorize_and_sum(i);
}
else
{
cout << "Number is prime\n";
}
}
}
bool isPrime (int number)
{
for ( int i = 2; i < number; i++)
{
if ( isDivisible( number, i ) )
{
returnfalse;
}
}
returntrue;
}
bool isDivisible (int number, int divisor)
{
return number % divisor == 0;
}
void factorize_and_sum(int number)
{
int sum = 0;
int i = 2;
while (i < number)
{
if(number % i == 0)
{
cout << i <<" ";
sum += i;
number /= i;
i = 1;
}
i++;
}
sum += number;
if (isPrime(sum))
{
cout << "number=" << number << "\n";
cout << "sum of prime factors of " << number << " is: " << sum << "\n";
}
else
{
cout << "Sum of factors is not prime\n";
}
return;
}
It's the factorization and adding factors together with a loop parts and I don't get right now
Well, you know what, if you don't get it than just split it up again :)
make a function that returns all factors and then work with them the way you like
#include <iostream>
#include <vector>
#include <cmath>
bool isDivisable(int number, int divisor)
{
return number % divisor == 0;
}
bool isPrime (int number)
{
int max = sqrt(number);
for ( int i = 2; i <= max; i++)
{
if ( isDivisible( number, i ) )
{
returnfalse;
}
}
returntrue;
}
std::vector<int> getPrimeFactors(int number)
{
int max = sqrt(number);
std::vector<int> primes;
for(int i = 2; i <= max; ++i) // loop through same numbers as isPrime to get prime factors
{
if(isPrime(number)) // check if the number is a prime Factor
{
primes.push_back(number); // add element to container
break; // break for-loop
}
if(isPrime(i)) // if it is a prime
{
if(isDivisable(number, i)) // and divisable, then it is a prime factor of number
{
number /= i;
primes.push_back(i); // add element to container
i--; // check same factor again
}
}
}
return primes;
}
int main(void)
{
for(int i = 0; i <= 1000; ++i)
{
if(isPrime(i))
{
std::cout << i << " -> is a Prime." << std::endl;
}
else
{
std::vector<int> factors; // a C++ Container class
factors = getPrimeFactors(i);
// do the printing work
int sum = 0;
std::cout << i << ': ';
for(int i = 0; i < factors.size(); ++i)
{
std::cout << factors[i] << ' '; // print the factors
sum += factors[i]; // add the factors up
}
std::cout << " = " << sum << " -> ";
if(isPrime(sum))
std::cout << "sum of factors is a Prime";
else
std::cout << "sum of factors is not a Prime";
std::cout << std::endl;
}
}
}
#include <iostream>
#include <cmath>
int main () {
std::cout << "The sum of prime factors of the following number's is Prime: \n";
for(int num=2; num<=1000; num++){
int res=num, sum=0, i=2;
int rootres = sqrt(res);
while(i<=rootres){
if(res %i == 0){
sum +=i;
res /= i;
rootres = sqrt(res);
continue;
}
i++;
}
if(sum) {
sum += res;
bool b=1;
int rootsum = sqrt(sum);
for(int i=2; i<=rootsum; i++){
if(sum%i == 0){
b=0;
break;
}
}
if(b) std::cout << num << "\n";
}
}
return 0;
}
#include <iostream>
#include <cmath>
#include <string>
int main () {
std::cout << "The sum of prime factors of the following number's is Prime: \n";
std::string s;
for(int num=2; num<=1000; num++){
int res=num, sum=0, i=2;
int rootres = sqrt(res);
s = "";
while(i<=rootres){
if(res %i == 0){
s += std::to_string(i) + "+";
sum +=i;
res /= i;
rootres = sqrt(res);
continue;
}
i++;
}
if(sum) {
s += std::to_string(res) + "= ";
sum += res;
bool b=1;
int rootsum = sqrt(sum);
for(int i=2; i<=rootsum; i++){
if(sum%i == 0){
b=0;
break;
}
}
if(b) std::cout << num << ": " << s << sum << "\n";
}
}
return 0;
}