The program will produce lines containing the factors of ONLY perfect numbers, W
ID: 3635681 • Letter: T
Question
The program will produce lines containing the factorsof ONLY perfect numbers, WITHOUT DOUBLE-CALCULATING
(each Perfect Number is found and displayed via the same
calculation, so the program doesn't waste time calculating
the same thing twice. In other words, you build the complete
output line, but you only print it one the program has
established you've found the factors of a Perfect Number.
HINT: sprintf
Sample output:
6: 1 2 3 sum: 6
28: 1 2 4 7 14 sum: 28
496: 1 2 4 8 16 31 62 124 248 sum: 496
8128: 1 2 4 8 16 32 64 127 254 508 1016 2032 4064 sum: 8128
this is what i have so far.
#include<iostream>
using namespace std;
int main()
{
for (int i=1 ; ; i++)
{
char buf[1024], *cp;
cp = buf;
cout << i << endl;
int total = 0;
for (int j = 1; j <= i/2; j++)
if (i % j == 0)
total += j;
if (total == i)
{
//cout << i << ": " ;
//for( int x = 1 ; x < i ; x++)
{
cp += sprintf(cp, "%d", i, ": %d" , total);
//
//cout << x << " ";
//}
//cout << "sum: " << i << endl;
}
}
}
Explanation / Answer
#include using namespace std; void perfect(int); int main() { for(int number=2; numberRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.