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

  1.  
  2. % Very large factorials
  3.  
  4. % Numbers are stored as lists with four decimal digits per element.
  5.  
  6. % Fact: bigmul is pred, mem.recovery with failure.
  7. % Fact2: bigmul is pred, mem recovery only with GC.
  8.  
  9. % Fact3: bigmul is simplest function, mem recovery only with GC.
  10. % Fact4: bigmul is function w/ such_that, mem recover only with GC.
  11.  
  12. % Bigmul as a predicate recovers all mem on GC.
  13. % Bigmul as a function recovers all memory ONLY if it's written with
  14. % such_that: fact3 has a memory leak, but fact4 does not!
  15.  
  16. main :-
  17.     write('Enter N: '),
  18.     read(N:int),
  19.     reverse( factorial(N), [Head|Tail]),
  20.     write('Factorial = '),
  21.     write(Head),
  22.     write_fact(Tail),
  23.     nl.
  24.  
  25. main2 :-
  26.     write('Enter N: '),
  27.     read(N:int),
  28.     reverse( factorial2(N), [Head|Tail]),
  29.     write('Factorial = '),
  30.     write(Head),
  31.     write_fact(Tail),
  32.     nl.
  33.  
  34. main3 :-
  35.     write('Enter N: '),
  36.     read(N:int),
  37.     reverse( factorial3(N), [Head|Tail]),
  38.     write('Factorial = '),
  39.     write(Head),
  40.     write_fact(Tail),
  41.     nl.
  42.  
  43. main4 :-
  44.     write('Enter N: '),
  45.     read(N:int),
  46.     reverse( factorial4(N), [Head|Tail]),
  47.     write('Factorial = '),
  48.     write(Head),
  49.     write_fact(Tail),
  50.     nl.
  51.  
  52. % Factorial function:
  53.  
  54. factorial(N) -> F | N>=0, F = fact_loop(N).
  55.  
  56. fact_loop(0)  -> [1].
  57. fact_loop(N) -> 
  58.     F
  59.     |   ( bigmul(fact_loop(N-1),0,N,F2), setEco(inter,F2), fail ; succeed),
  60.     F = inter .
  61.  
  62.  
  63. factorial2(N) -> F | N>=0, F = fact_loop2(N).
  64.  
  65. fact_loop2(0) -> [1].
  66. fact_loop2(N) -> F | bigmul2(fact_loop2(N-1),0,N,F).
  67.  
  68.  
  69. factorial3(N) -> F | N>=0, F = fact_loop3(N).
  70.  
  71. fact_loop3(0) -> [1].
  72. fact_loop3(N) -> bigmul3(fact_loop3(N-1),0,N).
  73.  
  74.  
  75. factorial4(N) -> F | N>=0, F = fact_loop4(N).
  76.  
  77. fact_loop4(0) -> [1].
  78. fact_loop4(N) -> bigmul4(fact_loop4(N-1),0,N).
  79.  
  80. % Multiply and add: (integer*bignum)+integer -> bignum
  81.  
  82. bigmul([], 0, Mul,[]) :- !.
  83. bigmul([], Add, Mul,[Add]) :- !.
  84. bigmul([L|BigIn], Add, Mul,[X:(L*Mul+ Add) mod 10000 |BigOut]) :- 
  85.     bigmul(BigIn,floor(X/10000),Mul,BigOut).
  86.  
  87. bigmul2([], 0, Mul,[]) :- !.
  88. bigmul2([], Add, Mul,[Add]) :- !.
  89. bigmul2([L|BigIn], Add, Mul,[X:(L*Mul+ Add) mod 10000 |BigOut]) :- 
  90.     bigmul2(BigIn,floor(X/10000),Mul,BigOut).
  91.  
  92. bigmul3([], 0,   _) -> [].
  93. bigmul3([], Add, _) -> [Add].
  94. bigmul3([L|BigIn], Add, Mul) ->
  95.     [X:(L*Mul+ Add) mod 10000 | bigmul3(BigIn,floor(X/10000),Mul)].
  96.  
  97. bigmul4([], 0,   _) -> [].
  98. bigmul4([], Add, _) -> [Add].
  99. bigmul4([L|BigIn], Add, Mul) ->
  100.     [X:(L*Mul+ Add) mod 10000 | F]
  101.     | F=bigmul4(BigIn,floor(X/10000),Mul).
  102.  
  103.  
  104. write_fact([]) :- !.
  105. write_fact([N|L]) :-
  106.     write_four(N),
  107.     write_fact(L).
  108.  
  109. write_four(N) :- 
  110.     cond( N<10,
  111.           (write('000'), write(N)),
  112.           cond(N<100,
  113.                (write('00'), write(N)),
  114.            cond( N<1000,
  115.                  (write('0'), write(N)),
  116.               write(N)))).
  117.  
  118. reverse(A, B) :- reverse_3(A, [], B).
  119.  
  120. reverse_3([X|A], L, B) :- !, reverse_3(A, [X|L], B).
  121. reverse_3([], B, B).
  122.  
  123. dynamic(inter)?
  124. inter->0.
  125.  
  126. non_strict(setEco)?
  127. setEco(_A,_B) :-
  128.     retract((_A -> @)),
  129.     assert((_A -> _B)).
  130.  
  131.  
  132.