CFLib.org – Common Function Library Project

IsPerfectNumber(number)

Last updated September 6, 2001

author

Rob Brooks-Bilson

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;
  }
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson