home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / EXAMPLES / PRIME.LF < prev    next >
Text File  |  1996-06-04  |  1KB  |  45 lines

  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. % Copyright 1992 Digital Equipment Corporation
  4. % All Rights Reserved
  5. %
  6. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  7.  
  8. module("prime") ?
  9. public(primes_to,prime) ? 
  10.  
  11. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  12.  
  13.  
  14. % A prime number is a positive integer whose number of proper factors is
  15. % exactly one.
  16.  
  17. prime :=  P:posint | number_of_factors(P) = one.
  18.  
  19. posint := I:int | I>0.
  20.  
  21. number_of_factors(N) -> cond (N < 2, {}, factors_from(N,2)).
  22.  
  23. factors_from(N:int,P:int) -> 
  24.     cond(P*P > N,
  25.              one,
  26.              cond(R:(N/P) =:= floor(R),
  27.                   many,
  28.                   factors_from(N,P + 1) 
  29.              ) 
  30.         ).
  31.  
  32. primes_to(N:int) :-
  33.     write(posint_stream_to(N) & prime), 
  34.     nl,
  35.     fail.
  36.  
  37. posint_stream_to(N:int) ->
  38.     cond(N < 1,
  39.              {},
  40.              {1;1 + posint_stream_to(N-1)}
  41.         ).
  42.  
  43. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  44.  
  45.