#include <iostream>
using namespace std;
int main ()
{
int n = 28, sum = 0;
for(int i = 1; i < n; i++){
if(n % i == 0)
sum = sum + i;
}
if(sum == n)
cout << n << " is a perfect number";
else
cout << n << " is not a perfect number";
}
// Time complexity: O(N)
// Space complexity: O(1)
// Time complexity: O(N)
// Space complexity: O(1)
#include <iostream>
using namespace std;
int main ()
{
int num = 28, sum = 0;
int i = 1;
while(i < num)
{
// check if i is a divisor
if(num % i == 0)
sum = sum + i;
i++;
}
if(sum == num)
cout << num << " is a perfect number";
else
cout << num << " is not a perfect number";
return 0;
}
Output
28 is a perfect number
Method 3
If we iterate b/w [1, num/2]
All the divisors of the number can be found in this range
// Time complexity: O(N)
// Space complexity: O(1)
#include <iostream>
using namespace std;
int main ()
{
int num = 28, sum = 0;
int i = 1;
// If we iterate b/w [1, num/2]
// all the divisors of the number can be found in this range
// example : for 28: divsors {1, 2, 4, 7, 14}
// all are before num/2 i.e. 14
while(i <= num/2)
{
// checking if i is a divisor
if(num % i == 0)
sum = sum + i;
i++;
}
if(sum == num)
cout << num << " is a perfect number";
else
cout << num << " is not a perfect number";
return 0;
}
Output
28 is a perfect number
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
// Time complexity: O(N)
// Space complexity: O(1)
#include <iostream>
using namespace std;
bool checkPerfectNum(int n)
{
int sum = 0;
for (int i = 1; i <= n/2; i++){
if (n % i == 0)
sum = sum + i;
}
if (sum == n)
return true;
return false;
}
int main ()
{
int num = 28;
if(checkPerfectNum(num))
cout << num << " is a perfect number";
else
cout << num << " is not a perfect number";
return 0;
}
// Time complexity: O(N)
// Space complexity: O(1)
// Auxiliary Space complexity: O(N) due to function call stack
#include <iostream>
using namespace std;
static int sum = 0;
int getSumDivisors(long num, int i)
{
// since, all factors can be found will num/2
// we will only check in this range
if(i <= num/2)
{
if(num % i ==0)
sum = sum + i;
i++;
// recursively call isPerfect
getSumDivisors(num, i);
}
//returns the sum
// when i becomes > num/2
return sum;
}
int main ()
{
int num = 28;
if(getSumDivisors(num, 1) == num)
cout << num << " is a perfect number";
else
cout << num << " is not a perfect number";
return 0;
}
Output
28 is a perfect number
Method 6
This method uses observations that all factors come in pairs.
All Factors come in pairsFor n = a * b (For each a there exists a unique b)
#include <iostream>
#include <math.h>
using namespace std;
int getDivisorsSum(int n)
{
int sum = 0;
for(int i = 1; i < sqrt(n); i++)
{
if (n % i == 0)
{
// For n : (1, n) will always be pair of divisor
// acc to def., we must ignore adding num itself as divisor
// when calculating for perfect number
if(i == 1)
sum = sum + i;
// Example For 100 (10,10) will be one pair
// But, we should add value to the sum just once
else if(i == n/i)
sum = sum + i;
// add both pairs as divisors
// For any divisor i, n/i will also be a divisor
else
sum = sum + i + n/i;
}
}
return sum;
}
int main ()
{
int n = 28, sum = 0;
if(n == getDivisorsSum(n))
cout << n << " is a perfect number";
else
cout << n << " is not a perfect number";
}
// Time complexity: O(sqrt(N))
// Space complexity: O(1)
#include
using namespace std;
int main(int argc, char const *argv[])
{
int n;
cout << "enter the number" <> n;
int ans = 0;
for (int i = 1; i < n; i++)
{
if (n % i == 0)
{
ans = ans + i;
}
}
if (n == ans)
{
cout << n << " is a perfect number " << endl;
}
else
cout << n << " is not a perfect number " << endl;
#include
using namespace std;
int main(int argc, char const *argv[])
{
int n;
cout << "enter the number" <> n;
int ans = 0;
for (int i = 1; i < n; i++)
{
if (n % i == 0)
{
ans = ans + i;
}
}
if (n == ans)
{
cout << n << " is a perfect number " << endl;
}
else
cout << n << " is not a perfect number " << endl;
return 0;
}