Left factorials: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 215:
!10000 has 35656 digits
</pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
Line 428 ⟶ 429:
!10000 has 35656 digits
</pre>
 
=={{header|C sharp|C#}}==
<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;
}
}
}
</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>
 
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|C++}}==
Line 593 ⟶ 739:
!10000 has 35656 digits.
</pre>
 
=={{header|C sharp|C#}}==
<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;
}
}
}
</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>
 
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}}==
Line 1,043 ⟶ 1,044:
35656
</pre>
 
 
 
=={{header|Factor}}==
Line 1,112 ⟶ 1,111:
!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}}==
Line 1,455 ⟶ 1,446:
!10000 has 35656 digits
</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|Go}}==
Line 2,210 ⟶ 2,209:
Since I copied the printf format strings from the perl6 implementation,
the output from the code above is identical to the output of the perl6 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}}==
Line 2,773 ⟶ 2,720:
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.
 
<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|REXX}}==
Line 3,368:
!9000 has 31678 digits.
!10000 has 35656 digits.
</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>
 
Line 3,449 ⟶ 3,518:
!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}}==
<lang tcl>proc leftfact {n} {
10,333

edits