CFLib.org – Common Function Library Project

IsPerfectNumber(number)

Last updated September 06, 2001

Version: 1 | Requires: CF5 | Library: MathLib

Description:
Returns True if the specified number is a perfect number. A perfect number is an integer greater than zero whose factors less than the number all add up to the number (i.e. 6 is a perfect number 3+2+1=6).

Return Values:
Returns a Boolean value.

Example:

``````<CFOUTPUT>
Is 6 a perfect number? #YesNoFormat(IsPerfectNumber(6))#<BR>
Is 12 a perfect number? #YesNoFormat(IsPerfectNumber(12))#<BR>
</CFOUTPUT>
``````

Parameters:

Name Description Required
number Integer greater than zero to test. Yes

Full UDF Source:

``````/**
* Returns True if the specified number is a perfect number.
*
* @param number      Integer greater than zero to test.
* @return Returns a Boolean value.
* @author Rob Brooks-Bilson (rbils@amkor.com)
* @version 1, September 6, 2001
*/
function IsPerfectNumber(number)
{
Var i=0;
Var factors = "";
Var factorArray=0;
Var sum=0;
if (number lt 1){
return False;
break;
}
for (i=1; i LTE number/2; i=i+1) {
if (Int(number/i) EQ number/i) {
factors = ListAppend(Factors, i);
}
}
factorArray=ListToArray(factors);
sum=ArraySum(factorArray);
if (sum eq number){
return True;
}
else{
return False;
}
}
``````

Search CFLib.org

QueryDeleteRows
November 04, 2017

May 11, 2016