Want to know what is perfect number ? Here is the detailed description about perfect number and how to implement it in Linux.
Table of Contents
What Is Perfect Number?
A perfect number is a positive number which is equal to its divisor’s multiplication and addition such as you have a number (6=1+2+3 and 1*2*3=6) so in both cases, you are getting same number i.e 6 so 6 is the perfect number
. Perfect number in Linux can be check using the following program
Example Of Perfect Number In Linux
(i.e. 6=2+3 is not a perfect number where 2,3 are factors of 6).
Perfect Number In Linux
echo \"Enter a number\"
read num
i=1
ans=0
data=`expr $num / 2`
echo \"Data $data\"while [ $i -le `expr $num / 2` ]
do
echo \"i: $i\"
if [ `expr $num % $i` -eq 0 ]
then
ans=`expr $ans + $i`
fi
echo \"ans $ans\"
i=`expr $i + 1`
done
if [ $num -eq $ans ]
then
echo \"$num is perfect\"
else
echo \"$num is not perfect\"
fi
Code Explanation of Perfect Number in Linux
This code is a simple script that checks whether a given number is a “perfect number” or not. Let me explain it step by step in simple words:
This code is a simple script that checks whether a given number is a “perfect number” or not. Let me explain it step by step in simple words:
- First script displaying a message, “Enter a number,” prompting the user to input a number.
- The user’s input is read and stored in a variable of “num.”
- Two variables, “i” and “ans,” are initialized to 1 and 0. “i” will be used as a counter, and “ans” will be used to accumulate the sum of the proper divisors of the input number.
- The code will calculates a value called “data,” which is half of the input number (num / 2). This value will be for optimizing the loop.
- Now script enters a loop that will starts from “i” equal to 1 up to “data” (half of the input number).
- Inside the loop, the script displays the current value of “i” for debugging purposes.
- It checks if “i” is a divisor of the input number (num) by calculating the remainder of dividing num by i (num % i). If the remainder is 0, it means “i” is a divisor of “num.”
- If “i” is a divisor, its value is added to the “ans” variable, which keeps track of the sum of divisors found.
- The script then displays the current value of “ans” for debugging purposes.
- The value of “i” counter is incremented by 1 for the next iteration.
- The loop continues until all values of “i” from 1 to “data” have been checked.
- After the loop, the script checks or compare if the “ans” variable is equal to the input number (num).
- If “ans” and “num” are equal, it means it is a perfect number.
- If “ans” and “num” are not equal, it means the input number is not a perfect number.
- The script then displays the result, stating whether the input number is perfect or not.
Output of finding perfect number in linux
Application of Perfect Number
Now you got what is perfect number , lets understand its application. Perfect numbers can be found in various areas of mathematics and computer science.
- Analysis of Algorithms: It is used in the analysis of algorithms to estimate their efficiency and time complexity
- Number Theory: Studying the integer and their properties
- Properties of Prime Numbers: perfect number is formed by multiplying a power of 2 by a Mersenne prime, which is a prime number of the form 2^n – 1. Therefore, studying perfect numbers can provide insights into the properties of prime numbers.