News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

Perfect numbers

Started by Danial, October 10, 2004, 03:10:20 PM

Previous topic - Next topic

Danial

Hi i wrote this code it's ment to display all the perfect numbers form 1-1000 and show all their factors but it does'nt work for some reason i would greatly appreciate any help.

#include <stdio.h>
#include <math.h>

int perfect(int num);
int factor(int num);

int main()
{
   int i;
   i=2;
   while (i<10000)     /*Display perfect numbers between 0 and 10000*/
   {
      if( perfect (i) )
      {
         printf("%d the factors of the number is\n",i);    /*This is to display the perfect number and its factor */
         factor (i);
         printf("And its factors are %d\n");
      }
      i++;
   }
   return 0;
}

int perfect(int num)
{
   int addition=0;
   int j=1;
   int Maximumlimit;
   Maximumlimit= num/2;
   for(j=1; j<Maximumlimit; j++)
   {
      if (num%j==0)
      {
         addition +=j;
      }
       if(addition==num)
      {
         return 1;
      }
      else
      {
         return 0;
      }
   }
   return 0;
}
int factor(int num)
{
int j=1;

int Maximumlimit=num/2;

do
{
if(num%j==0)
{
printf("%d ",j);
}

j++;
}while(j<=Maximumlimit);

return 0;
}

Pelle

I can't tell you what the code should look like, but it seems impossible for addition to ever be the same as num in the second if statement:


       if (num%j==0)
       {
           addition +=j;
       }
       if(addition==num)
       ...


Pelle
/Pelle

Danial

So are you suggesting i cange the second if statement

Pelle

Yes. I don't know enough about 'perfect numbers' to tell you how the code should be changed, but it looks like addition will always be different from num, so the function perfect will always return zero, which means you will never see any output in main...

Pelle
/Pelle

Garvan

Hi:

This is a version of your code with 2 bug fixes. However the algorithm you are using is not correct. As you will see it is producing false positives. Perhaps I missed a bug, or there is no accepted solution?

The changes I made may help you to make some progress.

Garvan

EDITED 2004-10-13

I think I found the last bug. The closing bracket of the "for" loop was in the wrong place. I updated the code below.

Garvan



//====================================


#include <stdio.h>
#include <math.h>

int perfect(int num);
int factor(int num);

int main()
{
   int i;
   i=2;
   while (i<10000) /*Display perfect numbers between 0 and 10000*/
      {
         if( perfect (i) )
         {
            printf("%d the factors of the number is\n",i); /*This is to display the perfect number and its factor */
            factor (i);             
            printf("\n");
         }
         i++;
      }
      return 0;
}

int perfect(int num)
{
   int addition=0;
   int j=1;
   int Maximumlimit;
   Maximumlimit= num/2;
   for(j=1; j<=Maximumlimit; j++)
   {
      if (num%j==0)
      {
         addition +=j;
      }
   }   
   if(addition==num)
   {
      return 1;
   }
   else
   {
      return 0;
   }  
}
int factor(int num)
{
   int j=1;
   
   int Maximumlimit=num/2;
   
   do
   {
      if(num%j==0)
      {
         printf("%d ",j);
      }
      
      j++;
   }while(j<=Maximumlimit);
   
   return 0;
}