A program to check whether a given number is a perfect number or not is discussed here. A perfect number is a number which is equal to the sum of its proper positive divisors.
For example, 6 is a perfect number.nThe divisors of 6 are 1, 2 and 3.n1 + 2 + 3 = 6.n
This problem can be solved in two different ways.
Method 1: Starting from 1 to (num – 1), find all the divisors of the number. If the sum of divisors of the number is equal to the number, return true.
Method 2: Starting from 1 to (num^1/2),find all the divisors of the number. If the sum of divisors of the number is equal to the number, return true.
The second method is much more efficient than the first method.
@@coding::1@@
Time complexity: O(n)
@@coding::2@@
Time complexity: O(n^1/2)
Recommended Programs