Left factorials: Difference between revisions
Content deleted Content added
ReeceGoding (talk | contribs) m →{{header|R}}: Syntax highlighting. |
Added PascalABC.NET |
||
(8 intermediate revisions by 6 users not shown) | |||
Line 49:
{{trans|D}}
<
BigInt result = 0
BigInt factorial = 1
Line 63:
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))</
{{out}}
Line 89:
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}}
<
PR precision 36000 PR
# stores left factorials in an array #
Line 156:
OD
END
</syntaxhighlight>
{{out}}
<pre>
Line 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">
#!/usr/bin/gawk -Mf
function left_factorial(num) {
Line 221 ⟶ 262:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 260 ⟶ 301:
{{works with|BBC BASIC for Windows}}
Use the 'Mapm' library.
<
Result$="0" : A$="1"
Line 270 ⟶ 311:
IF I% > 999 IF I% MOD 1000 = 0 PRINT "!";I% " has " LENFNMAPM_FormatDec(Result$,0) " digits"
NEXT
END</
{{out}}
<pre>
Line 307 ⟶ 348:
=={{header|Bracmat}}==
{{trans|D}}
<
= result factorial i
. 0:?result
Line 343 ⟶ 384:
. (=L.@(!arg:? [?L)&out$!L)
)
)</
{{out}}
<pre>First 11 left factorials:
Line 384 ⟶ 425:
=={{header|C}}==
{{libheader|GMP}}
<syntaxhighlight lang="c">
#include <stdio.h>
#include <stdlib.h>
Line 435 ⟶ 476:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 472 ⟶ 513:
=={{header|C sharp|C#}}==
<
using System;
using System.Numerics;
Line 525 ⟶ 566:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 563 ⟶ 604:
Faster Implementation
<
using System;
using System.Numerics;
Line 614 ⟶ 655:
}
}
</syntaxhighlight>
=={{header|C++}}==
<syntaxhighlight lang="cpp">
#include <vector>
#include <string>
Line 745 ⟶ 786:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 783 ⟶ 824:
===Faster alternative===
{{libheader|GMP}}
<
#include <gmpxx.h>
Line 820 ⟶ 861:
}
return 0;
}</
{{out}}
Line 861 ⟶ 902:
=={{header|Clojure}}==
<
(:gen-class))
Line 880 ⟶ 921:
(doseq [n (range 1000 10001 1000)]
(println (format "!%-5d has %5d digits" n (count (str (biginteger (left-factorial n)))))))
</syntaxhighlight>
{{Output}}
<pre>
Line 917 ⟶ 958:
=={{header|Common Lisp}}==
<
(defun fact (n)
(reduce #'* (loop for i from 1 to n collect i)))
Line 930 ⟶ 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>
{{out}}
<pre>
Line 960 ⟶ 1,001:
=={{header|D}}==
<
BigInt leftFact(in uint n) pure nothrow /*@safe*/ {
Line 977 ⟶ 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));
}</
{{out}}
<pre>First 11 left factorials:
Line 999 ⟶ 1,040:
=={{header|EchoLisp}}==
We use the 'bigint' library and memoization : (remember 'function).
<
(lib 'bigint)
(define (!n n)
Line 1,005 ⟶ 1,046:
(+ (!n (1- n)) (factorial (1- n)))))
(remember '!n)
</syntaxhighlight>
Output:
<
(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 1,046 ⟶ 1,087:
Digits of !n(9000) = 31678
Digits of !n(10000) = 35656
</syntaxhighlight>
=={{header|Elixir}}==
<
def calc(0), do: 0
def calc(n) do
Line 1,068 ⟶ 1,109:
digits = LeftFactorial.calc(i) |> to_char_list |> length
IO.puts "!#{i} has #{digits} digits"
end)</
{{out}}
Line 1,107 ⟶ 1,148:
=={{header|F_Sharp|F#}}==
===The Functıon===
<
// 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>
===The Tasks===
;Display LF 0..10
<
LF |> Seq.take 11|>Seq.iter(printfn "%A")
</syntaxhighlight>
{{out}}
<pre>
Line 1,131 ⟶ 1,172:
</pre>
;Display LF 20..110 in steps of 10
<
LF |> Seq.skip 20 |> Seq.take 91 |> Seq.iteri(fun n g->if n%10=0 then printfn "%A" g)
</syntaxhighlight>
{{out}}
<pre>
Line 1,148 ⟶ 1,189:
</pre>
;Display the length (in decimal digits) of LF 1000 .. 10000 in steps of 1000
<
LF |> Seq.skip 1000 |> Seq.take 9001 |> Seq.iteri(fun n g->if n%1000=0 then printfn "%d" (string g).Length)
</syntaxhighlight>
{{out}}
<pre>
Line 1,167 ⟶ 1,208:
=={{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,193 ⟶ 1,234:
: main ( -- ) part1 part2 part3 ;
MAIN: main</
{{out}}
<pre>
Line 1,235 ⟶ 1,276:
{{works with|Gforth 0.7.3}}
This solution inspired by the Fortran one.
<
CREATE S #DIGITS ALLOT S #DIGITS ERASE VARIABLE S#
CREATE F #DIGITS ALLOT F #DIGITS ERASE VARIABLE F#
Line 1,282 ⟶ 1,323:
dup REPORT
dup F F# @ rot B* F# !
1+ REPEAT drop ;</
{{out}}
<pre>$ gforth left-factorials.fs -e 'GO bye'
Line 1,324 ⟶ 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. <
CONTAINS !The usual suspects.
INTEGER*8 FUNCTION FACT(N) !Factorial, the ordinary.
Line 1,361 ⟶ 1,402:
WRITE (6,1) I,LFACT(I)
END DO
END</
Output:
Line 1,382 ⟶ 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: <
INTEGER ENUFF,BASE !Some parameters.
PARAMETER (BASE = 10, ENUFF = 40000) !This should do.
Line 1,448 ⟶ 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.</
Output: achieved in a few seconds. A larger BASE would give a faster calculation, but would complicate the digit count.
<pre>
Line 1,487 ⟶ 1,528:
{{trans|C}}
{{libheader|GMP}}
<
#include "gmp.bi"
Line 1,529 ⟶ 1,570:
Print
Print "Press any key to quit"
Sleep</
{{out}}
Line 1,569 ⟶ 1,610:
=={{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.
<
{
sum = 0
Line 1,597 ⟶ 1,662:
println["\nlength of 1000 through 10000"]
for n = 1000 to 10000 step 1000
println["$n has " + length[toString[leftFactorial[n]]] + " digits"]</
{{out}}
<pre>
Line 1,639 ⟶ 1,704:
=={{header|Go}}==
<
import (
Line 1,683 ⟶ 1,748:
}
fmt.Println()
}</
{{out}}
<pre>
Line 1,702 ⟶ 1,767:
=={{header|Haskell}}==
<
leftFact = scanl (+) 0 fact
Line 1,721 ⟶ 1,786:
, show $ length . show . (leftFact !!) <$> [1000,2000 .. 10000]
, ""
]</
{{Out}}
<pre>0 ~ 10:
Line 1,745 ⟶ 1,810:
{{trans|D}}
The following works in both languages:
<syntaxhighlight lang="text">procedure main()
every writes(lfact(0 | !10)," ")
write()
Line 1,760 ⟶ 1,825:
every (i := !n, r +:= .f, f *:= .i)
return r
end</
{{out}}
Line 1,786 ⟶ 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.
<
Task examples:
<
0 0
1 1
Line 1,823 ⟶ 1,888:
8000 27749
9000 31678
10000 35656</
=={{header|Java}}==
<
public class LeftFac{
Line 1,858 ⟶ 1,923:
}
}
}</
{{out}}
<pre>!0 = 0
Line 1,897 ⟶ 1,962:
'''Using builtin arithmetic''':
<
reduce range(1; .+1) as $i
# state: [i!, !i]
([1,0]; .[1] += .[0] | .[0] *= $i)
| .[1];</
'''Using BigInt.jq''':
Line 1,910 ⟶ 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.
<
# integer input
Line 1,930 ⟶ 1,995:
| (.[1] | tostring | length) as $lf
| if $i % gap == 0 then .[2] += [[$i, $lf]] else . end)
| .[2];</
'''The specific tasks''':
<
(10000 | long_left_factorial_lengths(1000) | .[] | "\(.[0]): length is \(.[1])")</
{{out}}
(scrollable)
<div style="overflow:scroll; height:200px;">
<
0: 0
1: 1
Line 1,971 ⟶ 2,036:
8000: length is 27749
9000: length is 31678
10000: length is 35656</
</div>
Line 1,977 ⟶ 2,042:
{{works with|Julia|0.6}}
<
@show leftfactorial.(0:10)
@show ndigits.(leftfactorial.(big.(1000:1000:10_000)))</
{{out}}
Line 1,987 ⟶ 2,052:
=={{header|Kotlin}}==
<
import java.math.BigInteger
Line 2,009 ⟶ 2,074:
for (i in 1000..10000 step 1000)
println("!${i.toString().padEnd(5)} has ${leftFactorial(i).toString().length} digits")
}</
{{out}}
Line 2,051 ⟶ 2,116:
The code can be tested in this wiki page: http://lambdaway.free.fr/lambdawalks/?view=left_factorial
<
'''1) defining !n'''
Line 2,132 ⟶ 2,197:
Digits of !n(10000) = 35656
</syntaxhighlight>
=={{header|Lua}}==
Takes about five seconds...
<
require("bc")
Line 2,163 ⟶ 2,228:
for i = 1000, 10000, 1000 do
print("!" .. i .. " contains " .. #leftFac(i) .. " digits")
end</
{{out}}
<pre>!0 = 0
Line 2,198 ⟶ 2,263:
=={{header|Maple}}==
<
seq(left_factorial(i), i = 1 .. 10);
seq(left_factorial(i), i = 20 .. 110, 10);
seq(length(left_factorial(i)), i = 1000 .. 10000, 1000);</
{{out}}
<pre>0, 1, 3, 9, 33, 153, 873, 5913, 46233, 409113
Line 2,210 ⟶ 2,275:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Print["left factorials 0 through 10:"]
Print[left /@ Range[0, 10] // TableForm]
Line 2,216 ⟶ 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]</
{{out}}
<pre>left factorials 0 through 10:
Line 2,254 ⟶ 2,319:
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}}
<
proc lfact: iterator: BigInt =
Line 2,282 ⟶ 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</
{{out}}
<pre>first 11:
Line 2,321 ⟶ 2,402:
=={{header|Oforth}}==
<
{{out}}
Line 2,349 ⟶ 2,430:
=={{header|PARI/GP}}==
<
apply(lf, [0..10])
apply(lf, 10*[2..11])
forstep(n=1000,1e4,1000,print1(#digits(lf(n))", "))</
{{out}}
<pre>%1 = [0, 1, 2, 4, 10, 34, 154, 874, 5914, 46234, 409114]
%2 = [128425485935180314, 9157958657951075573395300940314, 20935051082417771847631371547939998232420940314, 620960027832821612639424806694551108812720525606160920420940314, 141074930726669571000530822087000522211656242116439949000980378746128920420940314, 173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314, 906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314, 16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314, 942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314, 145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314]
2565, 5733, 9128, 12670, 16322, 20062, 23875, 27749, 31678, 35656, </pre>
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
function lfact(n: integer): biginteger;
begin
result := 0;
var fact := 1bi;
for var i := 1 to n do
begin
result += fact;
fact *= i;
end;
end;
begin
for var n := 0 to 10 do lfact(n).Print;
println;
for var n := 2 to 11 do lfact(n * 10).println;
for var n := 1 to 10 do lfact(n * 1000).tostring.length.print;
end.
</syntaxhighlight>
{{out}}
<pre>
0 1 2 4 10 34 154 874 5914 46234 409114
128425485935180314
9157958657951075573395300940314
20935051082417771847631371547939998232420940314
620960027832821612639424806694551108812720525606160920420940314
141074930726669571000530822087000522211656242116439949000980378746128920420940314
173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314
906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314
16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314
942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
2565 5733 9128 12670 16322 20062 23875 27749 31678 35656
</pre>
=={{header|Perl}}==
Line 2,363 ⟶ 2,480:
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).
<
use 5.010;
use strict;
Line 2,387 ⟶ 2,504:
printf "!%d has %d digits.\n", $_, length leftfact($_) for map $_*1000, 1..10;
</syntaxhighlight>
Since I copied the printf format strings from the Raku implementation,
Line 2,395 ⟶ 2,512:
{{trans|Lua}}
{{libheader|Phix/mpfr}}
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 2,426 ⟶ 2,543:
<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>
<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>
<!--</
{{out}}
<pre>
Line 2,464 ⟶ 2,581:
=={{header|PicoLisp}}==
<
(cache '(NIL) N
(if (> 2 N) 1
Line 2,481 ⟶ 2,598:
(prinl "length of 1000 - 10000")
(pril (mapcar 'length (mapcar '!n (range 1000 10000 1000))))
</syntaxhighlight>
{{out}}
<syntaxhighlight lang="text">0-10
1
1
Line 2,517 ⟶ 2,634:
31678
35656
</syntaxhighlight>
=={{header|PL/I}}==
Line 2,524 ⟶ 2,641:
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.
<
declare n fixed binary;
declare (s, f) fixed (31);
Line 2,546 ⟶ 2,663:
end;
end left_factorials;</
{{out}}
<pre>
Line 2,575 ⟶ 2,692:
=={{header|PowerShell}}==
{{works with|PowerShell|4.0}}
<syntaxhighlight lang="powershell">
function left-factorial ([BigInt]$n) {
[BigInt]$k, [BigInt]$fact = ([BigInt]::Zero), ([BigInt]::One)
Line 2,602 ⟶ 2,719:
else {"!$i has $digits digit"}
}
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 2,644 ⟶ 2,761:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<
leftfact(N, 0, 0, 1).
Line 2,665 ⟶ 2,782:
main:-
leftfact(10001).</
{{out}}
Line 2,703 ⟶ 2,820:
=={{header|Python}}==
<
def lfact():
Line 2,717 ⟶ 2,834:
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)] )</
{{out}}
Line 2,743 ⟶ 2,860:
{{Trans|Haskell}}
<
from itertools import (accumulate, chain, count, islice)
Line 2,834 ⟶ 2,951:
if __name__ == '__main__':
main()</
{{Out}}
<pre>Terms 0 thru 10 inclusive:
Line 2,856 ⟶ 2,973:
=={{header|Quackery}}==
<
[ 0 swap times [ i ! + ] ] is !n ( n --> n )
Line 2,868 ⟶ 2,985:
say "Digits in 1,000 through 10,000 by thousands:" cr
10 times [ i^ 1+ 1000 * !n number$ size echo cr ]
cr</
{{out}}
Line 2,901 ⟶ 3,018:
=={{header|R}}==
===Imperative solution===
<
left_factorial <- function(n) {
Line 2,932 ⟶ 3,049:
cat("!",n," has ",digit_count(left_factorial(n))," digits\n", sep = "")
}
</syntaxhighlight>
{{out}}
<pre>
Line 2,968 ⟶ 3,085:
===Vectorization solution===
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.
<
leftFact <- function(numbs)
{
Line 2,985 ⟶ 3,102:
#Task 3
inputs<-seq(1000, 10000, by = 1000)
print(data.frame(Digits = sapply(leftFact(inputs), nchar), row.names = paste0("!", inputs)))</
{{out}}
Line 3,030 ⟶ 3,147:
=={{header|Racket}}==
<
(define ! (let ((rv# (make-hash))) (λ (n) (hash-ref! rv# n (λ () (if (= n 0) 1 (* n (! (- n 1)))))))))
Line 3,046 ⟶ 3,163:
"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))))))</
{{out}}
Line 3,072 ⟶ 3,189:
Implement left factorial as a prefix !. Note that this redefines the core prefix ! (not) function.
<syntaxhighlight lang="raku"
$ = !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 !$_ };</
{{out}}
<pre>!0 = 0
Line 3,111 ⟶ 3,228:
!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"
$ = 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[$_] };</
Same output.
Line 3,124 ⟶ 3,241:
This is possible because there will be a number of trailing zeros which allow a floating point number to be converted to an integer.
<
parse arg bot top inc . /*obtain optional arguments from the CL*/
if bot=='' | bot=="," then bot= 1 /*Not specified: Then use the default.*/
Line 3,143 ⟶ 3,260:
$= $ + ! /*add the factorial ───► L! sum. */
end /*#*/ /* [↑] handles gihugeic numbers. */
return $ /*return the sum (L!) to the invoker.*/</
{{out|output|text= when using the input of: <tt> 0 10 </tt>}}
<pre>
Line 3,186 ⟶ 3,303:
=={{header|Ring}}==
<
a = leftFact(0,10,1)
see "" + a + nl
Line 3,202 ⟶ 3,319:
else see "" + i + " " + leftFact + nl ok
next
</syntaxhighlight>
=={{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}}==
<
f, lf = 1, 0
1.step do |n|
Line 3,212 ⟶ 3,360:
f *= n
end
end</
'''Test:'''
<
thousands = 1000.step(10_000, 1000)
Line 3,225 ⟶ 3,373:
puts "!#{n} has #{lf.to_s.size} digits"
end
end</
{{out}}
<pre>!0 = 0
Line 3,261 ⟶ 3,409:
=={{header|Run BASIC}}==
<
a = lftFct(20,110,10)
a = lftFct(1000,10000,1000)
Line 3,280 ⟶ 3,428:
end if
next i
end function</
<pre>------ From 0 --To-> 10 Step 1 -------
0 1
Line 3,319 ⟶ 3,467:
=={{header|Rust}}==
<syntaxhighlight lang="rust">
#[cfg(target_pointer_width = "64")]
type USingle = u32;
Line 3,443 ⟶ 3,591:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 3,482 ⟶ 3,630:
This solution uses the arbitrary precision integers from the rug crate,
which uses GMP under the covers.
<
// rug = "1.9"
Line 3,513 ⟶ 3,661:
println!("length of !{} = {}", i, n.to_string().len());
}
}</
{{out}}
Line 3,554 ⟶ 3,702:
=={{header|Scala}}==
<
// this part isn't really necessary, it just shows off Scala's ability
Line 3,574 ⟶ 3,722:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 3,617 ⟶ 3,765:
and (iota 5 100 2) produces a list (100 102 104 106 108)
<
(import (scheme base) ;; library imports in R7RS style
(scheme write)
Line 3,645 ⟶ 3,793:
(lambda (i) (show i (string-length (number->string (left-factorial i)))))
(iota 10 1000 1000))
</syntaxhighlight>
=={{header|Seed7}}==
<
include "bigint.s7i";
Line 3,683 ⟶ 3,831:
end for;
writeln;
end func;</
{{out}}
Line 3,716 ⟶ 3,864:
=={{header|Sidef}}==
Built-in:
<
Straightforward:
<
^n -> sum { _! }
}</
Alternatively, using ''Range.reduce()'':
<
^n -> reduce({ |a,b| a + b! }, 0)
}</
A faster approach:
<
static cached = 0
static factorial = 1
Line 3,746 ⟶ 3,894:
leftfact
}</
Completing the task:
<
printf("!%d = %s\n", n, left_factorial(n))
}
Line 3,755 ⟶ 3,903:
for n in (1000..10000 `by` 1000) {
printf("!%d has %d digits.\n", n, left_factorial(n).len)
}</
{{out}}
Line 3,792 ⟶ 3,940:
=={{header|Standard ML}}==
<
(* reuse earlier factorial calculations in dfac, apply to listed arguments in cumlfac *)
(* example: left factorial n, is #3 (dfac (0,n-1,1,1) ) *)
Line 3,822 ⟶ 3,970:
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>
Line 3,864 ⟶ 4,012:
{{libheader|AttaSwift BigInt}}
<
func factorial<T: BinaryInteger>(_ n: T) -> T {
Line 3,900 ⟶ 4,048:
for i in stride(from: BigInt(2000), through: 10_000, by: 1000) {
print("!\(i) = \((!i).description.count) digit number")
}</
{{out}}
Line 3,940 ⟶ 4,088:
=={{header|Tcl}}==
<
set s 0
for {set i [set f 1]} {$i <= $n} {incr i} {
Line 3,954 ⟶ 4,102:
for {set i 1000} {$i <= 10000} {incr i 1000} {
puts "!$i has [string length [leftfact $i]] digits"
}</
{{out}}
<pre>
Line 3,993 ⟶ 4,141:
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
<
import "./big" for BigInt
var lfacts = List.filled(12, BigInt.zero)
Line 4,022 ⟶ 4,170:
}
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)</
{{out}}
Line 4,056 ⟶ 4,204:
=={{header|zkl}}==
{{trans|D}}
<
fcn leftFact(n){
[1..n].reduce(fcn(p,n,rf){ p+=rf.value; rf.set(rf.value*n); p },
BN(0),Ref(BN(1)));
}</
<
lfs:=[20..111,10].apply(leftFact);
println(("\n20 through 110 (inclusive) by tens:\n" +
Line 4,068 ⟶ 4,216:
println("Digits in 1,000 through 10,000 by thousands:\n",
[0d1_000..0d10_000, 1000].pump(List,fcn(n){leftFact(n).toString().len()}));</
{{out}}
<pre>
|