A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly.
Given an integer n, return true if n is a perfect number, otherwise return false.
Solution
Let’s use a easy iteration try to get the answer.
Iterate from 1 to num, to check all it’s positive divisors and sum them together.
C# Solution
❌First try
public class Solution {
public bool CheckPerfectNumber(int num) {
if(num == 1)
{
return false;
}
int sum = 0;
for(int i =1; i<num;i++)
{
if(num%i ==0)
{
sum+=i;
}
}
return num == sum;
}
}
But, what we got? Time Limit Exceeded Error !!!!
⭐Solution1 – iterate to square root of num
If num has divisor, than it can be write as a * b
And when will we get the smallest difference between a and b ?
That will be num1/2
Why we need to know this, that is because if a divisor is smaller than num1/2 , we will find a divisor greater than num1/2 ➡ we can check the iterate to num1/2 instead of num
public class Solution {
public bool CheckPerfectNumber(int num) {
if(num == 1)
{
return false;
}
int sum = 1;
int max = (int)Math.Sqrt(num);
//int max = (int)Math.Pow(num,0.5); //you can choose which you want
for(int i =2; i<=max;i++)
{
if(num%i ==0)
{
sum+= i + num/i;
}
}
return num == sum;
}
}
Java Solution
Solution1 – iterate to square root of num
class Solution {
public boolean checkPerfectNumber(int num) {
if(num == 1)
{
return false;
}
int sum = 1;
int max = (int)Math.sqrt(num);
//int max = (int)Math.pow(num,0.5);
for(int i =2; i<=max;i++)
{
if(num%i ==0)
{
sum+= i + num/i;
}
}
return num == sum;
}
}
Python3 Solution
Solution1 – iterate to square root of num
class Solution:
def checkPerfectNumber(self, num: int) -> bool:
if num == 1:
return False
sum = 1;
max = math.ceil(math.sqrt(num))
#max = math.ceil(num**0.5)
for i in range(2,max):
if num%i ==0:
sum+= i + num/i
return num == sum
JavaScript Solution
Solution1 – iterate to square root of num
/**
* @param {number} num
* @return {boolean}
*/
var checkPerfectNumber = function(num) {
if(num == 1)
{
return false;
}
var sum = 1;
var max = parseInt(Math.sqrt(num));
//var max = parseInt(Math.pow(num,0.5));
for(let i =2; i<=max;i++)
{
if(num%i ==0)
{
sum+= i + num/i;
}
}
return num == sum;
};
Conclusion
🧡If my solution helps, that is my honor!
🧡You can support me by clicking some ad, Thanks a lot
✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know (either leave a comment or contact me by Messenger is ok!)
“If everything was perfect, you would never learn and you would never grow.”
ai_front = {"insertion_before":"BEFORE","insertion_after":"AFTER","insertion_prepend":"PREPEND CONTENT","insertion_append":"APPEND CONTENT","insertion_replace_content":"REPLACE CONTENT","insertion_replace_element":"REPLACE ELEMENT","visible":"VISIBLE","hidden":"HIDDEN","fallback":"FALLBACK","automatically_placed":"Automatically placed by AdSense Auto ads code","cancel":"Cancel","use":"Use","add":"Add","parent":"Parent","cancel_element_selection":"Cancel element selection","select_parent_element":"Select parent element","css_selector":"CSS selector","use_current_selector":"Use current selector","element":"ELEMENT","path":"PATH","selector":"SELECTOR"};