Left factorials: Difference between revisions

Slower and longer but more clear D entry
(→‎{{header|Ruby}}: Deleted doubled ruby)
(Slower and longer but more clear D entry)
Line 20:
 
=={{header|D}}==
<lang d>import std.stdio, std.rangebigint, std.algorithmrange, std.bigintalgorithm, std.conv;
<lang d>void main() {
import std.stdio, std.range, std.algorithm, std.bigint, std.conv;
 
BigInt leftFact(in uint n) pure /*nothrow*/ {
auto i1 = 11.iota.chain(iota(20,111, 10)).zip(0.repeat).assocArray;
BigInt result = 0, factorial = 1;
auto i2 = iota(1_000, 10_001, 1_000).zip(0.repeat).assocArray;
foreach (immutable ji; 01 .. i2.byKey.reduce!maxn + 1) {
 
BigInt leftFact, fact result += 1factorial;
factfactorial *= j + 1i;
foreach (immutable j; 0 .. i2.byKey.reduce!max + 1) {
if (j in i1)
writefln("!%d = %s", j, leftFact);
else if (j in i2)
writefln("!%d has %d digits.", j, leftFact.text.length);
leftFact += fact;
fact *= j + 1;
}
return result;
}
 
<lang d>void main() {
writeln("First 11 left factorials:\n", 11.iota.map!leftFact);
writefln("\n20 through 110 (inclusive) by tens:\n%(%s\n%)",
writefln iota("!%d = %s"20, j111, 10).map!leftFact);
writefln("\nDigits in 1,000 through 10,000 by thousands:\n%s",
writefln iota("!%d1000,10001, has %d digits1000).",map!(i j,=> i.leftFact.text.length));
}</lang>
{{out}}
<pre>!0First =11 0left factorials:
[0, 1, 2, 4, 10, 34, 154, 874, 5914, 46234, 409114]
!1 = 1
 
!2 = 2
20 through 110 (inclusive) by tens:
!3 = 4
!20 = 128425485935180314
!4 = 10
!30 = 9157958657951075573395300940314
!5 = 34
!40 = 20935051082417771847631371547939998232420940314
!6 = 154
!50 = 620960027832821612639424806694551108812720525606160920420940314
!7 = 874
!60 = 141074930726669571000530822087000522211656242116439949000980378746128920420940314
!8 = 5914
!70 = 173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314
!9 = 46234
!80 = 906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314
!10 = 409114
!90 = 16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314
!20 = 128425485935180314
!100 = 942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
!30 = 9157958657951075573395300940314
!110 = 145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
!40 = 20935051082417771847631371547939998232420940314
 
!50 = 620960027832821612639424806694551108812720525606160920420940314
Digits in 1,000 through 10,000 by thousands:
!60 = 141074930726669571000530822087000522211656242116439949000980378746128920420940314
[2565, 5733, 9128, 12670, 16322, 20062, 23875, 27749, 31678, 35656]</pre>
!70 = 173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314
!80 = 906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314
!90 = 16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314
!100 = 942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
!110 = 145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
!1000 has 2565 digits.
!2000 has 5733 digits.
!3000 has 9128 digits.
!4000 has 12670 digits.
!5000 has 16322 digits.
!6000 has 20062 digits.
!7000 has 23875 digits.
!8000 has 27749 digits.
!9000 has 31678 digits.
!10000 has 35656 digits.</pre>
The run-time is about 0.60 seconds.
 
=={{header|Perl 6}}==