1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include <stdio.h>
#define N 100
int factorsum( int number ); void PrintPN( int m, int n ); int main() { int i, m, n;
scanf("%d %d", &m, &n); if ( factorsum(m) == m ) printf("%d is a perfect number\n", m); if ( factorsum(n) == n ) printf("%d is a perfect number\n", n); PrintPN(m, n);
return 0; } int factorsum( int number ){ int a[N],i,j=0,n=0,sum=0; if(number == 1) return 1; for(i=1;i<number;i++) if(number%i==0){ a[j++]=i; } for(i=0;i<j;i++) sum+=a[i]; if(sum == number) return 1; return 0; } void PrintPN( int m, int n ){ int i,j,k=0,l,a[N]; if(m==1){ printf("%d = %d\n",m , m); m+=1; } for(i=m;i<=n;i++){ if(factorsum(i)==1){ for(j=1;j<i;j++){ if(i%j==0){ a[k++]=j; } } printf("%d = ", i); for(l=0;l<k-1;l++) printf("%d + ", a[l]); printf("%d\n", a[l]); k=0; } } } }
|