CCC 1996 Stage1: Problem A: Deficient, Perfect, and Abundan
Author 
Message 
Tony

Posted: Fri Jan 03, 2003 7:05 pm Post subject: CCC 1996 Stage1: Problem A: Deficient, Perfect, and Abundan 


Write a program that repeatedly reads a positive integer, determines if the integer is deficient, perfect, or abundant, and outputs the number along with its classification.
A positive integer, n, is said to be perfect if the sum of its proper divisors equals the number itself. (Proper divisors include 1 but not the number itself.) If this sum is less that n, the number is deficient, and if the sum is greater than n, the number is abundant.
The input starts with the number of integers that follow. For each of the following integers, your program should output the classification, as given below. You may assume that the input integers are greater than 1 and less than 32500.
Sample input
3
4
6
12

Sample output
4 is a deficient number.
6 is a perfect number.
12 is an abundant number.
Tony's Comment
Proper divisor is a number by which a number can be divided (excluding itself but including 1)
so for 4
1+2 = 3  3<4 so 4 is deficient
for 6
1+2+3 = 6  6=6 so 6 is perfect
for 12
1+2+3+4+6 = 16  16>12 so 12 is abundant

Tony's programming blog. DWITE  a programming contest. 




Sponsor Sponsor



Tony

Posted: Fri Jan 03, 2003 7:08 pm Post subject: official solution 


here's the official solution
C Source Code
# include <stdio.h>
FILE * infp;
FILE * outfp;
int t, /* number of test cases */
i, /* a counter */
n, /* a number to be classified */
sum; /* sum of divisors of n */
main()
{
infp = fopen( "dpa.in", "r");
outfp = fopen( "dpa.out", "w");
fscanf(infp, "%d\n", &t);
while (t>0) {
fscanf(infp, "%d\n", &n);
sum=0;
for (i=1; i<=n/2; i++ ) {
if (n%i==0) sum=sum+i;
}
if (sum < n) fprintf(outfp,"%d is a deficient number.\n",n);
else if (sum ==n) fprintf(outfp,"%d is a perfect number.\n",n);
else /* sum > n */ fprintf(outfp,"%d is an abundant number.\n",n);
t; fprintf(outfp,"\n");
}
}

Tony's programming blog. DWITE  a programming contest. 




Tony

Posted: Fri Jan 03, 2003 7:23 pm Post subject: unofficial solution 


since its a Stage1 problem, using turing is allowed. Here's a solution I came up with using Turing.
Basically its the same solution, but transfered into turing syntax. See file attached.
Note:
if you're going to run the solution, make sure to create a file called data.in in same folder and fill it with atleast 1 integer number. Program output will be saved in file called data.out which you don't have to create.
Description: 

Download 
Filename: 
1996 s1 p1.t 
Filesize: 
586 Bytes 
Downloaded: 
417 Time(s) 

Tony's programming blog. DWITE  a programming contest. 





