Divisors, sums and perfect numbers

The number of divisors and their sum follow from the prime factorization. Your program computes both, and classifies each number as perfect, abundant or deficient by comparing its proper divisors with itself.

What to build

A program that prints the divisor count, the divisor sum and the class of each number.

Requirements

  • Read t, then t numbers n.
  • For each n print D S C where D is the number of positive divisors of n, S is their sum (including 1 and n) and C is perfect if S − n equals n, abundant if S − n is greater than n, and deficient otherwise.

Input

The first line has t. Each of the next t lines has one number n.

Output

One line for each n.

Constraints

1 ≤ t ≤ 5; 1 ≤ n ≤ 10 to the power 10.

Example

Input:

1
12

Output:

6 28 abundant

Why it works

A divisor of n = p1^e1 × ... × pk^ek picks an exponent from 0 to e_i for each prime, so there are (e1+1)...(ek+1) divisors, and the sum of the divisors factors into a product of geometric sums, one for each prime. The same factorization is found by trial division up to the square root, which is fast enough even for ten digits.

Examples

Example 1 · Input
1
12
Expected output
6 28 abundant
Example 2 · Input
3
6
7
28
Expected output
4 12 perfect
2 8 deficient
6 56 perfect

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Factor n by trial division; for each prime power p^e the divisor count multiplies by e + 1 and the divisor sum by (p^(e+1) - 1) / (p - 1).

Hint 2

Compare the sum of the proper divisors, sigma minus n, with n: equal is perfect, bigger is abundant, smaller is deficient.

Hint 3

The input can be as large as 10 to the power 10, so factor with divisors up to the square root, not by looping to n.

Solution

Show one correct solution (try it yourself first)
t = int(input())
for _ in range(t):
    n = int(input())
    m = n
    count, total = 1, 1
    d = 2
    while d * d <= m:
        if m % d == 0:
            e = 0
            while m % d == 0:
                m //= d
                e += 1
            count *= e + 1
            total *= (d ** (e + 1) - 1) // (d - 1)
        d += 1
    if m > 1:
        count *= 2
        total *= m + 1
    proper = total - n
    kind = "perfect" if proper == n else "abundant" if proper > n else "deficient"
    print(count, total, kind)

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Divisibility, primes and the gcd →