Brilliant numbers: Difference between revisions

Line 984:
</pre>
=={{header|Prolog}}==
<syntaxhighlightworks lang="with swi-prolog">
<syntaxhighlight lang="prolog">factors(N, Flist):-
factors(N, 2, 0, Flist).
 
factors(1, _, _, []).
factors(_, _, Cnt, []):- Cnt > 1,!.
factors(N, Start, Cnt, [Fac|FList]):-
N1 is floor(sqrt(N)),
between(Start, N1, Fac),
N mod Fac =:= 0,!,
N2 is N div Fac,
Cnt1 is Cnt + 1,
factors(N2, Fac, Cnt1, FList).
factors(N, _, _, [N]):- N >= 2.
 
brilliantList(Start, Limit, List):-
findall(N, brilliants(Start, Limit, N), List).
nextBrilliant(Start, N):-
brilliants(Start, inf, N).
isBrilliant(N):-
brilliants(2, inf, N).
brilliants(Start, Limit, N):-
between(Start, Limit, N),
factors(N,[F1,F2]),
F1 * F2 =:= N,
digits(F1, D1), digits(F2, D2),
D1 =:= D2.
 
digits(N, D):-
D is 1 + floor(log10(N)).
 
%% generate results
 
run(LimitList):-
run(LimitList, 0, 2).
run([], _, _).
run([Limit|LList], OldCount, OldLimit):-
Limit1 is Limit - 1,
statistics(runtime,[Start|_]),
brilliantList(OldLimit, Limit1, BList),
length(BList, Cnt),
Cnt1 is OldCount + Cnt,
Index is Cnt1 + 1,
nextBrilliant(Limit, Bril),!,
statistics(runtime,[Stop|_]),
Time is Stop - Start,
writef('first >=%8r is%8r at position%6r [time:%6r]', [Limit, Bril, Index, Time]),nl,
run(LList, Cnt1, Limit).
 
showList(List, Limit):-
showList(List, Limit, 1).
showList([], _, _).
showList([H|TList], Limit, C):-
writef('%5r', [H]),
( C < Limit
-> C1 is C + 1
; C1 is 1, nl
),
showList(TList, Limit, C1).
do:-findnsols(100, B, isBrilliant(B), BList),!,
showList(BList, 10),nl,
numlist(1, 6, NList),
maplist([X,Y]>>(Y is 10**X), NList, LimitList),
run(LimitList).
</syntaxhighlight>
{{out}}
<pre>?- do.
4 6 9 10 14 15 21 25 35 49
121 143 169 187 209 221 247 253 289 299
319 323 341 361 377 391 403 407 437 451
473 481 493 517 527 529 533 551 559 583
589 611 629 649 667 671 689 697 703 713
731 737 767 779 781 793 799 803 817 841
851 869 871 893 899 901 913 923 943 949
961 979 989 1003 1007 1027 1037 1067 1073 1079
1081 1121 1139 1147 1157 1159 1189 1207 1219 1241
1247 1261 1271 1273 1333 1343 1349 1357 1363 1369
 
first >= 10 is 10 at position 4 [time: 0]
first >= 100 is 121 at position 11 [time: 1]
first >= 1000 is 1003 at position 74 [time: 4]
first >= 10000 is 10201 at position 242 [time: 73]
first >= 100000 is 100013 at position 2505 [time: 1442]
first >= 1000000 is 1018081 at position 10538 [time: 34054]
true.
</pre>
 
=={{header|Python}}==
64

edits