Array ( [0] => [1] => questions [2] => Basic [3] => Perfect-Number )
Given a integer N. You have to find the given number is a perfect number or not(Perfect number is a number, sum of all its factors is equal to double of given number).
First line contains a integer N.
Print Yes if it is perfect number else No.
6
Yes
48
No
28
Yes
Login to see Discussion
Approach 1: Traversing upto n
Step-i) initialize temp variable to two times of the given number
Step-ii) initialize answer variable to 0
Step-iii) run a loop from i=1 to i<= given number
Step-iv) if remainder of number / i is zero add i to the answer variable
Step-v) check if both temp and answer variable holds the same value
Step-vi) if they are same return "Yes" else return "No"
Time Complexity: O(n)
Space Complexity: O(1)
Approach 2: Traversing upto square root of n
Step-i) initialize temp variable to two times of the given number
Step-ii) initialize answer variable to 0
Step-iii) run a loop from i=1 to i<= square root of given number
Step-iv) if remainder of number/i is zero add both i and quotient to the answer variable
Step-v) check if both temp and answer varible holds the same value
Step-vi) if they are same return "Yes" else return "No"
Time Complexity: O(sqrt(n))
Space Complexity: O(1)
Note :
Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.
Login to see Solution