0
votes

The assignment is :

Write a program that calculates the sum of the divisors of a number from input.

A number is considered perfect if the sum of it's divisiors equal the number (ex: 6 = 1+2+3 ;28 = 1 + 2 + 4 + 7 +14).

Another definition: a perfect number is a number that is half the sum of all of its positive divisors (including itself)

Generate the first k perfect numbers (k<150).

The main problem with this is that it's confusing the two asking points don't really relate.

In this program i calculated the sum of divisors of an entered number, but i don't know how to relate it with the second point (Generate the first k perfect numbers (k<150)).

#include <stdio.h>
#include <stdlib.h>


main()
{
int x,i,y,div,suma,k;
printf("Introduceti numarul\n");          \\enter the number
scanf("%d",&x);
suma=0;                                   \\sum is 0
for(i=1;i<=x;i++)
{

if(x%i==0)
suma=suma+i;                \\sum=sum+i;
}
printf("Suma divizorilor naturali este: %d\n",suma);      \\the sum of the divisors is

for(k=1;k<150;k++)               \\ bad part
{
if (x==suma)
printf("%d",k);
}
}
3

3 Answers

1
votes

Suppose you have a function which can tell whether a given integer is perfect or not:

int isPerfect(int);

(function body not shown)

Now your main program will look like:

int candidate;
int perfectNumbers;

for(candidate = 1, perfectNumbers = 0; perfectNumbers < 150; candidate++) {
  if (isPerfect(candidate)) {
    printf("Number %d is perfect\n", candidate);
    perfectNumbers++;
  }
}

EDIT For the same program without functions:

int candidate;
int perfectNumbers;

for(candidate = 1, perfectNumbers = 0; perfectNumbers < 150; candidate++) {

  [... here your algorithm to compute the sum of the divisors of "candidate" ...]

  if (candidate*2 == sum_of_divisors) {
    printf("Number %d is perfect\n", candidate);
    perfectNumbers++;
  }
}

EDIT2: Just a note on perfect numbers

As noted in the comments section below, perfect numbers are very rare, only 48th of them are known as of 2014. The sequence (A000396) also grows very fast: using 64-bit integers you'll be able to compute up to the 8th perfect number (which happen to be 2,305,843,008,139,952,128). In this case the variable candidate will wrap around and start "finding" "new" perfect numbers from the beginning (until 150 of them are found: actually 19 repetitions of the only 8 findable in 64-bit integers). Note though that your algorithm must not choke on a candidate equals to 0 or to negative numbers (only to 0 if you declare candidate as unsigned int).

0
votes

I am interpreting the question to mean generate all numbers under 150 that could are perfect numbers.

Therefore, if your program works for calculating perfect numbers, you keep calculating them until the starting number is >= 150.

Hope that makes sense.

0
votes

Well, here's my solution ..

First, you have to make a reliable way of getting divisors.
Here's a function I made for that:

size_t
getdivisors(num, divisors)
    long long num;
    long long *divisors;
{
    size_t divs = 0;
    for(long long i = num; i > 0; --i)
        if (num%i == 0)
            divisors[divs++] = i;
    return divs;
}

Second, you need to check if the number's divisors match the perfect number's divisors properties (the sum of them is half the number).
Here's a second function for that:

bool
isperfect(num)
    long long num;
{
    long long divisors[num/2+1];
    size_t divs = getdivisors(num, divisors);
    if (divs == 0)
        return false;
    long long n = 0;
    for(int i = 1; i < divs; ++i)
        n += divisors[i];
    return (n == num);
}

Now, from your question, I think you need to print all perfect numbers less than 150, right ?
See this:

int
main(argc, argv)
    int argc;
    char ** argv;
{
    for(int i = 1; i < 150; ++i)
        if (isperfect(i))
            printf("%d is perfect.\n", i);
    return 0;
}

I hope that answers your question ..