Skills101
← All coding problems

Perfect Number

easyPlacement Coding+10 XPTime limit 2s

A perfect number equals the sum of its proper divisors (all divisors except the number itself). 28 is perfect: 1 + 2 + 4 + 7 + 14 = 28.

Input:

  • line 1: q
  • next q lines: one integer n

Output: q lines — true if n is perfect, otherwise false.

Constraints: 1 ≤ q ≤ 100, 1 ≤ n ≤ 10⁹.

Think about it: looping to n is too slow for 10⁹. Divisors come in pairs (i, n / i), so loop only up to √n and add both — but add a square root only once. 1 is not perfect.

Sample input 1

3
28
12
6

Sample output 1

true
false
true

Sample input 2

2
1
496

Sample output 2

false
true

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.