Van der Corput sequence: Difference between revisions

Line 2,122:
 
=={{header|Prolog}}==
=== Example solution ===
<lang prolog>% vdc( N, Base, Out )
% Out = the Van der Corput representation of N in given Base
Line 2,180 ⟶ 2,181:
7th member in base 4 (stretch goal) => 0.31
true .
</pre>
 
=== Efficient generator ===
<lang prolog>
% g(B,N,X):- consecutively generate in X the first N elements of the sequence based on {0, 1, ..., B}
 
g(_,N,[L|_]-_,X):- N > 1, atomic_list_concat(['0.'|L],X).
g(B,N,[L|Ls]-Xs,X):- N > 1, M is N-1, findall([I|L], between(0,B,I), T), append(T,Ys,Xs), g(B,M,Ls-Ys,X).
g(_,N,'0.0'):- N > 0.
g(B,N,X):- N > 0, findall([I], between(1,B,I), T), T \= [], append(T,Ys,Xs), g(B,N,Xs-Ys,X).
</lang>
{{out}}
<pre>
?- g(2,10,X).
X = '0.0' ;
X = '0.1' ;
X = '0.2' ;
X = '0.01' ;
...
X = '0.001' ;
false.
 
?- time(findall(X, g(1,1000000,X), T)).
% 23,000,011 inferences, 5.938 CPU in 6.083 seconds (98% CPU, 3873686 Lips)
T = ['0.0', '0.1', '0.01', '0.11', '0.001', '0.101', '0.011', '0.111', '0.0001'|...].
</pre>
 
Anonymous user