A perfect number is a number if it is equal to the sum of its factors,that is original number is equal to sum of all its factors excluding the number itself. We have already discuss how to check if a number is perfect or not in this article. In this article we will discuss about how to do the same in PHP.
Input : 6 Output : Perfect Number Explanation: factors of 6 are 1, 2, 3, 6 sum of its factors (excluding the number itself) = 1 + 2 + 3 = 6 Input : 24 Output : Not Perfect Number Explanation : factors of 24 are 1,2,3,4,6,8,12,24 sum of its factors(excluding the number itself) = 1 + 2 + 3 + 4 + 6 + 8 + 12 = 36
The idea to this is we will traverse through each number in the range [1,N) and check if it is a factor of the given number N. If it is a factor, we will add this number to a variable $sum. At the end is the variable $sum is equal to the original number then the given number is a Perfect Number.
Below is the implementation of above idea in PHP:
- Check whether the number can be made perfect square after adding 1
- Perfect Square factors of a Number
- Smallest and Largest N-digit perfect cubes
- PHP | Check if a number is armstrong number
- PHP | check if a number is Even or Odd
- PHP | Check if a number is prime
- PHP | Palindrome Check
- PHP to check substring in a string
- PHP program to check for Anagram
- PHP | Check if all characters are lower case
- PHP | Program to check a string is a rotation of another string
- PHP | ctype_alnum() (Check for Alphanumeric)
- How to check an array is associative or sequential in PHP?
- How to check whether an array is empty using PHP?
- Why to check both isset() and !empty() function in PHP ?
- How to check form submission in PHP ?
- How to check if URL contain certain string using PHP?
- PHP | Check if a variable is a function
- PHP | Check if two arrays contain same elements
- How to check the existence of URL in PHP?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai