Write a function which says whether a number is perfect.


A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.

Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).



Note:   The faster   Lucas-Lehmer test   is used to find primes of the form    2n-1,   all known perfect numbers can be derived from these primes
using the formula    (2n - 1) × 2n - 1.

It is not known if there are any odd perfect numbers (any that exist are larger than 102000).

The number of   known   perfect numbers is   51   (as of December, 2018),   and the largest known perfect number contains  49,724,095  decimal digits.



See also

   Rational Arithmetic
   Perfect numbers on OEIS
   Odd Perfect showing the current status of bounds on odd perfect numbers.


 
$lines
 
rem - return p mod q 
function mod(p, q = integer) = integer
end = p - q * (p/q)
 
rem - return true if n is perfect, otherwise false
function isperfect(n = integer) = integer
  var sum, f1, f2 = integer
  sum = 1
  f1 = 2
  while (f1 * f1) <= n do
    begin
      if mod(n, f1) = 0 then
        begin
          sum = sum + f1
          f2 = n / f1
          if f2 > f1 then sum = sum + f2
        end
      f1 = f1 + 1
    end
end = (sum = n)
 
rem - exercise the function
 
var k, found = integer
 
print "Searching up to"; search_limit; " for perfect numbers ..."
found = 0
for k = 2 to search_limit
  if isperfect(k) then
     begin
        print k
        found = found + 1
     end
next k
print found; " were found"
 
end
 