Perfect Numbers | Practice | GeeksforGeeks

Given a number N, check if a number is perfect or not. A number is said to be perfect if sum of all its factors excluding the number itself is equal to the number.
 
Example 1:
Input:
N = 6
Output:
1 
Explanation:
Factors of 6 are 1, 2, 3 and 6.
Excluding 6 their sum is 6 which
is equal to N itself. So, it's a
Perfect Number.
Example 2:
Input:
N = 10
Output:
0
Explanation:
Factors of 10 are 1, 2, 5 and 10.
Excluding 10 their sum is 8 which
is not equal to N itself. So, it's
not a Perfect Number.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function isPerfectNumber() which takes an Integer N as input and returns 1 if N is a Perfect number else returns 0.
 
Expected Time Complexity: O(sqrt(N))
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 1012
0
mayank180919991 week ago
  int isPerfectNumber(long long N) {
        int sum=1;
        if(N==1){
            return 0;
        }
       for(int i=2;i<=sqrt(N);i++){
           if(N%i==0){
               sum+=i;
               sum+=N/i;
           }
       }
      if(sum==N){
          return 1;
      }
     return 0;
    }
0
mohdraza2 weeks ago
class Solution {   static int isPerfectNumber(Long N) {      if(ispf(N)){          return 1;      }      else      {          return 0;      }   }   static boolean ispf(Long n){      {             if (n == 1)           return false;                      long sum = 1;             for (long i = 2; i * i <=n ; i++) {                      if (n % i == 0) {                              sum += i + (n / i);           }       }                     if (sum == n)                      return true;        return false;   }}};
0
mayank20212 months ago
C++   0.3/1.2int isPerfectNumber(long long N) {              if (N==1) return 0;       int sum=1;       for(int i=2; i<=sqrt(N); i++)       {                      if(N%i==0)            sum=sum+i+N/i;                             }              if(sum==N) return 1;       return 0;                 }
0
makkarbharat242 months ago
Why am I getting run time error for this code?
class Solution {   static int isPerfectNumber(Long N){
       int sum=1;       for(int i=2;i<=N/2;i++)       {           if(N%i==0)               {sum+=i;}       }       return sum==N?1:0;   }}
+2
arpitkatiyar091972 months ago
CORRECT JAVA SOLUTION 
 
class Solution {   static int isPerfectNumber(Long n) {       // code here      long sum = 1;   // Find all divisors and add them   for (int i = 2; i<=Math.sqrt(n); i++)     {         if (n%i==0)       {           if(i*i!=n)               sum = sum + i + n/i;           else               sum=sum+i;       }   }   // If sum of divisors is equal to   // n, then n is a perfect number   if (sum == n && n != 1)       return 1;   return 0;   }};
-2
satendrapal91112 months ago
 long long sum = 0,i;       for( i=1; i<=N/2; i++)       {           if(N % i == 0 )           {                sum = sum + i;           }              }           if(sum == N)           {               return 1;           }           else           {               return 0;           }
-5
varshakavi20017 months ago
long sum=1;       for (long i = 2; i * i <= N; i++)   {       if (N % i==0)       {           if(i * i != N)               sum = sum + i + N / i;           else               sum = sum + i;       }   }        // If sum of divisors is equal to number       // Then number is a perfect number       if (sum == N && N != 1)       return 1;      return 0;
0
Rachana11 months ago
Rachana
def perfect(n):    a=[]    d=0    for i in range(1,n):        if(n%i==0):            a.append(i)    for j in range(len(a)):        d+=a[j]    if(d==n):        return 1    return 0
0
Sawi Sharma1 year ago
Sawi Sharma
Python3 solusann:
def isPerfectNumber(self, N):        # code here         import math        if N==1:            return 0
 self.total=1        for i in range(2,int(math.sqrt(N))+1):            if N%i==0 and N!=i: self.total+=i self.total+=int(N/i)
        if self.total==N:            return 1        else:            return 0
+2
Rutvik Rana1 year ago
Rutvik Rana
Correct Solution As I Guess Should Be:-
    int isPerfectNumber(long long N) {        if(N==1)return 0;        long long sum = 1;        for(long long i=2;i<=sqrt(N);i++){            if(N/i==i){sum+=i;}            else if(N%i==0){sum+=(i+(N/i));}        }        return sum==N;    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.