Advertisement
logicmoo

Untitled

Mar 25th, 2019
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 3.96 KB | None | 0 0
  1. /*
  2.  
  3.  Test Queries:
  4.  
  5.  
  6. ?- time(prime_facter(1361129467683753853853498429727072845823, L)).
  7. % 42,132,024 inferences, 2.654 CPU in 2.703 seconds (98% CPU, 15877312 Lips)
  8. L = [1, 145295143558111, 7623851, 409891, 8191, 2731, 131, 31, 11|...].
  9.  
  10. ?- product(X,Y,1361129467683753853853498429727072845823).
  11. X = 1,
  12. Y = 1361129467683753853853498429727072845823 ;
  13. X = 3,
  14. Y = 453709822561251284617832809909024281941 ;
  15. X = 33,
  16. Y = 41246347505568298601621164537184025631 ;
  17. X = 1023,
  18. Y = 1330527338889299954891005307651097601 ;
  19. X = 134013,
  20. Y = 10156697243429770648022941279779371 ;
  21. X = 365989503,
  22. Y = 3719039635089626747719861325441 ;
  23. X = 2997820019073,
  24. Y = 454039755230085062595514751 ;
  25. X = 1228779445437851043,
  26. Y = 1107708525510648105461 ;
  27. X = 9368031403880806112026593,
  28. Y = 145295143558111 ;
  29. X = 1361129467683753853853498429727072845823,
  30. Y = 1 ;
  31.  
  32. ?- product(A,B,1).
  33.  
  34. ?- product(A,B,2).
  35.  
  36. ?- product(A,B,3).
  37.  
  38. ?- product(A,B,9).
  39.  
  40. ?- product(1,2,1).
  41.                                                  
  42. ?- \+ product(1,2,1361129467683753853853498429727072845823).
  43.  
  44.  
  45. ?- time(prime_facter(4987562349672390876389027, L)).
  46.  
  47. */
  48.  
  49. % Prime factorization
  50.  
  51. prime_facter(N, [1|L]) :-
  52.   SN is sqrt(N),
  53.   prime_facter_1(N, SN, 2, [], L).
  54.  
  55. prime_facter_1(1, _, _, L, L) :- !.
  56. prime_facter_1(N, SN, D, L, LF) :- % Special case for 2, increment 1
  57.   (   0 is N mod D ->
  58.       Q is N / D,
  59.       SQ is sqrt(Q),
  60.       prime_facter_1(Q, SQ, D, [D |L], LF)
  61.   ;
  62.       D1 is D+1,
  63.       ( D1 > SN ->
  64.           LF = [N |L]
  65.       ;
  66.           prime_facter_2(N, SN, D1, L, LF)
  67.       )
  68.   ).
  69.  
  70. prime_facter_2(1, _, _, L, L) :- !. % General case, increment 2
  71. prime_facter_2(N, SN, D, L, LF) :-
  72.    (   0 is N mod D ->
  73.     ( Q is N / D,
  74.       SQ is sqrt(Q),
  75.       prime_facter_2(Q, SQ, D, [D |L], LF))
  76.     ;
  77.       ((nxt_prime(D,D1)->true;D1 is D+2),
  78.       ( D1 > SN ->
  79.          LF = [N |L] ; prime_facter_2(N, SN, D1, L, LF)))
  80.    ).
  81.  
  82. % A slightly nicer version is one which is able to enumerate all the primes.
  83. prime(Prime) :-    
  84.     sieve3(1, [], Prime).  
  85.  
  86. sieve3(M, PS, Prime) :-
  87.     N is 1+M,
  88.     (   member(P, PS), N mod P =:= 0 ->  % N is composite
  89.         sieve3(N, PS, Prime)
  90.     ;/* forall P in PS N mod P =\= 0 */
  91.         (   Prime = N
  92.         ;   sieve3(N, [N|PS], Prime)
  93.         )
  94.     ).
  95.            
  96. :- dynamic(nxt_prime/2).
  97.  
  98. nxt_prime(0,1).
  99.  
  100. save_primes:-
  101.   prime(Prime),
  102.   (nxt_prime(_,Prev) -> asserta(nxt_prime(Prev,Prime))),
  103.   Prime>20000,!.
  104.  
  105.  
  106. distinct_var(Var) :-
  107.     nonvar(Var), !.
  108. distinct_var(Var) :-
  109.     empty_nb_set(Set),
  110.     put_attr(Var, dist_var, Set).
  111.  
  112. dist_var:attr_unify_hook(Set, Value) :-
  113.     add_nb_set(Value, Set, true).
  114.  
  115.  
  116. :- time(save_primes).
  117.  
  118.  
  119. product(A,B,C):- harness(mult,[A,B,C]).
  120.  
  121. %mult_u_u_u(A,B,C):-  guessed.
  122. mult_u_u_b(A,B,C):- prime_facter(C,PS), distinct_var(S), distinct_var(A), some_of(PS,Some), mult_list(Some,S), member(S,[A,B]), product(A,B,C).
  123. %mult_u_b_u(A,B,C):-  guessed.
  124. mult_u_b_b(A,B,C):- A is C / B.
  125. %mult_b_u_u(A,B,C):-  guessed.
  126. mult_b_u_b(A,B,C):- mult_u_b_b(B,A,C).
  127. mult_b_b_u(A,B,C):- C is A * B.
  128. mult_b_b_b(A,B,C):- mult_b_b_u(A,B,C).
  129.  
  130. some_of(Pm,These):-
  131.   %sort(Pm,PS),
  132.   =(Pm,PS),
  133.   permutation(PS,PM),
  134.   append([Of|Some],_,PM),
  135.   =([Of|Some],These).
  136.  
  137. mult_list([H|T],R):- !,
  138.   mult_list(T,NT),
  139.   R is H*NT.
  140. mult_list(_,1).  
  141.  
  142.  
  143. % boundedness
  144.  
  145. harness(P,V):- harness(P,V,V).
  146.  
  147. to_bounds_l([],[]).
  148. to_bounds_l([Var|Rest],[Type|RestL]):-
  149.   to_bounds(Var,Type),
  150.   to_bounds_l(Rest,RestL).
  151.  
  152. to_bounds(Var,Type):- var(Var),
  153.  % \+ attvar(Var),
  154.  !, Type=u.
  155. to_bounds(_,b):-!.
  156.  
  157. harness(P,V,VT):-
  158.   to_bounds_l(VT,UBU),
  159.   atomic_list_concat([P|UBU],'_',PU),
  160.   current_predicate(PU/_),
  161.     !,apply(PU,V).
  162. harness(P,V,_):- atomic_list_concat([P,'g'],'_',PU),
  163.   current_predicate(PU/_),
  164.     !,apply(PU,V).
  165. harness(P,V,_):- term_variables(V,Vs),
  166.    Vs\==[],
  167.    hook_vs(Vs,P,V).
  168.  
  169. hook_vs([],_,_).
  170. hook_vs([V|Vs],P,V):-
  171.   freeze(V,harness(P,V)),
  172.   hook_vs(Vs,P,V).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement