Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
23
Task/Left-factorials/00DESCRIPTION
Normal file
23
Task/Left-factorials/00DESCRIPTION
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
''Left factorials'', <math>!n</math>, may refer to either ''subfactorials'' or to ''factorial sums'';
|
||||
the same notation can be confusingly seen used for the two different definitions.
|
||||
|
||||
Sometimes, ''subfactorials'' (also known as ''derangements'') use any of the notations:
|
||||
:::::::* <span style="font-family:serif">!''n''`</span>
|
||||
:::::::* <math>!n'</math>
|
||||
:::::::* <span style="font-family:serif">''n''¡</span>
|
||||
|
||||
<br>This Rosetta Code task will be using this formula for ''left factorial'':
|
||||
:<math> !n = \sum_{k=0}^{n-1} k! </math>
|
||||
where
|
||||
:<math>!0 = 0</math>
|
||||
;Task
|
||||
Display the left factorials for:
|
||||
* zero through ten (inclusive)
|
||||
* 20 through 110 (inclusive) by tens
|
||||
Display the length (in decimal digits) of the left factorials for:
|
||||
* 1,000, 2,000 through 10,000 (inclusive), by thousands.
|
||||
;Also see
|
||||
* The OEIS entry: [[http://oeis.org/A003422 A003422 left factorials]]
|
||||
* The MathWorld entry: [[http://mathworld.wolfram.com/LeftFactorial.html left factorial]]
|
||||
* The MathWorld entry: [[http://mathworld.wolfram.com/FactorialSums.html factorial sums]]
|
||||
* The MathWorld entry: [[http://mathworld.wolfram.com/Subfactorial.html subfactorial]]
|
||||
3
Task/Left-factorials/00META.yaml
Normal file
3
Task/Left-factorials/00META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
category:
|
||||
- Mathematics
|
||||
37
Task/Left-factorials/Bracmat/left-factorials.bracmat
Normal file
37
Task/Left-factorials/Bracmat/left-factorials.bracmat
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
( ( leftFact
|
||||
= result factorial i
|
||||
. 0:?result
|
||||
& 1:?factorial
|
||||
& 0:?i
|
||||
& whl
|
||||
' ( !i+1:~>!arg:?i
|
||||
& !factorial+!result:?result
|
||||
& !factorial*!i:?factorial
|
||||
)
|
||||
& !result
|
||||
)
|
||||
& ( iterate
|
||||
= from to step c fun
|
||||
. !arg:(?from.?to.?step.?fun)
|
||||
& !from+-1*!step:?from
|
||||
& !step:?c
|
||||
& whl
|
||||
' ( !step+!from:~>!to:?from
|
||||
& !fun$(leftFact$!from)
|
||||
)
|
||||
&
|
||||
)
|
||||
& out$"First 11 left factorials:"
|
||||
& iterate$(0.10.1.out)
|
||||
& out$"
|
||||
20 through 110 (inclusive) by tens:"
|
||||
& iterate$(20.110.10.out)
|
||||
& out$"
|
||||
Digits in 1,000 through 10,000 by thousands:"
|
||||
& iterate
|
||||
$ ( 1000
|
||||
. 10000
|
||||
. 1000
|
||||
. (=L.@(!arg:? [?L)&out$!L)
|
||||
)
|
||||
)
|
||||
50
Task/Left-factorials/C/left-factorials.c
Normal file
50
Task/Left-factorials/C/left-factorials.c
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <gmp.h>
|
||||
|
||||
void mpz_left_fac_ui(mpz_t rop, unsigned long op)
|
||||
{
|
||||
mpz_t t1;
|
||||
mpz_init_set_ui(t1, 1);
|
||||
mpz_set_ui(rop, 0);
|
||||
|
||||
size_t i;
|
||||
for (i = 1; i <= op; ++i) {
|
||||
mpz_add(rop, rop, t1);
|
||||
mpz_mul_ui(t1, t1, i);
|
||||
}
|
||||
|
||||
mpz_clear(t1);
|
||||
}
|
||||
|
||||
size_t mpz_digitcount(mpz_t op)
|
||||
{
|
||||
/* mpz_sizeinbase can not be trusted to give accurate base 10 length */
|
||||
char *t = mpz_get_str(NULL, 10, op);
|
||||
size_t ret = strlen(t);
|
||||
free(t);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
mpz_t t;
|
||||
mpz_init(t);
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i <= 110; ++i) {
|
||||
if (i <= 10 || i % 10 == 0) {
|
||||
mpz_left_fac_ui(t, i);
|
||||
gmp_printf("!%u = %Zd\n", i, t);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 1000; i <= 10000; i += 1000) {
|
||||
mpz_left_fac_ui(t, i);
|
||||
printf("!%u has %u digits\n", i, mpz_digitcount(t));
|
||||
}
|
||||
|
||||
mpz_clear(t);
|
||||
return 0;
|
||||
}
|
||||
18
Task/Left-factorials/D/left-factorials.d
Normal file
18
Task/Left-factorials/D/left-factorials.d
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import std.stdio, std.bigint, std.range, std.algorithm, std.conv;
|
||||
|
||||
BigInt leftFact(in uint n) pure nothrow /*@safe*/ {
|
||||
BigInt result = 0, factorial = 1;
|
||||
foreach (immutable i; 1 .. n + 1) {
|
||||
result += factorial;
|
||||
factorial *= i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln("First 11 left factorials:\n", 11.iota.map!leftFact);
|
||||
writefln("\n20 through 110 (inclusive) by tens:\n%(%s\n%)",
|
||||
iota(20, 111, 10).map!leftFact);
|
||||
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));
|
||||
}
|
||||
45
Task/Left-factorials/Go/left-factorials.go
Normal file
45
Task/Left-factorials/Go/left-factorials.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Print("!0 through !10: 0")
|
||||
one := big.NewInt(1)
|
||||
n := big.NewInt(1)
|
||||
f := big.NewInt(1)
|
||||
l := big.NewInt(1)
|
||||
next := func() { f.Mul(f, n); l.Add(l, f); n.Add(n, one) }
|
||||
for ; ; next() {
|
||||
fmt.Print(" ", l)
|
||||
if n.Int64() == 10 {
|
||||
break
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
for {
|
||||
for i := 0; i < 10; i++ {
|
||||
next()
|
||||
}
|
||||
fmt.Printf("!%d: %d\n", n, l)
|
||||
if n.Int64() == 110 {
|
||||
break
|
||||
}
|
||||
}
|
||||
fmt.Println("Lengths of !1000 through !10000 by thousands:")
|
||||
for i := 110; i < 1000; i++ {
|
||||
next()
|
||||
}
|
||||
for {
|
||||
fmt.Print(" ", len(l.String()))
|
||||
if n.Int64() == 10000 {
|
||||
break
|
||||
}
|
||||
for i := 0; i < 1000; i++ {
|
||||
next()
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
18
Task/Left-factorials/Haskell/left-factorials.hs
Normal file
18
Task/Left-factorials/Haskell/left-factorials.hs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
fact :: [Integer]
|
||||
fact = scanl (*) 1 [1..]
|
||||
|
||||
leftFact :: [Integer]
|
||||
leftFact = scanl (+) 0 fact
|
||||
|
||||
main = do
|
||||
putStrLn "0 ~ 10:"
|
||||
putStrLn $ show $ map (\n -> leftFact !! n) [0..10]
|
||||
putStrLn ""
|
||||
|
||||
putStrLn "20 ~ 110 by tens:"
|
||||
putStrLn $ unlines $ map show $ map (\n -> leftFact !! n) [20,30..110]
|
||||
putStrLn ""
|
||||
|
||||
putStrLn "length of 1,000 ~ 10,000 by thousands:"
|
||||
putStrLn $ show $ map (\n -> length $ show $ leftFact !! n) [1000,2000..10000]
|
||||
putStrLn ""
|
||||
16
Task/Left-factorials/Icon/left-factorials.icon
Normal file
16
Task/Left-factorials/Icon/left-factorials.icon
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
procedure main()
|
||||
every writes(lfact(0 | !10)," ")
|
||||
write()
|
||||
write()
|
||||
every write(lfact(20 to 110 by 10))
|
||||
write()
|
||||
every writes(*lfact(1000 to 10000 by 1000)," ")
|
||||
write()
|
||||
end
|
||||
|
||||
procedure lfact(n)
|
||||
r := 0
|
||||
f := 1
|
||||
every (i := !n, r +:= .f, f *:= .i)
|
||||
return r
|
||||
end
|
||||
1
Task/Left-factorials/J/left-factorials-1.j
Normal file
1
Task/Left-factorials/J/left-factorials-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
leftFact=: +/@:!@i."0
|
||||
34
Task/Left-factorials/J/left-factorials-2.j
Normal file
34
Task/Left-factorials/J/left-factorials-2.j
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
(,. leftFact) i.11
|
||||
0 0
|
||||
1 1
|
||||
2 2
|
||||
3 4
|
||||
4 10
|
||||
5 34
|
||||
6 154
|
||||
7 874
|
||||
8 5914
|
||||
9 46234
|
||||
10 409114
|
||||
(,. leftFact) 10*2+i.10x
|
||||
20 128425485935180314
|
||||
30 9157958657951075573395300940314
|
||||
40 20935051082417771847631371547939998232420940314
|
||||
50 620960027832821612639424806694551108812720525606160920420940314
|
||||
60 141074930726669571000530822087000522211656242116439949000980378746128920420940314
|
||||
70 173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314
|
||||
80 906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314
|
||||
90 16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314
|
||||
100 942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
|
||||
110 145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
|
||||
(,. #@":@leftFact) 1000*1+i.10x
|
||||
1000 2565
|
||||
2000 5733
|
||||
3000 9128
|
||||
4000 12670
|
||||
5000 16322
|
||||
6000 20062
|
||||
7000 23875
|
||||
8000 27749
|
||||
9000 31678
|
||||
10000 35656
|
||||
33
Task/Left-factorials/Java/left-factorials.java
Normal file
33
Task/Left-factorials/Java/left-factorials.java
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import java.math.BigInteger;
|
||||
|
||||
public class LeftFac{
|
||||
public static BigInteger factorial(BigInteger n){
|
||||
BigInteger ans = BigInteger.ONE;
|
||||
for(BigInteger x = BigInteger.ONE; x.compareTo(n) <= 0; x = x.add(BigInteger.ONE)){
|
||||
ans = ans.multiply(x);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
public static BigInteger leftFact(BigInteger n){
|
||||
BigInteger ans = BigInteger.ZERO;
|
||||
for(BigInteger k = BigInteger.ZERO; k.compareTo(n.subtract(BigInteger.ONE)) <= 0; k = k.add(BigInteger.ONE)){
|
||||
ans = ans.add(factorial(k));
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
for(int i = 0; i <= 10; i++){
|
||||
System.out.println("!" + i + " = " + leftFact(BigInteger.valueOf(i)));
|
||||
}
|
||||
|
||||
for(int i = 20; i <= 110; i += 10){
|
||||
System.out.println("!" + i + " = " + leftFact(BigInteger.valueOf(i)));
|
||||
}
|
||||
|
||||
for(int i = 1000; i <= 10000; i += 1000){
|
||||
System.out.println("!" + i + " has " + leftFact(BigInteger.valueOf(i)).toString().length() + " digits");
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Task/Left-factorials/Julia/left-factorials.julia
Normal file
2
Task/Left-factorials/Julia/left-factorials.julia
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
leftfactorial(n::Integer) = n <= 0 ? zero(n) : sum(factorial, 0:n-1)
|
||||
@vectorize_1arg Integer leftfactorial
|
||||
7
Task/Left-factorials/Mathematica/left-factorials.math
Normal file
7
Task/Left-factorials/Mathematica/left-factorials.math
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
left[n_] := left[n] = Sum[k!, {k, 0, n - 1}]
|
||||
Print["left factorials 0 through 10:"]
|
||||
Print[left /@ Range[0, 10] // TableForm]
|
||||
Print["left factorials 20 through 110, by tens:"]
|
||||
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]
|
||||
4
Task/Left-factorials/PARI-GP/left-factorials.pari
Normal file
4
Task/Left-factorials/PARI-GP/left-factorials.pari
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
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))", "))
|
||||
23
Task/Left-factorials/PL-I/left-factorials.pli
Normal file
23
Task/Left-factorials/PL-I/left-factorials.pli
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
lf: procedure (n) returns (fixed decimal (31) );
|
||||
declare n fixed binary;
|
||||
declare (s, f) fixed (31);
|
||||
declare (i, j) fixed;
|
||||
|
||||
s = 0;
|
||||
do i = n-1 to 0 by -1;
|
||||
f = 1;
|
||||
do j = i to 1 by -1;
|
||||
f = f * j;
|
||||
end;
|
||||
s = s + f;
|
||||
end;
|
||||
return (s);
|
||||
end lf;
|
||||
|
||||
declare n fixed binary;
|
||||
|
||||
do n = 0 to 10, 20 to 30;
|
||||
put skip list ('Left factorial of ' || n || '=' || lf(n) );
|
||||
end;
|
||||
|
||||
end left_factorials;
|
||||
7
Task/Left-factorials/Perl-6/left-factorials-1.pl6
Normal file
7
Task/Left-factorials/Perl-6/left-factorials-1.pl6
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
multi sub postfix:<!> (0) { 1 };
|
||||
multi sub postfix:<!> ($n) { [*] 1 .. $n };
|
||||
multi sub prefix:<!> (0) { 0 };
|
||||
multi sub prefix:<!> ($k) { [+] (^$k).map: { $_! } }
|
||||
|
||||
printf "!%d = %s\n", $_, !$_ for ^11, 20, 30 ... 110;
|
||||
printf "!%d has %d digits.\n", $_, (!$_).chars for 1000, 2000 ... 10000;
|
||||
5
Task/Left-factorials/Perl-6/left-factorials-2.pl6
Normal file
5
Task/Left-factorials/Perl-6/left-factorials-2.pl6
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
constant fact = 1, [\*] 1..*;
|
||||
constant leftfact = 0, [\+] fact;
|
||||
|
||||
printf "!%d = %s\n", $_, leftfact[$_] for 0 ... 10, 20 ... 110;
|
||||
printf "!%d has %d digits.\n", $_, leftfact[$_].chars for 1000, 2000 ... 10000;
|
||||
1
Task/Left-factorials/Perl-6/left-factorials-3.pl6
Normal file
1
Task/Left-factorials/Perl-6/left-factorials-3.pl6
Normal file
|
|
@ -0,0 +1 @@
|
|||
constant leftfact = 0, [\+] 1, [\*] 1..*;
|
||||
23
Task/Left-factorials/Perl/left-factorials.pl
Normal file
23
Task/Left-factorials/Perl/left-factorials.pl
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#!perl
|
||||
use 5.010;
|
||||
use strict;
|
||||
use warnings;
|
||||
use bigint;
|
||||
|
||||
sub leftfact {
|
||||
my ($n) = @_;
|
||||
state $cached = 0;
|
||||
state $factorial = 1;
|
||||
state $leftfact = 0;
|
||||
if( $n < $cached ) {
|
||||
($cached, $factorial, $leftfact) = (0, 1, 0);
|
||||
}
|
||||
while( $n > $cached ) {
|
||||
$leftfact += $factorial;
|
||||
$factorial *= ++$cached;
|
||||
}
|
||||
return $leftfact;
|
||||
}
|
||||
|
||||
printf "!%d = %s\n", $_, leftfact($_) for 0 .. 10, map $_*10, 2..11;
|
||||
printf "!%d has %d digits.\n", $_, length leftfact($_) for map $_*1000, 1..10;
|
||||
15
Task/Left-factorials/Python/left-factorials.py
Normal file
15
Task/Left-factorials/Python/left-factorials.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
from itertools import islice
|
||||
|
||||
def lfact():
|
||||
yield 0
|
||||
fact, summ, n = 1, 0, 1
|
||||
while 1:
|
||||
fact, summ, n = fact*n, summ + fact, n + 1
|
||||
yield summ
|
||||
|
||||
print('first 11:\n %r' % [lf for i, lf in zip(range(11), lfact())])
|
||||
print('20 through 110 (inclusive) by tens:')
|
||||
for lf in islice(lfact(), 20, 111, 10):
|
||||
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)] )
|
||||
1
Task/Left-factorials/README
Normal file
1
Task/Left-factorials/README
Normal file
|
|
@ -0,0 +1 @@
|
|||
Data source: http://rosettacode.org/wiki/Left_factorials
|
||||
22
Task/Left-factorials/REXX/left-factorials.rexx
Normal file
22
Task/Left-factorials/REXX/left-factorials.rexx
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/*REXX pgm computes/shows the left factorial (or width) of N (or range).*/
|
||||
parse arg bot top inc . /*obtain optional args from C.L. */
|
||||
if bot=='' then bot=1 /*BOT defined? Then use default.*/
|
||||
td= bot<0 /*if BOT < 0, only show # digs.*/
|
||||
bot=abs(bot) /*use the |bot| for the DO loop.*/
|
||||
if top=='' then top=bot /* " " top " " " " */
|
||||
if inc='' then inc=1 /* " " inc " " " " */
|
||||
@='left ! of ' /*a literal used in the display. */
|
||||
w=length(H) /*width of largest number request*/
|
||||
do j=bot to top by inc /*traipse through #'s requested.*/
|
||||
if td then say @ right(j,w) " ───► " length(L!(j)) ' digits'
|
||||
else say @ right(j,w) " ───► " L!(j)
|
||||
end /*j*/ /* [↑] show either L! or #digits*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────L! subroutine───────────────────────*/
|
||||
L!: procedure; parse arg x .; if x<3 then return x; s=4 /*shortcuts.*/
|
||||
!=2; do f=3 to x-1 /*compute L! for all numbers───►X*/
|
||||
!=!*f /*compute intermediate factorial.*/
|
||||
if pos(.,!)\==0 then numeric digits digits()*1.5%1 /*bump digs.*/
|
||||
s=s+! /*add the factorial ───► L! sum.*/
|
||||
end /*f*/ /* [↑] handles gi-hugeic numbers*/
|
||||
return s /*return the sum (L!) to invoker.*/
|
||||
17
Task/Left-factorials/Racket/left-factorials.rkt
Normal file
17
Task/Left-factorials/Racket/left-factorials.rkt
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#lang racket
|
||||
(define ! (let ((rv# (make-hash))) (λ (n) (hash-ref! rv# n (λ () (if (= n 0) 1 (* n (! (- n 1)))))))))
|
||||
|
||||
(define (!n n)
|
||||
;; note that in-range n is from 0 to n-1 inclusive
|
||||
(for/sum ((k (in-range n))) (! k)))
|
||||
|
||||
(define (dnl. s) (for-each displayln s))
|
||||
(dnl
|
||||
"Display the left factorials for:"
|
||||
"zero through ten (inclusive)"
|
||||
(pretty-format (for/list ((i (in-range 0 (add1 10)))) (!n i)))
|
||||
"20 through 110 (inclusive) by tens"
|
||||
(pretty-format (for/list ((i (in-range 20 (add1 110) 10))) (!n i)))
|
||||
"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))))))
|
||||
9
Task/Left-factorials/Ruby/left-factorials-1.rb
Normal file
9
Task/Left-factorials/Ruby/left-factorials-1.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
left_fact = Enumerator.new do |y|
|
||||
n, f, lf = 0, 1, 0
|
||||
loop do
|
||||
y << lf #yield left_factorial
|
||||
n += 1
|
||||
lf += f
|
||||
f *= n
|
||||
end
|
||||
end
|
||||
8
Task/Left-factorials/Ruby/left-factorials-2.rb
Normal file
8
Task/Left-factorials/Ruby/left-factorials-2.rb
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
left_fact = Enumerator.new do |y|
|
||||
f, lf = 1, 0
|
||||
1.step do |n|
|
||||
y << lf #yield left_factorial
|
||||
lf += f
|
||||
f *= n
|
||||
end
|
||||
end
|
||||
12
Task/Left-factorials/Ruby/left-factorials-3.rb
Normal file
12
Task/Left-factorials/Ruby/left-factorials-3.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
tens = 20.step(110, 10)
|
||||
thousands = 1000.step(10_000, 1000)
|
||||
|
||||
10001.times do |n|
|
||||
lf = left_fact.next
|
||||
case n
|
||||
when 0..10, *tens
|
||||
puts "!#{n} = #{lf}"
|
||||
when *thousands
|
||||
puts "!#{n} has #{lf.to_s.size} digits"
|
||||
end
|
||||
end
|
||||
36
Task/Left-factorials/Seed7/left-factorials.seed7
Normal file
36
Task/Left-factorials/Seed7/left-factorials.seed7
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
$ include "seed7_05.s7i";
|
||||
include "bigint.s7i";
|
||||
|
||||
const func bigInteger: leftFact (in integer: n) is func
|
||||
result
|
||||
var bigInteger: leftFact is 0_;
|
||||
local
|
||||
var bigInteger: factorial is 1_;
|
||||
var integer: i is 0;
|
||||
begin
|
||||
for i range 1 to n do
|
||||
leftFact +:= factorial;
|
||||
factorial *:= bigInteger conv i;
|
||||
end for;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var integer: n is 0;
|
||||
begin
|
||||
writeln("First 11 left factorials:");
|
||||
for n range 0 to 10 do
|
||||
write(" " <& leftFact(n));
|
||||
end for;
|
||||
writeln;
|
||||
writeln("20 through 110 (inclusive) by tens:");
|
||||
for n range 20 to 110 step 10 do
|
||||
writeln(leftFact(n));
|
||||
end for;
|
||||
writeln;
|
||||
writeln("Digits in 1,000 through 10,000 by thousands:");
|
||||
for n range 1000 to 10000 step 1000 do
|
||||
writeln(length(str(leftFact(n))));
|
||||
end for;
|
||||
writeln;
|
||||
end func;
|
||||
15
Task/Left-factorials/Tcl/left-factorials.tcl
Normal file
15
Task/Left-factorials/Tcl/left-factorials.tcl
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
proc leftfact {n} {
|
||||
set s 0
|
||||
for {set i [set f 1]} {$i <= $n} {incr i} {
|
||||
incr s $f
|
||||
set f [expr {$f * $i}]
|
||||
}
|
||||
return $s
|
||||
}
|
||||
|
||||
for {set i 0} {$i <= 110} {incr i [expr {$i>9?10:1}]} {
|
||||
puts "!$i = [leftfact $i]"
|
||||
}
|
||||
for {set i 1000} {$i <= 10000} {incr i 1000} {
|
||||
puts "!$i has [string length [leftfact $i]] digits"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue