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
arihanfvre6 days ago
def isPerfectNumber(self, N):
        # code here 
        
        sum = 0
        for i in range(1,N):
            if N%i == 0:
                sum += i
        
        if sum == N:
            return 1
        else:
            return 0
getting runtime error
+1
mayank180919992 months 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
mohdraza3 months 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
mayank20214 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
makkarbharat244 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;   }}
+3
arpitkatiyar091974 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;   }};
-5
satendrapal91115 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;           }
-4
varshakavi20019 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
Rachana1 year agodef 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 0Reply Open ExternallyShow 0 RepliesLoading...
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
-1
Sawi Sharma1 year agoPython3 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 0Reply Open ExternallyShow 0 RepliesLoading...
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
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.