Left factorials: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(38 intermediate revisions by 17 users not shown)
Line 4:
 
'''Left factorials''', &nbsp; <big><big>!n</big></big>, &nbsp; may refer to either &nbsp; ''subfactorials'' &nbsp; or to &nbsp; ''factorial sums'';
<br>the same notation can be confusingly seen being used for the two different definitions.
 
Sometimes, &nbsp; ''subfactorials'' &nbsp; (also known as ''derangements'') &nbsp; may use any of the notations:
Line 10:
:::::::* &nbsp; <big><big> <b> <span style="font-family:serif">!''n''</span> </b> </big></big>
:::::::* &nbsp; <big><big> <b> <span style="font-family:serif">''n''¡</span> </b> </big></big>
 
 
(It may not be visually obvious, but the last example uses an upside-down exclamation mark.)
 
 
<br>This Rosetta Code task will be using this formula &nbsp; (''factorial sums'') &nbsp; for &nbsp; '''left factorial''':
<big><big>
::::: &nbsp; <math> !n = \sum_{k=0}^{n-1} k! </math>
</big></big>
:::: where
<big><big>
::::: &nbsp; <math>!0 = 0</math>
</big></big>
 
Line 26 ⟶ 27:
;Task
Display the left factorials for:
* &nbsp; zero through ten &nbsp; &nbsp; (inclusive)
* &nbsp; 20 &nbsp; through &nbsp; 110 &nbsp; (inclusive) &nbsp; by tens
 
<br>
Display the length (in decimal digits) of the left factorials for:
* 1,000, &nbsp; 21,000 &nbsp; through &nbsp; 10,000 &nbsp; (inclusive), by thousands.
 
 
;Also see:
* &nbsp; The OEIS entry: [http://oeis.org/A003422 A003422 left factorials]
* &nbsp; The MathWorld entry: [http://mathworld.wolfram.com/LeftFactorial.html left factorial]
Line 44 ⟶ 45:
* &nbsp; [http://rosettacode.org/wiki/Permutations/Derangements permutations/derangements (subfactorials)]
<br><br>
 
=={{header|11l}}==
{{trans|D}}
 
<syntaxhighlight lang="11l">F left_fact(n)
BigInt result = 0
BigInt factorial = 1
L(i) 1 .. n
result += factorial
factorial *= i
R result
 
print(‘First 11 left factorials:’)
print((0..10).map(i -> left_fact(i)))
print("\n20 through 110 (inclusive) by tens:")
L(i) (20..110).step(10)
print(left_fact(i))
print("\nDigits in 1,000 through 10,000 by thousands:")
print((1000..10000).step(1000).map(i -> String(left_fact(i)).len))</syntaxhighlight>
 
{{out}}
<pre>
First 11 left factorials:
[0, 1, 2, 4, 10, 34, 154, 874, 5914, 46234, 409114]
 
20 through 110 (inclusive) by tens:
128425485935180314
9157958657951075573395300940314
20935051082417771847631371547939998232420940314
620960027832821612639424806694551108812720525606160920420940314
141074930726669571000530822087000522211656242116439949000980378746128920420940314
173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314
906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314
16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314
942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
 
Digits in 1,000 through 10,000 by thousands:
[2565, 5733, 9128, 12670, 16322, 20062, 23875, 27749, 31678, 35656]
</pre>
 
=={{header|ALGOL 68}}==
Uses the Algol 68G LONG LONG INT type which has programmer definable precision.
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<langsyntaxhighlight lang="algol68"># set the precision of LONG LONG INT - large enough for !n up to ! 10 000 #
PR precision 36000 PR
# stores left factorials in an array #
Line 115 ⟶ 156:
OD
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 153 ⟶ 194:
35656
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">lfactorial: function [n][
if zero? n -> return 0
fold 0..dec n [x y] -> x + factorial y
]
 
print "First eleven:"
0..10 | map => lfactorial
| print
 
print "\n20th through 110th by tens:"
r: range.step: 10 20 110
r | map => lfactorial
| loop => print
 
print "\nDigits in 1,000th through 10,000th by thousands:"
r: range.step: 1000 1000 10000
r | map'x -> size ~"|lfactorial x|"
| print</syntaxhighlight>
 
{{out}}
 
<pre>First eleven:
0 1 2 4 10 34 154 874 5914 46234 409114
 
20th through 110th by tens:
128425485935180314
9157958657951075573395300940314
20935051082417771847631371547939998232420940314
620960027832821612639424806694551108812720525606160920420940314
141074930726669571000530822087000522211656242116439949000980378746128920420940314
173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314
906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314
16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314
942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
 
Digits in 1,000th through 10,000th by thousands:
2565 5733 9128 12670 16322 20062 23875 27749 31678 35656</pre>
 
=={{header|AWK}}==
Old posix AWK doesn't support computing with large numbers. However modern gawk can use GMP if the flag -M is used.
<syntaxhighlight lang="awk">
<lang AWK>
#!/usr/bin/gawk -Mf
function left_factorial(num) {
Line 180 ⟶ 262:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 215 ⟶ 297:
!10000 has 35656 digits
</pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
Use the 'Mapm' library.
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"BB4WMAPMLIB" : PROCMAPM_Init : MAPM_Dec%=200
 
Result$="0" : A$="1"
Line 228 ⟶ 311:
IF I% > 999 IF I% MOD 1000 = 0 PRINT "!";I% " has " LENFNMAPM_FormatDec(Result$,0) " digits"
NEXT
END</langsyntaxhighlight>
{{out}}
<pre>
Line 265 ⟶ 348:
=={{header|Bracmat}}==
{{trans|D}}
<langsyntaxhighlight lang="bracmat">( ( leftFact
= result factorial i
. 0:?result
Line 301 ⟶ 384:
. (=L.@(!arg:? [?L)&out$!L)
)
)</langsyntaxhighlight>
{{out}}
<pre>First 11 left factorials:
Line 342 ⟶ 425:
=={{header|C}}==
{{libheader|GMP}}
<syntaxhighlight lang="c">
<lang C>
#include <stdio.h>
#include <stdlib.h>
Line 393 ⟶ 476:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 428 ⟶ 511:
!10000 has 35656 digits
</pre>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">
using System;
using System.Numerics;
 
namespace LeftFactorial
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i <= 10; i++)
{
Console.WriteLine(string.Format("!{0} = {1}", i, LeftFactorial(i)));
}
 
for (int j = 20; j <= 110; j += 10)
{
Console.WriteLine(string.Format("!{0} = {1}", j, LeftFactorial(j)));
}
 
for (int k = 1000; k <= 10000; k += 1000)
{
Console.WriteLine(string.Format("!{0} has {1} digits", k, LeftFactorial(k).ToString().Length));
}
 
Console.ReadKey();
}
 
private static BigInteger Factorial(int number)
{
BigInteger accumulator = 1;
 
for (int factor = 1; factor <= number; factor++)
{
accumulator *= factor;
}
 
return accumulator;
}
 
private static BigInteger LeftFactorial(int n)
{
BigInteger result = 0;
 
for (int i = 0; i < n; i++)
{
result += Factorial(i);
}
 
return result;
}
}
}
</syntaxhighlight>
{{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
!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>
 
Faster Implementation
 
<syntaxhighlight lang="csharp">
using System;
using System.Numerics;
 
namespace LeftFactorial
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i <= 10; i++)
{
Console.WriteLine(string.Format("!{0} : {1}", i, LeftFactorial(i)));
}
 
for (int j = 20; j <= 110; j += 10)
{
Console.WriteLine(string.Format("!{0} : {1}", j, LeftFactorial(j)));
}
 
for (int k = 1000; k <= 10000; k += 1000)
{
Console.WriteLine(string.Format("!{0} : has {1} digits", k, LeftFactorial(k).ToString().Length));
}
 
Console.ReadKey();
}
 
private static BigInteger LeftFactorial(int n)
{
BigInteger result = 0;
BigInteger subResult = 1;
 
for (int i = 0; i < n; i++)
{
if (i == 0)
{
subResult = 1;
}
else
{
subResult *= i;
}
 
result += subResult;
}
 
return result;
}
}
}
</syntaxhighlight>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">
<lang CPP>
#include <vector>
#include <string>
Line 558 ⟶ 786:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 594 ⟶ 822:
</pre>
 
===Faster alternative===
=={{header|C sharp|C#}}==
{{libheader|GMP}}
<lang csharp>
<syntaxhighlight lang="cpp">#include <iostream>
using System;
#include <gmpxx.h>
using System.Numerics;
 
template <typename integer>
namespace LeftFactorial
class left_factorial_generator {
{
public:
class Program
integer next() {
staticinteger voidresult Main(string[]= args)next_;
{next_ += factorial_;
factorial_ for (int i *= 0; i <= 10; in_++);
return {result;
}
Console.WriteLine(string.Format("!{0} = {1}", i, LeftFactorial(i)));
private:
}
unsigned int n_ = 1;
integer factorial_ = 1;
integer next_ = 0;
};
 
int main() {
for (int j = 20; j <= 110; j += 10)
left_factorial_generator<mpz_class> lf;
{
int i = 0;
Console.WriteLine(string.Format("!{0} = {1}", j, LeftFactorial(j)));
std::cout << "Left factorials 0 through 10:\n";
}
for (; i <= 10; ++i)
std::cout << "!" << i << " = " << lf.next() << '\n';
std::cout << "Left factorials 20 through 110, by tens:\n";
for (; i <= 110; ++i) {
auto n = lf.next();
if (i % 10 == 0)
std::cout << "!" << i << " = " << n << '\n';
}
std::cout << "Lengths of left factorials 1000 through 10000, by thousands:\n";
for (; i <= 10000; ++i) {
auto n = lf.next();
if (i % 1000 == 0)
std::cout << "length of !" << i << " = " << n.get_str().size() << '\n';
}
return 0;
}</syntaxhighlight>
 
for (int k = 1000; k <= 10000; k += 1000)
{
Console.WriteLine(string.Format("!{0} has {1} digits", k, LeftFactorial(k).ToString().Length));
}
 
Console.ReadKey();
}
 
private static BigInteger Factorial(int number)
{
BigInteger accumulator = 1;
 
for (int factor = 1; factor <= number; factor++)
{
accumulator *= factor;
}
 
return accumulator;
}
 
private static BigInteger LeftFactorial(int n)
{
BigInteger result = 0;
 
for (int i = 0; i < n; i++)
{
result += Factorial(i);
}
 
return result;
}
}
}
</lang>
{{out}}
<pre>
Left factorials 0 through 10:
!0 = 0
!1 = 1
Line 662 ⟶ 877:
!9 = 46234
!10 = 409114
Left factorials 20 through 110, by tens:
!20 = 128425485935180314
!30 = 9157958657951075573395300940314
Line 672 ⟶ 888:
!100 = 942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
!110 = 145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
Lengths of left factorials 1000 through 10000, by thousands:
!1000 has 2565 digits
length of !1000 = 2565
!2000 has 5733 digits
length of !2000 = 5733
!3000 has 9128 digits
length of !3000 = 9128
!4000 has 12670 digits
length of !4000 = 12670
!5000 has 16322 digits
length of !5000 = 16322
!6000 has 20062 digits
length of !6000 = 20062
!7000 has 23875 digits
length of !7000 = 23875
!8000 has 27749 digits
length of !8000 = 27749
!9000 has 31678 digits
length of !9000 = 31678
!10000 has 35656 digits
length of !10000 = 35656
</pre>
 
Faster Implementation
 
<lang csharp>
using System;
using System.Numerics;
 
namespace LeftFactorial
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i <= 10; i++)
{
Console.WriteLine(string.Format("!{0} : {1}", i, LeftFactorial(i)));
}
 
for (int j = 20; j <= 110; j += 10)
{
Console.WriteLine(string.Format("!{0} : {1}", j, LeftFactorial(j)));
}
 
for (int k = 1000; k <= 10000; k += 1000)
{
Console.WriteLine(string.Format("!{0} : has {1} digits", k, LeftFactorial(k).ToString().Length));
}
 
Console.ReadKey();
}
 
private static BigInteger LeftFactorial(int n)
{
BigInteger result = 0;
BigInteger subResult = 1;
 
for (int i = 0; i < n; i++)
{
if (i == 0)
{
subResult = 1;
}
else
{
subResult *= i;
}
 
result += subResult;
}
 
return result;
}
}
}
</lang>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(ns left-factorial
(:gen-class))
 
Line 759 ⟶ 921:
(doseq [n (range 1000 10001 1000)]
(println (format "!%-5d has %5d digits" n (count (str (biginteger (left-factorial n)))))))
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 796 ⟶ 958:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">
(defun fact (n)
(reduce #'* (loop for i from 1 to n collect i)))
Line 809 ⟶ 971:
(format t "1000 -> 10000 by 1000~&")
(format t "~{~a digits~&~}" (loop for i from 1000 upto 10000 by 1000 collect (length (format nil "~a" (left-fac i)))))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 839 ⟶ 1,001:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.bigint, std.range, std.algorithm, std.conv;
 
BigInt leftFact(in uint n) pure nothrow /*@safe*/ {
Line 856 ⟶ 1,018:
writefln("\nDigits in 1,000 through 10,000 by thousands:\n%s",
iota(1_000, 10_001, 1_000).map!(i => i.leftFact.text.length));
}</langsyntaxhighlight>
{{out}}
<pre>First 11 left factorials:
Line 878 ⟶ 1,040:
=={{header|EchoLisp}}==
We use the 'bigint' library and memoization : (remember 'function).
<langsyntaxhighlight lang="lisp">
(lib 'bigint)
(define (!n n)
Line 884 ⟶ 1,046:
(+ (!n (1- n)) (factorial (1- n)))))
(remember '!n)
</syntaxhighlight>
</lang>
Output:
<langsyntaxhighlight lang="lisp">
(for ((n 11)) (printf "!n(%d) = %d" n (!n n)))
(for ((n (in-range 20 120 10))) (printf "!n(%d) = %d" n (!n n)))
Line 925 ⟶ 1,087:
Digits of !n(9000) = 31678
Digits of !n(10000) = 35656
</syntaxhighlight>
</lang>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule LeftFactorial do
def calc(0), do: 0
def calc(n) do
Line 947 ⟶ 1,109:
digits = LeftFactorial.calc(i) |> to_char_list |> length
IO.puts "!#{i} has #{digits} digits"
end)</langsyntaxhighlight>
 
{{out}}
Line 986 ⟶ 1,148:
=={{header|F_Sharp|F#}}==
===The Functıon===
<langsyntaxhighlight lang="fsharp">
// Generate Sequence of Left Factorials: Nigel Galloway, March 5th., 2019.
let LF=Seq.unfold(fun (Σ,n,g)->Some(Σ,(Σ+n,n*g,g+1I))) (0I,1I,1I)
</syntaxhighlight>
</lang>
===The Tasks===
;Display LF 0..10
<langsyntaxhighlight lang="fsharp">
LF |> Seq.take 11|>Seq.iter(printfn "%A")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,010 ⟶ 1,172:
</pre>
;Display LF 20..110 in steps of 10
<langsyntaxhighlight lang="fsharp">
LF |> Seq.skip 20 |> Seq.take 91 |> Seq.iteri(fun n g->if n%10=0 then printfn "%A" g)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,027 ⟶ 1,189:
</pre>
;Display the length (in decimal digits) of LF 1000 .. 10000 in steps of 1000
<langsyntaxhighlight lang="fsharp">
LF |> Seq.skip 1000 |> Seq.take 9001 |> Seq.iteri(fun n g->if n%1000=0 then printfn "%d" (string g).Length)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,043 ⟶ 1,205:
35656
</pre>
 
 
 
=={{header|Factor}}==
{{works with|Factor|0.98}}
<syntaxhighlight lang="text">USING: formatting fry io kernel math math.factorials
math.functions math.parser math.ranges sequences ;
IN: rosetta-code.left-factorials
Line 1,074 ⟶ 1,234:
: main ( -- ) part1 part2 part3 ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 1,112 ⟶ 1,272:
!10000 35656
</pre>
 
=={{header|Fōrmulæ}}==
 
In [https://wiki.formulae.org/Left_factorials this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Forth}}==
{{works with|Gforth 0.7.3}}
This solution inspired by the Fortran one.
<langsyntaxhighlight Forthlang="forth">36000 CONSTANT #DIGITS \ Enough for !10000
CREATE S #DIGITS ALLOT S #DIGITS ERASE VARIABLE S#
CREATE F #DIGITS ALLOT F #DIGITS ERASE VARIABLE F#
Line 1,171 ⟶ 1,323:
dup REPORT
dup F F# @ rot B* F# !
1+ REPEAT drop ;</langsyntaxhighlight>
{{out}}
<pre>$ gforth left-factorials.fs -e 'GO bye'
Line 1,213 ⟶ 1,365:
Because this calculation won't get far, no attempt is made to save intermediate results (such as the factorial numbers) nor develop the results progressively even though they are to be produced in sequence. Each result is computed from the start, as per the specified formulae.
 
For output, to have the exclamation mark precede the number without a gap, format sequence <code>"!",I0</code> will do, the <code>I0</code> format code being standardised in F90. However, this produces varying-length digit sequences, which will mean that the following output changes position likewise. Rather than use say <code>I20</code> for the result and have a wide gap, code <code>I0</code> will do, and to start each such number in the same place, the code <code>T6</code> will start it in column six, far enough along not to clash with the first number on the line, given that it will not be large. <langsyntaxhighlight Fortranlang="fortran"> MODULE LAIROTCAF !Calculates "left factorials".
CONTAINS !The usual suspects.
INTEGER*8 FUNCTION FACT(N) !Factorial, the ordinary.
Line 1,250 ⟶ 1,402:
WRITE (6,1) I,LFACT(I)
END DO
END</langsyntaxhighlight>
 
Output:
Line 1,271 ⟶ 1,423:
</pre>
 
Obviously, one could proceed using the services of some collection of "bignum" routines, and then the code would merely depict their uses for this problem. Since the task is to produce consecutive values, all that need be done is to maintain a S value holding the accumulated sum, and a F value for the successive factorials to be added into S. The only difficulty is to arrange the proper phasing of the starting values so that the calculation will work. Since only one multiply and one addition is needed per step, explicit code might as well be used, as follows: <langsyntaxhighlight Fortranlang="fortran">Calculates "left factorials", in sequence, and shows some.
INTEGER ENUFF,BASE !Some parameters.
PARAMETER (BASE = 10, ENUFF = 40000) !This should do.
Line 1,337 ⟶ 1,489:
END DO !If there is one, as when N > BASE.
END DO !On to the next result.
END !Ends with a new factorial that won't be used.</langsyntaxhighlight>
Output: achieved in a few seconds. A larger BASE would give a faster calculation, but would complicate the digit count.
<pre>
Line 1,376 ⟶ 1,528:
{{trans|C}}
{{libheader|GMP}}
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
#include "gmp.bi"
Line 1,418 ⟶ 1,570:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,454 ⟶ 1,606:
!9000 has 31678 digits
!10000 has 35656 digits
</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Left_factorials}}
 
'''Solution'''
 
The following function calculates the left factorial directly by its definition:
 
[[File:Fōrmulæ - Left factorials 08.png]]
 
However, the following is much faster:
 
[[File:Fōrmulæ - Left factorials 01.png]]
 
'''Test case 1. Showing left factorials from zero to ten'''
 
[[File:Fōrmulæ - Left factorials 02.png]]
 
[[File:Fōrmulæ - Left factorials 03.png]]
 
'''Test case 2. Showing left factorials from 20 to 110, by tens'''
 
[[File:Fōrmulæ - Left factorials 04.png]]
 
[[File:Fōrmulæ - Left factorials 05.png|858px]]
 
'''Test case 3. Showing length of left factorials, from 1,000 to 10,000 by thousands'''
 
[[File:Fōrmulæ - Left factorials 06.png]]
 
[[File:Fōrmulæ - Left factorials 07.png]]
 
=={{header|Frink}}==
Frink contains efficient algorithms for calculating and caching factorials and this program will work for arbitrarily-large numbers.
 
<syntaxhighlight lang="frink">leftFactorial[n] :=
{
sum = 0
for k = 0 to n-1
sum = sum + k!
 
return sum
}
 
println["Zero through ten"]
for n = 0 to 10
println["$n\t" + leftFactorial[n]]
 
println["\n20 through 110"]
for n = 20 to 110 step 10
println["$n\t" + leftFactorial[n]]
 
println["\nlength of 1000 through 10000"]
for n = 1000 to 10000 step 1000
println["$n has " + length[toString[leftFactorial[n]]] + " digits"]</syntaxhighlight>
{{out}}
<pre>
Zero through ten
0 0
1 1
2 2
3 4
4 10
5 34
6 154
7 874
8 5914
9 46234
10 409114
 
20 through 110
20 128425485935180314
30 9157958657951075573395300940314
40 20935051082417771847631371547939998232420940314
50 620960027832821612639424806694551108812720525606160920420940314
60 141074930726669571000530822087000522211656242116439949000980378746128920420940314
70 173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314
80 906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314
90 16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314
100 942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
110 145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
 
length of 1000 through 10000
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>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,501 ⟶ 1,748:
}
fmt.Println()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,520 ⟶ 1,767:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">factleftFact :: [Integer]
fact = scanl (*) 1 [1 ..]
 
leftFact :: [Integer]
leftFact = scanl (+) 0 fact
 
fact :: [Integer]
fact = scanl (*) 1 [1 ..]
 
main :: IO ()
Line 1,537 ⟶ 1,784:
, ""
, "length of 1,000 ~ 10,000 by thousands:"
, show $ (length . show . (leftFact !!)) <$> [1000,2000 .. 10000]
, ""
]</langsyntaxhighlight>
{{Out}}
<pre>0 ~ 10:
Line 1,563 ⟶ 1,810:
{{trans|D}}
The following works in both languages:
<syntaxhighlight lang="text">procedure main()
every writes(lfact(0 | !10)," ")
write()
Line 1,578 ⟶ 1,825:
every (i := !n, r +:= .f, f *:= .i)
return r
end</langsyntaxhighlight>
 
{{out}}
Line 1,604 ⟶ 1,851:
This could be made more efficient (in terms of machine time), is there a practical application for this? The more efficient machine approach would require a more specialized interface or memory dedicated to caching.
 
<langsyntaxhighlight Jlang="j">leftFact=: +/@:!@i."0</langsyntaxhighlight>
 
Task examples:
 
<langsyntaxhighlight Jlang="j"> (,. leftFact) i.11
0 0
1 1
Line 1,641 ⟶ 1,888:
8000 27749
9000 31678
10000 35656</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.math.BigInteger;
 
public class LeftFac{
Line 1,676 ⟶ 1,923:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>!0 = 0
Line 1,715 ⟶ 1,962:
 
'''Using builtin arithmetic''':
<langsyntaxhighlight lang="jq">def left_factorial:
reduce range(1; .+1) as $i
# state: [i!, !i]
([1,0]; .[1] += .[0] | .[0] *= $i)
| .[1];</langsyntaxhighlight>
 
'''Using BigInt.jq''':
Line 1,728 ⟶ 1,975:
To compute the lengths of the decimal representation without having to recompute !n,
we also define left_factorial_lengths(gap) to emit [n, ( !n|length) ] when n % gap == 0.
<langsyntaxhighlight lang="jq">import "BigInt" as BigInt;
 
# integer input
Line 1,748 ⟶ 1,995:
| (.[1] | tostring | length) as $lf
| if $i % gap == 0 then .[2] += [[$i, $lf]] else . end)
| .[2];</langsyntaxhighlight>
 
'''The specific tasks''':
<langsyntaxhighlight lang="sh">((range(0;11), (range(2; 12) * 10)) | "\(.): \(long_left_factorial)"),
 
(10000 | long_left_factorial_lengths(1000) | .[] | "\(.[0]): length is \(.[1])")</langsyntaxhighlight>
 
{{out}}
(scrollable)
<div style="overflow:scroll; height:200px;">
<langsyntaxhighlight lang="sh">$ jq -r -n -L . -f Long_left_factorial.jq
0: 0
1: 1
Line 1,789 ⟶ 2,036:
8000: length is 27749
9000: length is 31678
10000: length is 35656</langsyntaxhighlight>
</div>
 
Line 1,795 ⟶ 2,042:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">leftfactorial(n::Integer) = n ≤ 0 ? zero(n) : sum(factorial, 0:n-1)
 
@show leftfactorial.(0:10)
@show ndigits.(leftfactorial.(big.(1000:1000:10_000)))</langsyntaxhighlight>
 
{{out}}
Line 1,805 ⟶ 2,052:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
import java.math.BigInteger
Line 1,827 ⟶ 2,074:
for (i in 1000..10000 step 1000)
println("!${i.toString().padEnd(5)} has ${leftFactorial(i).toString().length} digits")
}</langsyntaxhighlight>
 
{{out}}
Line 1,869 ⟶ 2,116:
 
The code can be tested in this wiki page: http://lambdaway.free.fr/lambdawalks/?view=left_factorial
<langsyntaxhighlight lang="scheme">
'''1) defining !n'''
 
Line 1,950 ⟶ 2,197:
Digits of !n(10000) = 35656
 
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
Takes about five seconds...
<langsyntaxhighlight Lualang="lua">-- Lua bindings for GNU bc
require("bc")
 
Line 1,981 ⟶ 2,228:
for i = 1000, 10000, 1000 do
print("!" .. i .. " contains " .. #leftFac(i) .. " digits")
end</langsyntaxhighlight>
{{out}}
<pre>!0 = 0
Line 2,016 ⟶ 2,263:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">left_factorial := n -> sumadd(k!, k = 1 .. n - 1);
seq(left_factorial(i), i = 1 .. 10);
seq(left_factorial(i), i = 20 .. 110, 10);
seq(length(left_factorial(i)), i = 1000 .. 10000, 1000);</langsyntaxhighlight>
{{out}}
<pre>0, 1, 3, 9, 33, 153, 873, 5913, 46233, 409113
Line 2,027 ⟶ 2,274:
2565, 5733, 9128, 12670, 16322, 20062, 23875, 27749, 31678, 35656</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">left[n_] := left[n] = Sum[k!, {k, 0, n - 1}]
Print["left factorials 0 through 10:"]
Print[left /@ Range[0, 10] // TableForm]
Line 2,034 ⟶ 2,281:
Print[left /@ Range[20, 110, 10] // TableForm]
Print["Digits in left factorials 1,000 through 10,000, by thousands:"]
Print[Length[IntegerDigits[left[#]]] & /@ Range[1000, 10000, 1000] // TableForm]</langsyntaxhighlight>
{{out}}
<pre>left factorials 0 through 10:
Line 2,071 ⟶ 2,318:
27749
31678
35656</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
l_factorial(n):=sum(k!,k,0,n-1)$
 
/* Test cases */
makelist(l_factorial(i),i,0,10);
 
makelist(l_factorial(i),i,20,110,10);
</syntaxhighlight>
{{out}}
<pre>
[0,1,2,4,10,34,154,874,5914,46234,409114]
 
[128425485935180314,9157958657951075573395300940314,20935051082417771847631371547939998232420940314,620960027832821612639424806694551108812720525606160920420940314,141074930726669571000530822087000522211656242116439949000980378746128920420940314,173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314,906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314,16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314,942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314,145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314]
</pre>
 
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang="nim">import iterutils, bigints
 
proc lfact: iterator: BigInt =
Line 2,101 ⟶ 2,363:
echo "Digits in 1,000 through 10,000 (inclusive) by thousands:"
for i in lfact().slice(1_000, 10_000, 1_000):
echo " ", ($i).len</langsyntaxhighlight>
{{out}}
<pre>first 11:
Line 2,140 ⟶ 2,402:
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">: leftFact | i | 0 1 rot loop: i [ tuck + swap i * ] drop ;</langsyntaxhighlight>
 
{{out}}
Line 2,168 ⟶ 2,430:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">lf(n)=sum(k=0,n-1,k!);
apply(lf, [0..10])
apply(lf, 10*[2..11])
forstep(n=1000,1e4,1000,print1(#digits(lf(n))", "))</langsyntaxhighlight>
{{out}}
<pre>%1 = [0, 1, 2, 4, 10, 34, 154, 874, 5914, 46234, 409114]
Line 2,182 ⟶ 2,444:
If performance is a concern, this will run over 100x faster by replacing the line "use bigint" with "use Math::GMP qw/:constant/" (after installing that module).
 
<langsyntaxhighlight lang="perl">#!perl
use 5.010;
use strict;
Line 2,206 ⟶ 2,468:
printf "!%d has %d digits.\n", $_, length leftfact($_) for map $_*1000, 1..10;
 
</syntaxhighlight>
</lang>
 
Since I copied the printf format strings from the perl6Raku implementation,
the output from the code above is identical to the output of the perl6Raku code.
 
=={{header|Perl 6}}==
 
Implement left factorial as a prefix !. Note that this redefines the core prefix ! (not) function.
 
<lang perl6>sub prefix:<!> ($k) { (constant l = 0, |[\+] 1, (|[\*] 1..*))[$k] }
 
$ = !10000; # Pre-initialize
 
.say for ( 0 … 10, 20 … 110 ).hyper(:4batch).map: { sprintf "!%d = %s", $_, !$_ };
.say for (1000, 2000 … 10000).hyper(:4batch).map: { sprintf "!%d has %d digits.", $_, chars !$_ };</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
!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>
If you would rather not override prefix ! operator and you can live with just defining lazy lists and indexing into them, this should suffice; (and is in fact very slightly faster than the first example since it avoids routine dispatch overhead):
<lang perl6>constant leftfact = 0, |[\+] 1, (|[\*] 1..*);
 
$ = leftfact[10000]; # Pre-initialize
 
.say for ( 0 … 10, 20 … 110 ).hyper(:4batch).map: { sprintf "!%d = %s", $_, leftfact[$_] };
.say for (1000, 2000 … 10000).hyper(:4batch).map: { sprintf "!%d has %d digits.", $_, chars leftfact[$_] };</lang>
 
Same output.
 
=={{header|Phix}}==
{{trans|Lua}}
{{libheader|Phix/mpfr}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
(now over 1500 times faster than the previous bigatom version.)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<lang Phix>include mpfr.e
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
sequence lf_list
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lf_list</span>
procedure init(integer n)
<span style="color: #008080;">procedure</span> <span style="color: #000000;">init</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
mpz f = mpz_init(1)
<span style="color: #004080;">mpz</span> <span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
lf_list = repeat(f,n+1)
<span style="color: #000000;">lf_list</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
for i=1 to n do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
f = mpz_init_set(f)
<span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
mpz_mul_si(f,f,i)
<span style="color: #7060A8;">mpz_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
lf_list[i+1] = f
<span style="color: #000000;">lf_list</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">f</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
function lf(integer n, bool len=false)
<span style="color: #008080;">function</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">len</span><span style="color: #0000FF;">=</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
-- Returns left factorial of n, or it's length, as a string
<span style="color: #000080;font-style:italic;">-- Returns left factorial of n, or it's length, as a string</span>
mpz sumf = mpz_init(0)
<span style="color: #004080;">mpz</span> <span style="color: #000000;">sumf</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
for k=1 to n do mpz_add(sumf,sumf,lf_list[k]) end for
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span> <span style="color: #7060A8;">mpz_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sumf</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sumf</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lf_list</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return iff(len?sprintf("%d",mpz_sizeinbase(sumf,10))
<span style="color: #008080;">return</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">len</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_sizeinbase</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sumf</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">))</span>
:mpz_get_str(sumf))
<span style="color: #0000FF;">:</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sumf</span><span style="color: #0000FF;">)))</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
-- Main procedure
<span style="color: #000080;font-style:italic;">-- Main procedure</span>
atom t0 = time()
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
init(10000)
<span style="color: #000000;">init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">)</span>
for i=0 to 10 do printf(1,"!%d = %s\n",{i,lf(i)}) end for
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span> <span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"!%d = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)})</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for i=20 to 110 by 10 do printf(1,"!%d = %s\n",{i,lf(i)}) end for
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">20</span> <span style="color: #008080;">to</span> <span style="color: #000000;">110</span> <span style="color: #008080;">by</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span> <span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"!%d = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)})</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for i=1000 to 10000 by 1000 do printf(1,"!%d contains %s digits\n",{i,lf(i,true)}) end for
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1000</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10000</span> <span style="color: #008080;">by</span> <span style="color: #000000;">1000</span> <span style="color: #008080;">do</span> <span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"!%d contains %s digits\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)})</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"complete (%3.2fs)\n",{time()-t0})</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"complete (%3.2fs)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,312 ⟶ 2,524:
!30 = 9157958657951075573395300940314
!40 = 20935051082417771847631371547939998232420940314
!50 = 62096002783282161263...25606160920420940314 (63 digits)
!50 = 620960027832821612639424806694551108812720525606160920420940314
!60 = 14107493072666957100...78746128920420940314 (81 digits)
!60 = 141074930726669571000530822087000522211656242116439949000980378746128920420940314
!70 = 17363951180298752669...09216528920420940314 (99 digits)
!70 = 173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314
!80 = 90608958798769534653...22336528920420940314 (117 digits)
!80 = 906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314
!90 = 16695570072624210767...42336528920420940314 (137 digits)
!90 = 16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314
!100 = 94278623976582657916...42336528920420940314 (156 digits)
!100 = 942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
!110 = 14572298106158529700...42336528920420940314 (177 digits)
!110 = 145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
!1000 contains 2565 digits
!2000 contains 5733 digits
!3000 contains 9128 digits
!4000 contains 12670 digits
!5000 contains 1632316322 digits
!6000 contains 20062 digits
!7000 contains 23875 digits
Line 2,329 ⟶ 2,541:
!9000 contains 31678 digits
!10000 contains 35656 digits
complete (0.25s45s)
</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de n! (N)
(cache '(NIL) N
(if (> 2 N) 1
Line 2,350 ⟶ 2,562:
(prinl "length of 1000 - 10000")
(pril (mapcar 'length (mapcar '!n (range 1000 10000 1000))))
</syntaxhighlight>
</lang>
{{out}}
<syntaxhighlight lang="text">0-10
1
1
Line 2,386 ⟶ 2,598:
31678
35656
</syntaxhighlight>
</lang>
 
=={{header|PL/I}}==
Line 2,393 ⟶ 2,605:
Results are shown for the first 11 integers, as required;
then for the integers from 20 through 30 only, because factorials for n = 40 and larger are not possible.
<langsyntaxhighlight lang="pli">lf: procedure (n) returns (fixed decimal (31) );
declare n fixed binary;
declare (s, f) fixed (31);
Line 2,415 ⟶ 2,627:
end;
 
end left_factorials;</langsyntaxhighlight>
{{out}}
<pre>
Line 2,444 ⟶ 2,656:
=={{header|PowerShell}}==
{{works with|PowerShell|4.0}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
function left-factorial ([BigInt]$n) {
[BigInt]$k, [BigInt]$fact = ([BigInt]::Zero), ([BigInt]::One)
Line 2,471 ⟶ 2,683:
else {"!$i has $digits digit"}
}
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 2,509 ⟶ 2,721:
!9000 has 31678 digits
!10000 has 35656 digits
</pre>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<syntaxhighlight 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).</syntaxhighlight>
 
{{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>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from itertools import islice
 
def lfact():
Line 2,526 ⟶ 2,798:
print(lf)
print('Digits in 1,000 through 10,000 (inclusive) by thousands:\n %r'
% [len(str(lf)) for lf in islice(lfact(), 1000, 10001, 1000)] )</langsyntaxhighlight>
 
{{out}}
Line 2,552 ⟶ 2,824:
 
{{Trans|Haskell}}
<syntaxhighlight lang ="python">"""'''Left factorials"""'''
 
from itertools import (accumulate, chain, count, islice)
Line 2,560 ⟶ 2,832:
# leftFact :: [Integer]
def leftFact():
'''Left factorial series defined in terms of the factorial series'''
of the factorial series.
return scanl(add)(0)(
fact()'''
return accumulate(
chain([0], fact()), add
)
 
Line 2,568 ⟶ 2,842:
# fact :: [Integer]
def fact():
'''FactorialThe factorial series – a non-finite list'''.
'''
return scanl(mul)(1)(
return enumFromaccumulate(1)
chain([1], count(1)), mul
)
 
 
# TEST --------------------------- TEST -------------------------
# main :: IO ()
def main():
Line 2,598 ⟶ 2,873:
 
 
# GENERIC ------------------------- GENERIC ------------------------
 
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
Line 2,604 ⟶ 2,879:
'''Function composition.'''
return lambda f: lambda x: g(f(x))
 
 
# enumFrom :: Enum a => a -> [a]
def enumFrom(x):
'''A non-finite stream of enumerable values,
starting from the given value.'''
return count(x) if isinstance(x, int) else (
map(chr, count(ord(x)))
)
 
 
# scanl :: (b -> a -> b) -> b -> [a] -> [b]
def scanl(f):
'''scanl is like reduce, but returnsdefines a succession of
intermediate values, building from the left.'''
'''
return lambda a: lambda xs: (
def go(a):
accumulate(chain([a], xs), f)
def g(xs):
return accumulate(chain([a], xs), f)
return g
return go
 
 
Line 2,646 ⟶ 2,915:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Terms 0 thru 10 inclusive:
Line 2,665 ⟶ 2,934:
Digit counts for terms 1k through 10k (inclusive) by k:
[2565, 5733, 9128, 12670, 16322, 20062, 23875, 27749, 31678, 35656]</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ 1 swap times [ i 1+ * ] ] is ! ( n --> n )
 
[ 0 swap times [ i ! + ] ] is !n ( n --> n )
 
say "First 11 left factorials:" cr
11 times [ i^ !n echo sp ] cr
cr
say "20 through 110 (inclusive) by tens:" cr
10 times [ i^ 2 + 10 * !n echo cr ]
cr
say "Digits in 1,000 through 10,000 by thousands:" cr
10 times [ i^ 1+ 1000 * !n number$ size echo cr ]
cr</syntaxhighlight>
 
{{out}}
 
<pre>First 11 left factorials:
0 1 2 4 10 34 154 874 5914 46234 409114
 
20 through 110 (inclusive) by tens:
128425485935180314
9157958657951075573395300940314
20935051082417771847631371547939998232420940314
620960027832821612639424806694551108812720525606160920420940314
141074930726669571000530822087000522211656242116439949000980378746128920420940314
173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314
906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314
16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314
942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
 
Digits in 1,000 through 10,000 by thousands:
2565
5733
9128
12670
16322
20062
23875
27749
31678
35656</pre>
 
=={{header|R}}==
===Imperative solution===
<lang rsplus>
<syntaxhighlight lang="rsplus">library(gmp)
 
left_factorial <- function(n) {
Line 2,699 ⟶ 3,013:
cat("!",n," has ",digit_count(left_factorial(n))," digits\n", sep = "")
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,732 ⟶ 3,046:
!8000 has 27749 digits
!9000 has 31678 digits
!10000 has 35656 digits</pre>
===Vectorization solution===
</pre>
Due to vectorization, these sorts of problems are R's bread and butter. The only challenge comes from making sure that R plays nice with objects from the gmp library.
<syntaxhighlight lang="rsplus">library(gmp)
leftFact <- function(numbs)
{
#As we will never actually use the numeric values of our outputs, we will
#immediately coerce them to characters. For technical reasons to do with
#nchar misbehaving, this also makes task 3 much easier.
#As task 1 will demonstrate, the n=0 special case is covered.
sapply(numbs, function(n) as.character(sum(factorialZ(seq_len(n)-1))))
}
printer <- function(inputs) print(data.frame(Value = leftFact(inputs), row.names = paste0("!", inputs)))
 
#Task 1
printer(0:10)
#Task 2
printer(seq(20, 110, by = 10))
#Task 3
inputs<-seq(1000, 10000, by = 1000)
print(data.frame(Digits = sapply(leftFact(inputs), nchar), row.names = paste0("!", inputs)))</syntaxhighlight>
 
{{out}}
<pre>> printer(0:10)
Value
!0 0
!1 1
!2 2
!3 4
!4 10
!5 34
!6 154
!7 874
!8 5914
!9 46234
!10 409114
 
> printer(seq(20, 110, by = 10))
Value
!20 128425485935180314
!30 9157958657951075573395300940314
!40 20935051082417771847631371547939998232420940314
!50 620960027832821612639424806694551108812720525606160920420940314
!60 141074930726669571000530822087000522211656242116439949000980378746128920420940314
!70 173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314
!80 906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314
!90 16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314
!100 942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
!110 145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
 
> print(data.frame(Digits = sapply(leftFact(inputs), nchar), row.names = paste0("!", inputs)))
Digits
!1000 2565
!2000 5733
!3000 9128
!4000 12670
!5000 16322
!6000 20062
!7000 23875
!8000 27749
!9000 31678
!10000 35656</pre>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
(define ! (let ((rv# (make-hash))) (λ (n) (hash-ref! rv# n (λ () (if (= n 0) 1 (* n (! (- n 1)))))))))
 
Line 2,753 ⟶ 3,127:
"Display the length (in decimal digits) of the left factorials for:"
"1,000, 2,000 through 10,000 (inclusive), by thousands."
(pretty-format (for/list ((i (in-range 1000 10001 1000))) (add1 (order-of-magnitude (!n i))))))</langsyntaxhighlight>
 
{{out}}
Line 2,773 ⟶ 3,147:
1,000, 2,000 through 10,000 (inclusive), by thousands.
'(2565 5733 9128 12670 16322 20062 23875 27749 31678 35656)</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
Implement left factorial as a prefix !. Note that this redefines the core prefix ! (not) function.
 
<syntaxhighlight lang="raku" line>sub prefix:<!> ($k) { (constant l = 0, |[\+] 1, (|[\*] 1..*))[$k] }
 
$ = !10000; # Pre-initialize
 
.say for ( 0 … 10, 20 … 110 ).hyper(:4batch).map: { sprintf "!%d = %s", $_, !$_ };
.say for (1000, 2000 … 10000).hyper(:4batch).map: { sprintf "!%d has %d digits.", $_, chars !$_ };</syntaxhighlight>
{{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
!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>
If you would rather not override prefix ! operator and you can live with just defining lazy lists and indexing into them, this should suffice; (and is in fact very slightly faster than the first example since it avoids routine dispatch overhead):
<syntaxhighlight lang="raku" line>constant leftfact = 0, |[\+] 1, (|[\*] 1..*);
 
$ = leftfact[10000]; # Pre-initialize
 
.say for ( 0 … 10, 20 … 110 ).hyper(:4batch).map: { sprintf "!%d = %s", $_, leftfact[$_] };
.say for (1000, 2000 … 10000).hyper(:4batch).map: { sprintf "!%d has %d digits.", $_, chars leftfact[$_] };</syntaxhighlight>
 
Same output.
 
=={{header|REXX}}==
Programmer's note: &nbsp; this REXX version automatically adjusts the number of decimal digits (precision) after the factorial is calculated.
<lang rexx>/*REXX program computes/display the left factorial (or its width) of N (or range). */
 
parse arg bot top inc . /*obtain optional argumenst from the CL*/
This is possible because there will be a number of trailing zeros which allow a floating point number to be converted to an integer.
if bot=='' | bot=="," then bot= 1 /*Not specified: Then use the default.*/
<syntaxhighlight lang="rexx">/*REXX program computes/display the left factorial (or its dec. width) of N (or a range)*/
if top=='' | top=="," then top=bot /* " " " " " " */
ifparse inc=''arg bot |top inc=="," . then inc= 1 /* " " " " " /*obtain "optional arguments from the CL*/
tellDigs=if (bot<0) =='' | bot=="," then bot= 1 /*ifNot BOTspecified: < 0,Then use the only show # of digitsdefault. */
botif top=abs(bot)='' | top=="," then top= bot /* " " " /*use the" │bot│ " for the DO" loop. */
@=if inc=''left ! of| 'inc=="," then inc= 1 /* " " /*a handy" " " literal used" in the display. */
wtell=length(H) bot<0 /*widthif ofBOT the< largest0, number request only show # of digits. */
bot= abs(bot) do j=bot to top by inc /*traipseuse throughthe │bot│ for the numbers requested DO loop. */
w= length(top) if tellDigs then say @ right(j,w) " ───► " length(L!(j)) ' digits' /*width of the largest number request. */
do j=bot to top by inc else say @ right(j,w) /*traipse through "the ───►numbers " L!(j)requested*/
if endtell then /*j*/say 'left ! of ' right(j,w) " ───► " /* [↑] show either length(L!(j)) or # of' digits*/'
exit else say 'left ! of ' right(j,w) " ───► " /*stick a fork in it, we're all done. */L!(j)
end /*j*/ /* [↑] show either L! or # of digits*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
L!: procedure; parse arg x .; if x<3 then return x; s $= 4; != 2 /*some shortcuts. */
!=2; do f=3 to x-1 do #=3 to x-1; != ! * # /*compute L! for all numbers ─── ► X.*/
!=!*f if pos(., !)\==0 then numeric digits digits() * 3 % 2 /*computebump intermediatedec. factorialdigs. */
if pos(.,!)\ $==0 then$ + ! numeric digits digits()*1.5%1 /*bumpadd decimalthe digitsfactorial ───► L! sum. */
s=s+! end /*#*/ /*add the[↑] factorial ───►handles gihugeic L!numbers. sum. */
return $ end /*f*/ /*return [↑]the handles gihugeic numbers.sum (L!) to the invoker.*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 0 &nbsp; 10 </tt>}}
return s /*return the sum (L!) to the invoker.*/</lang>
'''output''' &nbsp; when using the input: &nbsp; <tt> 0 &nbsp; 10 </tt>
<pre>
left ! of 0 ───► 0
Line 2,811 ⟶ 3,239:
left ! of 10 ───► 409114
</pre>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; &nbsp; &nbsp; <tt> 20 &nbsp; 110 &nbsp; 10 </tt>}}
<pre>
left ! of 20 ───► 128425485935180314
Line 2,824 ⟶ 3,252:
left ! of 110 ───► 145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
</pre>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> -1000 &nbsp; 10000 &nbsp; 1000 </tt>}}
<pre>
left ! of 1000 ───► 2565 digits
Line 2,839 ⟶ 3,267:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
a = leftFact(0,10,1)
see "" + a + nl
Line 2,855 ⟶ 3,283:
else see "" + i + " " + leftFact + nl ok
next
</syntaxhighlight>
</lang>
 
=={{header|RPL}}==
For small values of n, the built-in number type can support the job:
≪ '''IF''' DUP '''THEN''' 0 1 ROT 1 - '''FOR''' k k FACT + '''NEXT END''' ≫ ‘<span style="color:blue">LFACT</span>’ STO
≪ { } 0 10 '''FOR''' n n <span style="color:blue">LFACT</span> + '''NEXT''' ≫ EVAL
 
'''Output:'''
1: { 0 1 2 4 10 34 154 874 5914 46234 409114 }
For 20 through 110 (inclusive) by tens, we need a kind of BigInt library if using a RPL version from the previous century. <code>ADDbig</code> and <code>MULbig</code> are defined at [[Long multiplication#RPL|Long multiplication]].
Calculation is optimized by using the recursive formula: <code>!(n+1) = !n * n + 1</code>
≪ "1" SWAP
'''WHILE''' DUP 1 > '''REPEAT'''
1 - DUP →STR ROT <span style="color:blue">MULbig</span> "1" <span style="color:blue"><span style="color:blue">ADDbig</span></span> SWAP
'''END''' DROP
≫ ‘<span style="color:blue">LFACTbig</span>’ STO
≪ 20 110 '''FOR''' n n <span style="color:blue">LFACTbig</span> 10 '''STEP''' ≫ EVAL
{{out}}
<pre>
10: "128425485935180314"
9: "9157958657951075573395300940314"
8: "20935051082417771847631371547939998232420940314"
7: "620960027832821612639424806694551108812720525606160920420940314"
6: "141074930726669571000530822087000522211656242116439949000980378746128920420940314"
5: "173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314"
4: "906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314"
3: "16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314"
2: "942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314"
1: "145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314"
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">left_fact = Enumerator.new do |y|
f, lf = 1, 0
1.step do |n|
Line 2,865 ⟶ 3,324:
f *= n
end
end</langsyntaxhighlight>
'''Test:'''
<langsyntaxhighlight lang="ruby">tens = 20.step(110, 10)
thousands = 1000.step(10_000, 1000)
 
Line 2,878 ⟶ 3,337:
puts "!#{n} has #{lf.to_s.size} digits"
end
end</langsyntaxhighlight>
{{out}}
<pre>!0 = 0
Line 2,914 ⟶ 3,373:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight Runbasiclang="runbasic">a = lftFct(0,10,1)
a = lftFct(20,110,10)
a = lftFct(1000,10000,1000)
Line 2,933 ⟶ 3,392:
end if
next i
end function</langsyntaxhighlight>Output:
<pre>------ From 0 --To-> 10 Step 1 -------
0 1
Line 2,972 ⟶ 3,431:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
<lang Rust>
#[cfg(target_pointer_width = "64")]
type USingle = u32;
Line 3,096 ⟶ 3,555:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,130 ⟶ 3,589:
!9000 has 31678 digits.
!10000 has 35656 digits.
</pre>
 
===Faster alternative===
This solution uses the arbitrary precision integers from the rug crate,
which uses GMP under the covers.
<syntaxhighlight lang="rust">// [dependencies]
// rug = "1.9"
 
fn left_factorials() -> impl std::iter::Iterator<Item = rug::Integer> {
use rug::Integer;
let mut factorial = Integer::from(1);
let mut next = Integer::from(0);
let mut n = 1;
std::iter::from_fn(move || {
let result = next.clone();
next += &factorial;
factorial *= n;
n += 1;
Some(result)
})
}
 
fn main() {
let mut lf = left_factorials().take(10001).enumerate();
println!("Left factorials 0 through 10:");
for (i, n) in lf.by_ref().take(11) {
println!("!{} = {}", i, n);
}
println!("Left factorials 20 through 110, by tens:");
for (i, n) in lf.by_ref().take(100).skip(9).step_by(10) {
println!("!{} = {}", i, n);
}
println!("Lengths of left factorials 1000 through 10000, by thousands:");
for (i, n) in lf.skip(1000 - 111).step_by(1000) {
println!("length of !{} = {}", i, n.to_string().len());
}
}</syntaxhighlight>
 
{{out}}
<pre>
Left factorials 0 through 10:
!0 = 0
!1 = 1
!2 = 2
!3 = 4
!4 = 10
!5 = 34
!6 = 154
!7 = 874
!8 = 5914
!9 = 46234
!10 = 409114
Left factorials 20 through 110, by tens:
!20 = 128425485935180314
!30 = 9157958657951075573395300940314
!40 = 20935051082417771847631371547939998232420940314
!50 = 620960027832821612639424806694551108812720525606160920420940314
!60 = 141074930726669571000530822087000522211656242116439949000980378746128920420940314
!70 = 173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314
!80 = 906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314
!90 = 16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314
!100 = 942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
!110 = 145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
Lengths of left factorials 1000 through 10000, by thousands:
length of !1000 = 2565
length of !2000 = 5733
length of !3000 = 9128
length of !4000 = 12670
length of !5000 = 16322
length of !6000 = 20062
length of !7000 = 23875
length of !8000 = 27749
length of !9000 = 31678
length of !10000 = 35656
</pre>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">object LeftFactorial extends App {
 
// this part isn't really necessary, it just shows off Scala's ability
Line 3,153 ⟶ 3,686:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,196 ⟶ 3,729:
and (iota 5 100 2) produces a list (100 102 104 106 108)
 
<langsyntaxhighlight lang="scheme">
(import (scheme base) ;; library imports in R7RS style
(scheme write)
Line 3,224 ⟶ 3,757:
(lambda (i) (show i (string-length (number->string (left-factorial i)))))
(iota 10 1000 1000))
</syntaxhighlight>
</lang>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "bigint.s7i";
 
Line 3,262 ⟶ 3,795:
end for;
writeln;
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,295 ⟶ 3,828:
=={{header|Sidef}}==
Built-in:
<langsyntaxhighlight lang="ruby">say 20.of { .left_factorial }</langsyntaxhighlight>
 
Straightforward:
<langsyntaxhighlight lang="ruby">func left_factorial(n) {
^n -> sum { _! }
}</langsyntaxhighlight>
 
Alternatively, using ''Range.reduce()'':
<langsyntaxhighlight lang="ruby">func left_factorial(n) {
^n -> reduce({ |a,b| a + b! }, 0)
}</langsyntaxhighlight>
 
A faster approach:
<langsyntaxhighlight lang="ruby">func left_factorial(n) {
static cached = 0
static factorial = 1
Line 3,325 ⟶ 3,858:
 
leftfact
}</langsyntaxhighlight>
 
Completing the task:
<langsyntaxhighlight lang="ruby">for n in (0..10, 20..110 `by` 10) {
printf("!%d = %s\n", n, left_factorial(n))
}
Line 3,334 ⟶ 3,867:
for n in (1000..10000 `by` 1000) {
printf("!%d has %d digits.\n", n, left_factorial(n).len)
}</langsyntaxhighlight>
 
{{out}}
Line 3,368 ⟶ 3,901:
!9000 has 31678 digits.
!10000 has 35656 digits.
</pre>
 
=={{header|Standard ML}}==
<syntaxhighlight lang="sml">
(* reuse earlier factorial calculations in dfac, apply to listed arguments in cumlfac *)
(* example: left factorial n, is #3 (dfac (0,n-1,1,1) ) *)
(* output list contains (number, factorial, left factorial) *)
(* tested in PolyML *)
 
 
val store = ref 0;
 
val rec dfac = fn
(from,to,acc,cm) => if from = to then (from,acc,cm) else (store:=(from+1)*acc;dfac (from+1,to,!store,!store+cm ) );
 
val rec cumlfac = fn
(x::y::rm) => x :: cumlfac ( dfac (#1 x, #1 y, #2 x, #3 x) :: rm ) |
rm =>rm ;
 
val arguments = List.tabulate (10,fn 0=>(0,1,1)|i=>(i,0,0)) @
List.tabulate (10,fn i=> (10*i+19,0,0) ) @
List.tabulate ( 10,fn i=> (1000*i+999,0,0));
 
val result = (~1,0,0)::(cumlfac arguments);
 
(* done *)
(* display: *)
 
List.app (fn triple :int*int*int =>
print(Int.toString (1+ #1 triple ) ^ " : " ^ Int.fmt StringCvt.DEC (#3 triple ) ^" \n" )
) (List.take(result,21) ) ;
List.app (fn triple :int*int*int =>
print( Int.toString (1+ #1 triple ) ^ " : " ^ Int.toString (size(Int.toString (#3 triple ))) ^" \n" ) ) (List.drop(result,21) );
</syntaxhighlight>
{{out}}
<pre>
time poly --script thisscript
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
1000 : 2565
2000 : 5733
3000 : 9128
4000 : 12670
5000 : 16322
6000 : 20062
7000 : 23875
8000 : 27749
9000 : 31678
10000 : 35656
(CPU 2.1Ghz:) 0.36 real 0.29 user 0.08 sys
</pre>
 
Line 3,374 ⟶ 3,976:
{{libheader|AttaSwift BigInt}}
 
<langsyntaxhighlight lang="swift">import BigInt
 
func factorial<T: BinaryInteger>(_ n: T) -> T {
Line 3,410 ⟶ 4,012:
for i in stride(from: BigInt(2000), through: 10_000, by: 1000) {
print("!\(i) = \((!i).description.count) digit number")
}</langsyntaxhighlight>
 
{{out}}
Line 3,449 ⟶ 4,051:
!10000 = 35656 digit number</pre>
 
=={{header|Standard ML}}==
<lang sml>
(* reuse earlier factorial calculations in dfac, apply to listed arguments in cumlfac *)
(* example: left factorial n, is #3 (dfac (0,n-1,1,1) ) *)
(* output list contains (number, factorial, left factorial) *)
(* tested in PolyML *)
 
 
val store = ref 0;
 
val rec dfac = fn
(from,to,acc,cm) => if from = to then (from,acc,cm) else (store:=(from+1)*acc;dfac (from+1,to,!store,!store+cm ) );
 
val rec cumlfac = fn
(x::y::rm) => x :: cumlfac ( dfac (#1 x, #1 y, #2 x, #3 x) :: rm ) |
rm =>rm ;
 
val arguments = List.tabulate (10,fn 0=>(0,1,1)|i=>(i,0,0)) @
List.tabulate (10,fn i=> (10*i+19,0,0) ) @
List.tabulate ( 10,fn i=> (1000*i+999,0,0));
 
val result = (~1,0,0)::(cumlfac arguments);
 
(* done *)
(* display: *)
 
List.app (fn triple :int*int*int =>
print(Int.toString (1+ #1 triple ) ^ " : " ^ Int.fmt StringCvt.DEC (#3 triple ) ^" \n" )
) (List.take(result,21) ) ;
List.app (fn triple :int*int*int =>
print( Int.toString (1+ #1 triple ) ^ " : " ^ Int.toString (size(Int.toString (#3 triple ))) ^" \n" ) ) (List.drop(result,21) );
</lang>
{{out}}
<pre>
time poly --script thisscript
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
1000 : 2565
2000 : 5733
3000 : 9128
4000 : 12670
5000 : 16322
6000 : 20062
7000 : 23875
8000 : 27749
9000 : 31678
10000 : 35656
(CPU 2.1Ghz:) 0.36 real 0.29 user 0.08 sys
</pre>
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc leftfact {n} {
set s 0
for {set i [set f 1]} {$i <= $n} {incr i} {
Line 3,532 ⟶ 4,066:
for {set i 1000} {$i <= 10000} {incr i 1000} {
puts "!$i has [string length [leftfact $i]] digits"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,566 ⟶ 4,100:
!9000 has 31678 digits
!10000 has 35656 digits
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
import "./big" for BigInt
 
var lfacts = List.filled(12, BigInt.zero)
var lfact = BigInt.one
var sum = BigInt.zero
 
for (i in 1..10) {
sum = sum + lfact
lfacts[i] = sum
lfact = lfact * i
}
System.print("Left factorials from 0 to 10:")
for (i in 0..10) System.write(" %(lfacts[i])")
 
for (i in 11..110) {
sum = sum + lfact
if (i%10 == 0) lfacts[i/10] = sum
lfact = lfact * i
}
System.print("\n\nLeft factorials from 20 to 110 by tens:")
for (i in 2..11) Fmt.print(" !$-3d -> $s", i * 10, lfacts[i])
 
for (i in 111..10000) {
sum = sum + lfact
if (i%1000 == 0) lfacts[i/1000] = sum
lfact = lfact * i
}
System.print("\nLengths of left factorals from 1000 to 10000 by thousands:")
for (i in 1..10) Fmt.print(" !$-5d -> $5s", i * 1000, lfacts[i].toString.count)</syntaxhighlight>
 
{{out}}
<pre>
Left factorials from 0 to 10:
0 1 2 4 10 34 154 874 5914 46234 409114
 
Left factorials from 20 to 110 by tens:
!20 -> 128425485935180314
!30 -> 9157958657951075573395300940314
!40 -> 20935051082417771847631371547939998232420940314
!50 -> 620960027832821612639424806694551108812720525606160920420940314
!60 -> 141074930726669571000530822087000522211656242116439949000980378746128920420940314
!70 -> 173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314
!80 -> 906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314
!90 -> 16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314
!100 -> 942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
!110 -> 145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
 
Lengths of left factorals from 1000 to 10000 by thousands:
!1000 -> 2565
!2000 -> 5733
!3000 -> 9128
!4000 -> 12670
!5000 -> 16322
!6000 -> 20062
!7000 -> 23875
!8000 -> 27749
!9000 -> 31678
!10000 -> 35656
</pre>
 
=={{header|zkl}}==
{{trans|D}}
<langsyntaxhighlight lang="zkl">var BN=Import("zklBigNum");
 
fcn leftFact(n){
[1..n].reduce(fcn(p,n,rf){ p+=rf.value; rf.set(rf.value*n); p },
BN(0),Ref(BN(1)));
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">println("First 11 left factorials:\n", [0..10].apply(leftFact));
lfs:=[20..111,10].apply(leftFact);
println(("\n20 through 110 (inclusive) by tens:\n" +
Line 3,582 ⟶ 4,180:
 
println("Digits in 1,000 through 10,000 by thousands:\n",
[0d1_000..0d10_000, 1000].pump(List,fcn(n){leftFact(n).toString().len()}));</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits