English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Do it using C Language. So, I will check your answer in my Borland C. I will pick the best answer for the fastest and the right answer. Better fasten your seat belt !?

Find the Perfect Number.

Sample Number : 28
The factorial from 28 is : 1,2,4,7,14
If you multiple the factorial, you will get back the sample Number :
1+2+4+7+14 = 28.
That's called Perfect Number.

Sample input :
6
28

Sample output :
1,2,3
1,2,4,7,14

2006-10-04 03:07:21 · 7 answers · asked by Rabbit-X 2 in Computers & Internet Programming & Design

7 answers

I shouldn't be doing this but.... This code will generate all perfect numbers from an interval which you place into the stdin (the console). For example if you want all perfect numbers from [1,1000] just type 1 1000. Here is the code:

#include

bool perfect(int x){
int s=0;
for (int i=1;i<=(x/2)+1;i++)
if (x%i==0) s+=i;
if (s==x) return true;
return false;
}

void gen(int x){
for (int i=1;i<=(x/2)+1;i++)
if (x%i==0) printf("%d ",i);
printf("\n");
}

int main() {
int a,b;
scanf("%d %d",&a,&b);
if ((a*b<0) || (a>b)) {printf("Input error!"); return 1;}
for (int i=a;i<=b;i++)
if (perfect(i)) {printf("%d :",i); gen(i);}
return 0;
}

Be careful how you ask qusetions next time :). That's not multiplying.

2006-10-04 03:38:05 · answer #1 · answered by agent-X 6 · 0 0

thats not a factorial e.g a factorial for 7 is 1*2*3*4*5*6*7

,
and thats adition not multiple(1+2+4+7+14)

2006-10-04 10:17:19 · answer #2 · answered by tru_story 4 · 0 0

They're kinda called factors not factorials... Oh and you're excluding the number itself, which is always a factor of itself... But I'm gonna base it without the number itself... I'm guessing you mean a program that finds perfect numbers... so... Here it goes:

#include
#include
#include
#include
using namespace std;

int findFactorsSum(int k)
{
int sum = 0;
for(int i = 1; i < k; ++i)
{
if(k%i==0)
{
sum+=i;
}
}
return sum;
}
int main(int argc, char *argv[])
{
if ( argc == 3)
{
istringstream buffer(argv[1]);
int start;
int end;
buffer >> start;
istringstream buffer2(argv[2]);
buffer2 >> end;
for(int i = start; i < end; ++i)
{
int sum = findFactorsSum(i);
if (sum == i)
{
cout << i << endl;
}
}
}
else
{
cout << "Usage: pn " << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}

2006-10-04 10:52:39 · answer #3 · answered by Deadlyx 1 · 0 0

Certainly NOT gonna write in C. Would take some effort in VB, but I don't feel like doing it right now. Also, this is not necessarly for IT experts -- more for developers as most "IT experts" don't do a whole lot of C developing.

2006-10-04 10:15:17 · answer #4 · answered by Brian D 3 · 0 0

You said multiple the factoral when actually you are adding the factorals

2006-10-04 10:16:15 · answer #5 · answered by ewtaylor2001 5 · 0 0

huh

2006-10-04 10:09:39 · answer #6 · answered by Anonymous · 0 0

THATS IT.....

..........U GOT IT!

2006-10-04 10:11:01 · answer #7 · answered by monaUK 5 · 0 0

fedest.com, questions and answers