Finding perfect number within 1 to input

I tried but i didnt manage to figure out the right way to find all perfect numbers from 1 to input pleaseee helppp

#include <iostream>
using namespace std;

int Perfect(int posInt){
int total=0;
for (int i=1; i<posInt; i++){
if (posInt%i==0)
total+=i;
if (total==i)
cout << total;}
return total;
}

int main(){
int input,i;
cin >> input;
if (input >0){
i=Perfect(input);
cout << i;}
else
cout << "error, please enter a positive integer";

return 0;
}

By Perfect Number do you mean https://en.wikipedia.org/wiki/Perfect_number ? So the first four are 6, 28, 496, 8128 . This reminds me of some of the questions from the site Project Euler, which had some math-based programming questions, and which I tended to solve in a scripting language like Ruby ;D

1. But anyway, first come up with an algorithm. If indeed this is the kind of perfect number they're looking for, as per the list at http://oeis.org/A000396 , this stuff scales up FAST. Look again what kind of upper ceiling the input could be, which could be the difference of integer (int) or really big numbers (long long)
2. Don't panic; break it up into parts. Looks like it involves prime numbers -- do you have a way of generating a prime number list? Perhaps create a function that generates the first 10000 prime numbers in a vector or so, and then in your Perfect() function, start iterating through them [2,3,5,7...] to generate that 2^(p-1) * (2^p - 1) , which seems valid, until upper limit is reached.
@icy1 thanks for ur reply
Yesss my Question requires e.g. input 100 ouput 6 28
i didnt get the prime number part
Why we need prime number to find the prefect number?
Thank you sooo much
what do you think p is in the wiki page?

Edit: since for this particular question, the output scales up so fast and you might not anticipate huge input from user, you could simply have a bunch of primes hard-coded and avoid making a prime-generation method. vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19}; Could add a comment explaining the limitations of the input.
Last edited on
Topic archived. No new replies allowed.