Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,5 @@
(lib 'bigint)
(define (!n n)
(if (zero? n) 0
(+ (!n (1- n)) (factorial (1- n)))))
(remember '!n)

View file

@ -0,0 +1,38 @@
(for ((n 11)) (printf "!n(%d) = %d" n (!n n)))
(for ((n (in-range 20 120 10))) (printf "!n(%d) = %d" n (!n n)))
!n(0) = 0
!n(1) = 1
!n(2) = 2
!n(3) = 4
!n(4) = 10
!n(5) = 34
!n(6) = 154
!n(7) = 874
!n(8) = 5914
!n(9) = 46234
!n(10) = 409114
!n(20) = 128425485935180314
!n(30) = 9157958657951075573395300940314
!n(40) = 20935051082417771847631371547939998232420940314
!n(50) = 620960027832821612639424806694551108812720525606160920420940314
!n(60) = 141074930726669571000530822087000522211656242116439949000980378746128920420940314
!n(70) = 173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314
!n(80) = 906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314
!n(90) = 16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314
!n(100) = 942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
!n(110) = 145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
; Compute !n : 5 seconds
(for ((n (in-range 1000 10001 500))) (!n n) (writeln n))
; Display results : 12 seconds
(for ((n (in-range 1000 10001 1000))) (printf "Digits of !n(%d) = %d" n (number-length (!n n))))
Digits of !n(1000) = 2565
Digits of !n(2000) = 5733
Digits of !n(3000) = 9128
Digits of !n(4000) = 12670
Digits of !n(5000) = 16322
Digits of !n(6000) = 20062
Digits of !n(7000) = 23875
Digits of !n(8000) = 27749
Digits of !n(9000) = 31678
Digits of !n(10000) = 35656

View file

@ -0,0 +1,26 @@
import iterutils, bigints
proc lfact: iterator: BigInt =
result = iterator: BigInt =
yield 0.initBigInt
var
fact = 1.initBigInt
sum = 0.initBigInt
n = 1.initBigInt
while true:
sum += fact
fact *= n
n += 1
yield sum
echo "first 11:\n "
for i in lfact().slice(last = 10):
echo " ", i
echo "20 through 110 (inclusive) by tens:"
for i in lfact().slice(20, 110, 10):
echo " ", i
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

View file

@ -0,0 +1 @@
: leftFact | i | 0 1 rot loop: i [ tuck + swap i * ] drop ;

View file

@ -0,0 +1,15 @@
a = leftFact(0,10,1)
see "" + a + nl
func leftFact f,t,s
see "------ From " + f + " --To -> " + t +" Step " + s + " -------" + nl
for i = f to t step s
leftFact = 1
fct = 1
for j = 1 to i - 1
fct = fct * j
leftFact = leftFact + fct
next
if i >= 1000 see "" + i + " " + len(string(leftFact)) + " digits" + nl
else see "" + i + " " + leftFact + nl ok
next

View file

@ -0,0 +1,3 @@
func left_fact(k) {
^k -> map {|n| n! } -> sum(0)
}

View file

@ -0,0 +1,3 @@
func left_fact(k) {
^k -> reduce { |a,b| a + b! } + 1
}

View file

@ -0,0 +1,18 @@
func left_fact(n) {
static cached = 0
static factorial = 1
static leftfact = 0
 
if (n < cached) {
cached = 0
factorial = 1
leftfact = 0
}
 
while (n > cached) {
leftfact += factorial
factorial *= ++cached
}
 
leftfact
}

View file

@ -0,0 +1,9 @@
for r in [range(0, 10), range(20, 110).by(10)] {
for i in r {
printf("!%d = %s\n", i, left_fact(i));
}
}
for i in range(1000, 10000).by(1000) {
printf("!%d has %d digits.\n", i, left_fact(i).len);
}

View file

@ -0,0 +1,5 @@
def left_factorial:
reduce range(1; .+1) as $i
# state: [i!, !i]
([1,0]; .[1] += .[0] | .[0] *= $i)
| .[1];

View file

@ -0,0 +1,21 @@
import "BigInt" as BigInt;
# integer input
def long_left_factorial:
reduce range(1; .+1) as $i
# state: [i!, !i]
( ["1", "0"];
.[1] = BigInt::long_add(.[0]; .[1])
| .[0] = BigInt::long_multiply(.[0]; $i | tostring) )
| .[1];
# input and gap should be integers
def long_left_factorial_lengths(gap):
reduce range(1; .+1) as $i
# state: [i!, !i, gap]
(["1", "0", []];
.[1] = BigInt::long_add(.[0]; .[1])
| .[0] = BigInt::long_multiply(.[0]; $i|tostring)
| (.[1] | tostring | length) as $lf
| if $i % gap == 0 then .[2] += [[$i, $lf]] else . end)
| .[2];

View file

@ -0,0 +1,3 @@
((range(0;11), (range(2; 12) * 10)) | "\(.): \(long_left_factorial)"),
(10000 | long_left_factorial_lengths(1000) | .[] | "\(.[0]): length is \(.[1])")

View file

@ -0,0 +1,32 @@
$ jq -r -n -L . -f Long_left_factorial.jq
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: length is 2565
2000: length is 5733
3000: length is 9128
4000: length is 12670
5000: length is 16322
6000: length is 20062
7000: length is 23875
8000: length is 27749
9000: length is 31678
10000: length is 35656