Posted By

Scooter on 01/03/09


Tagged

math


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

benrudolph


Perfect Numbers


 / Published in: ASP
 

URL: http://reusablecode.blogspot.com/2009/01/perfect-numbers.html

Determine whether the given number is a perfect number.

  1. <%
  2. ' Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.
  3. '
  4. ' This work is licensed under the Creative Commons Attribution License. To view
  5. ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  6. ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  7. ' 94305, USA.
  8.  
  9. ' Determine whether the given number is a perfect number.
  10. function isPerfect(someNumber)
  11. dim i
  12. dim arrFactors
  13. arrFactors = Array()
  14.  
  15. ' Only positive integers can be perfect.
  16. if someNumber < 1 then
  17. isPerfect = false
  18. exit function
  19. end if
  20.  
  21. ' Calculate the factors for the given number.
  22. for i = 1 to someNumber
  23. if someNumber mod i = 0 then
  24. redim preserve arrFactors(UBound(arrFactors) + 1)
  25. arrFactors(UBound(arrFactors)) = i
  26. end if
  27. next
  28.  
  29. ' A perfect number is a number that is half the sum of all of its positive divisors (including itself).
  30. if someNumber = eval(join(arrFactors, " + ")) / 2 then
  31. isPerfect = true
  32. else
  33. isPerfect = false
  34. end if
  35. end function
  36. %>

Report this snippet  

You need to login to post a comment.