Jump to content

Left factorials: Difference between revisions

Added Prolog Solution
(Added alternative C++ solution)
(Added Prolog Solution)
Line 2,536:
!9000 has 31678 digits
!10000 has 35656 digits
</pre>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<lang prolog>leftfact(N):-
leftfact(N, 0, 0, 1).
 
leftfact(N, N, _, _):-
!.
leftfact(N, M, L, F):-
((M =< 10 ; (M =< 110, 0 is M mod 10)) ->
writef("!%w = %w\n", [M, L])
;
(0 is M mod 1000 ->
number_string(L, S),
string_length(S, Len),
writef("length of !%w is %w\n", [M, Len])
;
true)),
L1 is L + F,
M1 is M + 1,
F1 is F * M1,
leftfact(N, M1, L1, F1).
main:-
leftfact(10001).</lang>
 
{{out}}
<pre>
!0 = 0
!1 = 1
!2 = 2
!3 = 4
!4 = 10
!5 = 34
!6 = 154
!7 = 874
!8 = 5914
!9 = 46234
!10 = 409114
!20 = 128425485935180314
!30 = 9157958657951075573395300940314
!40 = 20935051082417771847631371547939998232420940314
!50 = 620960027832821612639424806694551108812720525606160920420940314
!60 = 141074930726669571000530822087000522211656242116439949000980378746128920420940314
!70 = 173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314
!80 = 906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314
!90 = 16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314
!100 = 942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
!110 = 145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
length of !1000 is 2565
length of !2000 is 5733
length of !3000 is 9128
length of !4000 is 12670
length of !5000 is 16322
length of !6000 is 20062
length of !7000 is 23875
length of !8000 is 27749
length of !9000 is 31678
length of !10000 is 35656
</pre>
 
1,777

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.