Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
20
Task/Almost-prime/Prolog/almost-prime-1.pro
Normal file
20
Task/Almost-prime/Prolog/almost-prime-1.pro
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
% almostPrime(K, +Take, List) succeeds if List can be unified with the
|
||||
% first Take K-almost-primes.
|
||||
% Notice that K need not be specified.
|
||||
% To avoid having to cache or recompute the first Take primes, we define
|
||||
% almostPrime/3 in terms of almostPrime/4 as follows:
|
||||
%
|
||||
almostPrime(K, Take, List) :-
|
||||
% Compute the list of the first Take primes:
|
||||
nPrimes(Take, Primes),
|
||||
almostPrime(K, Take, Primes, List).
|
||||
|
||||
almostPrime(1, Take, Primes, Primes).
|
||||
|
||||
almostPrime(K, Take, Primes, List) :-
|
||||
generate(2, K), % generate K >= 2
|
||||
K1 is K - 1,
|
||||
almostPrime(K1, Take, Primes, L),
|
||||
multiplylist( Primes, L, Long),
|
||||
sort(Long, Sorted), % uniquifies
|
||||
take(Take, Sorted, List).
|
||||
40
Task/Almost-prime/Prolog/almost-prime-2.pro
Normal file
40
Task/Almost-prime/Prolog/almost-prime-2.pro
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
nPrimes( M, Primes) :- nPrimes( [2], M, Primes).
|
||||
|
||||
nPrimes( Accumulator, I, Primes) :-
|
||||
next_prime(Accumulator, Prime),
|
||||
append(Accumulator, [Prime], Next),
|
||||
length(Next, N),
|
||||
( N = I -> Primes = Next; nPrimes( Next, I, Primes)).
|
||||
|
||||
% next_prime(+Primes, NextPrime) succeeds if NextPrime is the next
|
||||
% prime after a list, Primes, of consecutive primes starting at 2.
|
||||
next_prime([2], 3).
|
||||
next_prime([2|Primes], P) :-
|
||||
last(Primes, PP),
|
||||
P2 is PP + 2,
|
||||
generate(P2, N),
|
||||
1 is N mod 2, % odd
|
||||
Max is floor(sqrt(N+1)), % round-off paranoia
|
||||
forall( (member(Prime, [2|Primes]),
|
||||
(Prime =< Max -> true
|
||||
; (!, fail))), N mod Prime > 0 ),
|
||||
!,
|
||||
P = N.
|
||||
|
||||
% multiply( +A, +List, Answer )
|
||||
multiply( A, [], [] ).
|
||||
multiply( A, [X|Xs], [AX|As] ) :-
|
||||
AX is A * X,
|
||||
multiply(A, Xs, As).
|
||||
|
||||
% multiplylist( L1, L2, List ) succeeds if List is the concatenation of X * L2
|
||||
% for successive elements X of L1.
|
||||
multiplylist( [], B, [] ).
|
||||
multiplylist( [A|As], B, List ) :-
|
||||
multiply(A, B, L1),
|
||||
multiplylist(As, B, L2),
|
||||
append(L1, L2, List).
|
||||
|
||||
take(N, List, Head) :-
|
||||
length(Head, N),
|
||||
append(Head,X,List).
|
||||
28
Task/Almost-prime/Prolog/almost-prime-3.pro
Normal file
28
Task/Almost-prime/Prolog/almost-prime-3.pro
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
%%%%% compatibility section %%%%%
|
||||
|
||||
:- if(current_prolog_flag(dialect, yap)).
|
||||
generate(Min, I) :- between(Min, inf, I).
|
||||
|
||||
append([],L,L).
|
||||
append([X|Xs], L, [X|Ls]) :- append(Xs,L,Ls).
|
||||
|
||||
:- endif.
|
||||
|
||||
:- if(current_prolog_flag(dialect, swi)).
|
||||
generate(Min, I) :- between(Min, inf, I).
|
||||
:- endif.
|
||||
|
||||
:- if(current_prolog_flag(dialect, yap)).
|
||||
append([],L,L).
|
||||
append([X|Xs], L, [X|Ls]) :- append(Xs,L,Ls).
|
||||
|
||||
last([X], X).
|
||||
last([_|Xs],X) :- last(Xs,X).
|
||||
|
||||
:- endif.
|
||||
|
||||
:- if(current_prolog_flag(dialect, gprolog)).
|
||||
generate(Min, I) :-
|
||||
current_prolog_flag(max_integer, Max),
|
||||
between(Min, Max, I).
|
||||
:- endif.
|
||||
Loading…
Add table
Add a link
Reference in a new issue