NO

Author Topic: Perfect numbers  (Read 4803 times)

Danial

  • Guest
Perfect numbers
« on: October 10, 2004, 03:10:20 PM »
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;
}

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Perfect numbers
« Reply #1 on: October 10, 2004, 04:17:33 PM »
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:

Code: [Select]

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


Pelle
/Pelle

Danial

  • Guest
If statement
« Reply #2 on: October 10, 2004, 10:18:58 PM »
So are you suggesting i cange the second if statement

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Perfect numbers
« Reply #3 on: October 11, 2004, 12:35:12 AM »
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

  • Guest
(Not so) Perfect numbers
« Reply #4 on: October 12, 2004, 03:00:23 PM »
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;
}