How to get a result type n1 + N2 + n... = x?

Asked

Viewed 91 times

2

I am learning programming in C online and wrote this code to find the perfect numbers in a given range, but I would like to show the result with the sum of the divisors, for example :

1 + 2 + 3 = 6 PERFECT !!!
1 + 2 + 4 + 7 + 14 = 28 PERFECT !!!

My code is like this :

#include <stdio.h>

int main(int argc, const char * argv[]) {

    int first_interval;
    int last_interval;
    int number;
    int divider;
    int sum = 0;

    printf ("Enter first and last interval`s number\n");
    scanf("%d %d", &first_interval, &last_interval);

    for (number = first_interval; number >= first_interval && number <= last_interval; number++ ) {
        for (divider = 1; divider < number; divider++) {
            if (number % divider == 0) {
                sum = sum + divider;

            }

        }

        if (sum == number) {
            printf("%d = %d PERFECT !!!\n", number, sum);
        }

        sum = 0;

    }

    return 0;
}

How can I get the desired result, does anyone have any idea?

1 answer

2

A very simple way to do this is to store the number dividers in an auxiliary vector and, if perfect, display them. The main limitation is that the number of divisors will be limited by the size of the vector.

#include <stdio.h>

int main(int argc, const char *argv[])
{

  int first_interval;
  int last_interval;
  int number;
  int divider;
  int sum = 0;

  // Define um vetor para armazer os divisores:
  int dividers[10] = {0};

  // Variável de controle para os divisores:
  int i = 0;

  printf("Enter first and last interval`s number\n");
  scanf("%d %d", &first_interval, &last_interval);

  for (number = first_interval; number >= first_interval && number <= last_interval; number++ )
  { 
    for (divider = 1; divider < number; divider++)
    {
      if (number % divider == 0)
      {
        sum = sum + divider;

        // Encontrou um divisor, então armazena-o:
        dividers[i++] = divider;
      }
    }

    if (sum == number)
    {
      // Número é perfeito, então exibe os divisores:
      for (int j = 0; j < i-1; j++) printf("%d + ", dividers[j]);
      printf("%d = ", dividers[i-1]);

      printf("%d PERFECT !!!\n", number);
    }

    // Reinicia a variável de controle:
    i = 0;
    sum = 0;
  }

  return 0;
}

See working on Ideone for the range between 1 and 500, the expected result is 6, 28 and 496.

Number 496 already has 9 splitters, so that’s as far as the program can go. The next number would be 8128, which has 14 divisors (more than the vector size).

It is possible to circumvent this limitation using dynamic memory allocation, but this is a future challenge for you, since (apparently) you are starting your C studies now.

  • Thank you Anderson, I have not yet arrived in vectors, but I will already search how to use them (would be array in English ? ).

  • Yes, it would be the equivalent of array in English.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.