Perfect Numbers

Lab C Intermediate 20 points

Read n (≤ 10000) and print all perfect numbers ≤ n (numbers equal to the sum of their proper divisors) on one line, or none.

Input

One integer.

Output

One line.

Example

Input

30

Output

6 28
Sample input available Sample output available
Editor
C

Hints

Divisors come in pairs d and x/d - only loop to the square root.

Solution explanation

Pairing divisors makes the inner loop much shorter.

Show solution
#include <stdio.h>

int main() {
    int n, found = 0;
    scanf("%d", &n);
    for (int x = 2; x <= n; x++) {
        int sum = 1;
        for (int d = 2; d * d <= x; d++) {
            if (x % d == 0) {
                sum += d;
                if (d != x / d) sum += x / d;
            }
        }
        if (sum == x) {
            if (found) printf(" ");
            printf("%d", x);
            found = 1;
        }
    }
    printf(found ? "\n" : "none\n");
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD