Initializing help system before first use

Perfect numbers


Type: Programming
Rating: 2 (easy-medium)
Description: A perfect number is defined as a number for which the sum of all the divisors adds up to the number itself. This examples calculates all perfect numbers up to a given upper bound.
  • nested 'forall' loops
  • use of 'mod'
File(s): perfect.mos


perfect.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file perfect.mos 
   ```````````````` 
   Using the 'forall' loop.
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2001
*******************************************************!)

model Perfect

 parameters
  LIMIT=100
 end-parameters

 writeln("Perfect numbers between 1 and ", LIMIT, ":")

 forall(p in 1..LIMIT) do
   sumd:=1
   forall(d in 2..p-1)
     if p mod d = 0 then          ! Mosel's built-in mod operator
       sumd+=d                    ! The same as sum:= sum + d
     end-if
   if p=sumd then 
     writeln(p) 
   end-if 
 end-do

end-model

© 2001-2025 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.