Perfect integer function
1. Definition
A perfect number is a positive integer equal to the sum of
its proper positive divisors (excluding itself); or the half of the sum
of all of its positive divisors (including itself).
Example:
--------
Propres divisors of 6: 1,2,3. Their somme = 1+2+3 = 6.
Divisors of 6: 1,2,3,6. Their half somme = (1+2+3+6)/2 = 6.
2. Example in C language:
#include <stdio.h>
#include <math.h>
enum BOOLEAN { false, true };
//Test is it perfect ?
int perfect(int number)
{
int n;
int sum =0;
for ( n = 1 ; n <= number-1 ; n++ )
{
if ( number% n == 0 )
{
sum += n;
}
}
if (sum == number)
{
return true;
}
else
{
return false;
}
}
//Displying ..
int display_perfect(int number)
{
int m = number;
if (perfect(m))
{
printf("\n The number %d is perfect. \n", number);
}
else
{
printf("\n The number %d is not perfect. \n", number);
}
return 0;
}
int main()
{
int number;
printf("\n Enter an integer ?: --> ");
scanf("%d", &number);
int m = number;
display_perfect(m);
//Perfect positive integers betwenn two values ..
int a,b;
int count=0;//To count them:
printf("\n Enter two integer limits : --> \n");
scanf(" %d %d", &a,&b);
printf("\n The perfect numbers between %d and %d are: \n",a,b);
int n;
for ( n = a ; n <= b ; n++ )
{
if ( perfect(n) )
{
count += 1;
printf("\n %d) %d \n",count,n);
}
}
if (count ==0)
{
printf("\n No perfect number between %d and %d \n", a,b);
}
printf("\n");
return 0;
}