# My perfect number assignment...

• 11-15-2005
Ninestar
My perfect number assignment...
Hey, I've had to create a program that figures out the upper limits of perfect numbers, so here is my program.

Code:

```#include <stdio.h> #include <stdlib.h> int perfect(int x); int main(char *argv[],int argc) {     int x=0;     int y=0;     int z;     int a;         printf("Enter a number up to 10000 to discover the perfect numbers within the limit:\n\n");     scanf("%d", &y);     printf("\n");     /* The above code asks the user to input a limit and the assigns this number to the variable, "y" */             for (z=1;z<=y;z++)     /* This is a loop which runs the function "perfect" for each number in turn, up to the number inputted by the user */     {         a = perfect(z);         if(a==1)         {             a==1;             printf("%d is a perfect number\n\n",z);             /* The program then prints each perfect number up to this limit */                     }     }     system("PAUSE"); return 0;        } int perfect(int x) {     int y = 0;     int m = 0;     int p;         for(p=1;x>p;p++)     /* This will loop whist "x" is more than "p" */     {         m=x%p;         /* This piece of code uses the 'MOD' calculation to determine if there's a remainder */                 if(m==0)         /* "p" is added to "y" if there is no remainder */         {             y=y+p;         }     }     if(y==x)     /* "x" is perfect number if it is equal to "y" */     {         x=1;     }     else     {         x=0;     }         return x;     }```

Now, I used "int main(char *argv[],int argc)" as I read in the help file about the debugging values, but we have been taught to use void main(void) and so I feel like I'm somewhat cheating. Does anyone know how to improve this?
For the second part of my assignment I have to modify this program so that the program asks the user to input a number and the program should print the nearest perfect number on the screen, in short I haven't a clue where to start, help would be appreciated, thanks.
• 11-15-2005
Quantum1024
Quote:

Originally Posted by Ninestar
Now, I used "int main(char *argv[],int argc)" as I read in the help file about the debugging values, but we have been taught to use void main(void) and so I feel like I'm somewhat cheating. Does anyone know how to improve this?

setting the return type of main to void is never correct yu should always use either
int main(void)
or int main (char *argv[],int argc)
Take a look at this FAQ entry
• 11-15-2005
HAssan
Here is my code
Hello I did a very very similar assignment to this. Mine calculates the perfect numbers from 1-10000. Here is the code I hope it will help you.

Code:

``` #include <stdio.h> #include <math.h> int perfect(int); void printNumbers(); int main() {                 printf("The perfect number between 1 and 10000 plus their factors");         printNumbers();         return 0;    } int perfect(int num) { int addition=0; /*the variable that stores the addition of factors */   int Maxlimit=num/2; /*Condition for checking where the factors might be */   int c=1; /* counter for dividing */ do { if(num % c==0) /*if the reraimder is 0 the addition is updated*/ { addition+=c; }   c++; }while(c<=Maxlimit);   if(addition==num) /* If the addition is equal to the number then it is a perfect number*/ { return 1; } else { return 0; } } void printNumbers() {         int number, factor;     for(number =2; number<10000; number++)         {         if (perfect(number))         {             printf("\n %d This a perfect number, And its factors are:", number);                             for (factor=1; factor<number; factor++)             {                 if((number % factor) == 0)                 {                     printf(" %d", factor);                 }             }                     }     } }```
• 11-15-2005
Ninestar
Quote:

Originally Posted by Quantum1024
setting the return type of main to void is never correct yu should always use either
int main(void)
or int main (char *argv[],int argc)
Take a look at this FAQ entry

Oh, I've read through the FAQ and it makes sense, but my Italian teacher has always taught us to us void main(void)

Any reason for this?

• 11-15-2005
Thantos
Quote:

Any reason for this?
Because they don't know what they are doing.
• 11-15-2005
Harbinger
> Any reason for this?

Code:

```int main ( ) {   char *buff;   fflush(stdin);   gets(buff); }```
If they can't tell you at least 3 things wrong with this code, then find another tutor, you're not going to learn anything useful from them
• 11-15-2005
CBUK
Don't think there is any upper limit to perfect numbers, well none had been found when I produced a similar program a few years back, I found if you do some reading on the maths side of it you can gain serious speed increases by only investigating relivant numbers rather than such an exhaustive search.

I found it was a good project to learn a bit about distributed computing too. I used one PC to find likely perfect numbers and then distributed them to computers on my small home network to prove them. It was really quick when i moved it to the network at uni and it had 50+ clients.

You shouldn't bee too hard on lectures/tutors when it comes to programming in my experience many teach it without any background in it (they are really engineers mathematicians ect) and this is probably a big part of why very little software works and what does work only works when you play nicely with it.
• 11-15-2005
Rashakil Fol
Quote:

Originally Posted by CBUK
Don't think there is any upper limit to perfect numbers, well none had been found when I produced a similar program a few years back, I found if you do some reading on the maths side of it you can gain serious speed increases by only investigating relivant numbers rather than such an exhaustive search.

This is true. There are only 42 known perfect numbers, since they correspond to Mersenne primes, and it is known that there are only 39 even perfect numbers less than 2^26933835. Nobody knows if there are any odd perfect numbers. One of the simplest ways to write this program is to make an array of all the known perfect numbers that can be stored inside an unsigned int. Then no computations are needed at all, just comparisons with the upper bound.
• 11-16-2005
Ninestar
I know the first 8 perfect numbers. This part of my assignment is basically just wanting me to do what I have already done, so I'm not going to mess around with it now. I find the void main(void) function unreliable, yet when I confronted my tutor he merely told me not to take advice from these boards since many people don't know what they're talking about...

Since I find your style of code more reliable, I will continue to do so.

For part 2, I had to create a guess the number game, I found asking relevant questions hard, but anyway here it is, please tell me how it could be improved!

Code:

```#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) {     int number;     int b;     int c=0;     int z=50;     int fin=0;     int constant=0;     printf("Think of a number between -100 and 100...\n\nI will attempt to guess it. To answer the questions you must enter:\n\n1 for Higher\n2 for Lower\n3 for the Correct answer\n");     /* Welcome message for the program */         while(constant==0)     /* This loops the program, but the welcome message does not loop repeatedly as the variable "constant" doesn't change */         {         z=50;         number=0;         fin=0;         printf("\nThought of that number yet? - Ready when you are!\n\n");         /* This prepares for the first question */                 printf("Is your number Higher or Lower than 0? ");         scanf("%d", &b);         /* This asks the user the first question */         if(b==1)         {             number=50;         }         /* This set of code determines the reply to your answer. Higher then 0 and the question will be half way between 0 and 100 (50) */         if(b==2)         {             number=-50;         }         /* Should you answer lower then 0, the question will be half way between 0 and -100 (-50) */         if(b==3)         {             printf("Your number is 0");         }         if(b!=3)         {                         while(fin==c)             /* This while loop is used to run the code to ask the next question while the game is not over i.e. "fin" is not set to 1 */             {                 printf("Is your number Higher, Lower or Correct at %d? ", number);                 scanf("%d", &b);                 /* The code below checks to see if "z" divided by two leaves a remainer, if it does it adds one */                 /* to the result. In other words it makes sure if "z" cannot be divided by two without a remainder */                 /* it is rounded up */                                 if(z%2==1)                 {                     z=(z/2)+1;                 }                 else                 {                     z=z/2;                 }                                 // If the response to the question was 1 (Higher) then z is ADDED to "number"                                 if(b==1)                 {                     number=number+z;                 }                                 // If the response to the question was 2 (Lower) then z is SUBTRACTED from "number"                                 if(b==2)                 {                     number=number-z;                 }                                 // If the response to the question was 3 (Equal to) "number" is displayed on the screen                 // and "fin" is set to 1 which makes it drop out of the while loop                                 if(b==3)                 {                     printf("\nYour number is %d\n\n", number);                     fin = 1;                 }             }         }     }         system("PAUSE");          return 0; }```