This is one of the popular number and series questions asked in coding interviews.
There are many students who are interested to become software developers so the best way to crack the SDE roles is to practice programming. This is one of the common basic questions that are asked in the Interviews in some service and product-based companies.
Now let’s back to the question.
You can also watch the video where I have explained this tutorial. Or keep continue reading.
If the sum of the factors of the given number is equal to the same number then it is known as a perfect number.
Example
6 is a perfect number.
Factors of 6 = 1, 2,3
Sum of Factor = Given number
1+2+3 = 6
6 = 6 (Therefore, 6 is a perfect number.)
Prerequisite
C++ Code
#include<stdio.h> int main() { int n, i, sum; printf("Enter a number"); scanf("%d",&n); for(i=1;i<n;i++) { if(n%i==0) { sum=sum+i; } } if(n==sum) printf("%d is a Perfect number",n) else printf("%d is not a Perfect number", n);
I have used for loop here. You can also implement this logic using a while or do-while loop.
Prerequisite
Python Code
n = int(input("Enter a number")) sum = 0 for i in range(1, n): if n%i == 0: sum += i if n == sum: print(f"{n} is a Perfect number") else: print(f"{n} is not a Perfect number");
As in C++ and Python, you can also implement the same program in Java.
Output
Testcase 1:
Enter a number 10
10 is not a Perfect number
Explanation:
Factors of 10 = 1, 2, 5
Sum of Factors 1+2+5 = 8
8 is not equal to 10. So, 10 is not a perfect number.
Testcase 2:
Enter a number 28 28 is a Perfect number
Explanation:
Factors of 28 = 1, 2, 4, 7, 14
Sum of Factors 1+2+4+7+14 = 28
28 = 28. So, 28 is a perfect number.
You can also write this program more programmatic way.
Problem statement: Write a function that returns True if the given number is a perfect number, else False.
def is_perfect(x): sum = 0 for i in range(1, x): if x%i == 0: sum += i if sum == x: return True else: return False out = is_perfect(6) print(out)
Output:
True
As we are traversing the for-loop from 1 to n-1, the complexity of this code is O(n)
. So, this algorithm takes linear time.
We are using one integer constant ‘sum’ to store the sum of the factors of the given number. It has constant space complexity.
This is similar to the above problem so in the above problem you were asked to check whether the given number is a perfect number or not but in this problem you have to print all the perfect numbers within the given range
#include<stdio.h> int main() { int start, end, n, i, sum; printf("Enter the starting number of the range"); scanf("%d",&start); printf("Enter the ending number of the range"); scanf("%d",&end); for(n=start;n<=end;n++) { sum=0; for(i=1;i<n;i++) { if(n%i==0) { sum=sum+i; } } if(n==sum) { printf("%d",n); } } }
start = int(input("Enter the starting number of the range")) end = int(input("Enter the ending number of the range")) for n in range(start, end+1): sum = 0 for i in range(1, n): if n%i == 0: sum += i #print(n, sum) if n == sum: print(n)
Output
Enter the starting number of the range 1 Enter the ending number of the range 1000 6 28 496
Note: There are only three perfect numbers in the range of 1 to 1000.
If you are asked in the coding interview you should follow the standard practice.
Problem statement: Write a function that prints all the perfect numbers in the range of 1 to 100.
Here we are utilizing the above function is_perfect()
to check if the given number is perfect or not.
def is_perfect(x): sum = 0 for i in range(1, x): if x%i == 0: sum += i if sum == x: return True else: return False def print_perfect_numbers(a, b): for i in range(a, b+1): if is_perfect(i): print(i) print_perfect_numbers(1, 100)
Output:
6 28
Interviewers can also ask you to count the perfect numbers in the given range.
The above Python program i.e., for range for perfect number is not correct. It is for prime number.
You’re not right saying that. This program is for perfect number.