• 01-08-2002
Unregistered
Hi,
Pleas help me to solve this ............I tried...but now ...I am feeling really tired.,my brain is standstill.....
HELPPPPPP!

The question is to find perfect number between 1 to 100.Well,who doesnt know perfect no.is the sum of its factors ,including 1 but not number itself.For eg .6 =1+2+3 is perfect no.
The problem is of finding perfect number from 1 to 100.

I have almost completed it (I assume:)....
But I am stucked on the last part.
I am checking the totalof factors with original number ..to see its perfect number...
But its not showing the return value....???
Another probelm is of scrolling of the screen fast.
I just dont know the syntax of pausing it..and where in my prog should I use it????

My code :

//function perfect//

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
using std::ios;

int perfect();//funcn prtotype

int main()
{

perfect();//calling funcn
return 0;
}
int perfect ()//funcn defn
{
int quo,rem,result,counter,count;
quo=0;
rem=0;
result=0;
for(count=1;count<=100;count++)//nos from 1 to 100
{

for(counter=1;counter<count-1;counter++) //nos for dividing it with count
{
quo=count/counter;
rem=count%counter;

if (rem==0)//checking for remainder is 0 ans showing the factors
{
cout<<count<<"its factors:"<<counter<<endl;
result=result+counter;//storing the total of factors in result

}

}
cout<<"total of factors"<<result;
cout<<endl;

result=0;
}

if(count==result) //problem lies here.......??//
{
return count;
}

}
• 01-08-2002
Unregistered
Make perfect check if only the inputted number is a perfect number. Then, in the main, create a loop going through all possible values. This will make the program a lot easier to make.
• 01-08-2002
golfinguy4
crap, it didn't sign me in for some reason.
• 01-08-2002
Uraldor
try this:
Code:

```#include <iostream.h> int main(void) {         for(int i = 2; i <= 10000; i++)         {                 int sum = 0;                 for(int j = 1; j < i; j++)                 {                         if(i % j == 0)                         {                                 sum += j;                         }                 }                 if(sum == i)                 {                         cout << i << " is a perfect number\n";                 }         }         return(0); }```
• 01-08-2002
Uraldor
here's a faster version... but it's not particularly nice :)

Code:

```#include <iostream.h> #include <math.h> #include <memory.h> #define AMOUNT 10000 int main(void) {         int factors[1000]; // buffer for storing factors;         int flags[AMOUNT];         for(int i = 2; i <= AMOUNT; i++)         {                 memset(flags, 0, sizeof(flags));                 memset(factors, 0, sizeof(factors));                 int squareRoot = (int)sqrt((double)i);                 int factor = 0;                 int sum = 0;                 int j = 0;                 for(j = 2; j <= squareRoot; j++)                 {                         if(i % j == 0)                         {                                 if(!flags[j])                                 {                                         flags[j]++;                                         factors[factor++] = j;                                 }                                 if(!flags[i / j])                                 {                                         flags[i / j]++;                                         factors[factor++] = i / j;                                 }                         }                 }                 j = 0;                 while(factors[j] != 0)                 {                         sum += factors[j++];                 }                 if(sum + 1 == i)                 {                         cout << i << " is a perfect number\n";                 }         }         return(0); }```
• 01-08-2002
Unregistered
Thanks
Thanks mate....I am recharged now :)