Magic constant: Difference between revisions

no edit summary
(restore BASIC header)
No edit summary
Line 382:
1e19: 2,714,418
1e20: 5,848,036
</pre>
 
=={{header|Prolog}}==
Minimalistic, efficient approach
<lang prolog>
m(X,Y):- Y is X*(X*X+1)/2.
 
l(L,R,T,X):- L > R -> X is L; M is div(L+R,2), m(M,F),
(T < F -> R_ is M-1, l(L,R_,T,X); L_ is M+1, l(L_,R,T,X)).
l(B,X):- l(1,B,B,X).
 
task:-
write("First 20 magic constants are:"), findall(_, (between(3,22,N), m(N,X), format(" ~d",X)), _), nl,
write("The 1000th magic constant is:"), findall(_, (m(1002,X), format(" ~d",X)), _), nl,
findall(_, (between(1,20,N), l(10**N,X), format("10^~d:\t~d\n",[N,X])), _).
</lang>
{{out}}
<pre>
?- task.
First 20 magic constants are: 15 34 65 111 175 260 369 505 671 870 1105 1379 1695 2056 2465 2925 3439 4010 4641 5335
The 1000th magic constant is: 503006505
10^1: 3
10^2: 6
10^3: 13
10^4: 28
10^5: 59
10^6: 126
10^7: 272
10^8: 585
10^9: 1260
10^10: 2715
10^11: 5849
10^12: 12600
10^13: 27145
10^14: 58481
10^15: 125993
10^16: 271442
10^17: 584804
10^18: 1259922
10^19: 2714418
10^20: 5848036
true.
</pre>
 
Anonymous user