Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
62
Task/Bernoulli-numbers/Crystal/bernoulli-numbers.crystal
Normal file
62
Task/Bernoulli-numbers/Crystal/bernoulli-numbers.crystal
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
# Taken from the 'Ada 99' project, https://marquisdegeek.com/code_ada99
|
||||
|
||||
class Fraction
|
||||
def initialize(n : Int64, d : Int64)
|
||||
@numerator = n
|
||||
@denominator = d
|
||||
end
|
||||
|
||||
def numerator
|
||||
@numerator
|
||||
end
|
||||
|
||||
def denominator
|
||||
@denominator
|
||||
end
|
||||
|
||||
def subtract(rhs_fraction)
|
||||
rhs_numerator = rhs_fraction.numerator * @denominator
|
||||
rhs_denominator = rhs_fraction.denominator * @denominator
|
||||
@numerator *= rhs_fraction.denominator
|
||||
@denominator *= rhs_fraction.denominator
|
||||
@numerator -= rhs_numerator
|
||||
self.reduce
|
||||
end
|
||||
|
||||
def multiply(value)
|
||||
@numerator *= value
|
||||
end
|
||||
|
||||
def reduce
|
||||
gcd = gcd(@numerator, @denominator)
|
||||
@numerator /= gcd
|
||||
@denominator /= gcd
|
||||
end
|
||||
|
||||
def to_s
|
||||
@numerator == 0 ? 0 : @numerator.to_s + '/' + @denominator.to_s
|
||||
end
|
||||
end
|
||||
|
||||
def gcd(a, b)
|
||||
# we need b>0 because b on its own isn't considered true
|
||||
b > 0 ? gcd(b, a % b) : a
|
||||
end
|
||||
|
||||
def calculate_bernoulli(bern)
|
||||
row = [] of Fraction
|
||||
0_i64.step(bern) do |m|
|
||||
row << Fraction.new(1_i64, m + 1)
|
||||
m.step(1, -1) do |j|
|
||||
row[j - 1].subtract(row[j])
|
||||
row[j - 1].multiply(j)
|
||||
row[j - 1].reduce
|
||||
end
|
||||
end
|
||||
|
||||
row[0]
|
||||
end
|
||||
|
||||
1_i64.step(30_i64) do |bern|
|
||||
puts "#{bern} : #{calculate_bernoulli(bern).to_s}"
|
||||
end
|
||||
24
Task/Bernoulli-numbers/EchoLisp/bernoulli-numbers-1.echolisp
Normal file
24
Task/Bernoulli-numbers/EchoLisp/bernoulli-numbers-1.echolisp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
(lib 'bigint) ;; lerge numbers
|
||||
(lib 'gloops) ;; classes
|
||||
|
||||
(define-class Rational null ((a :initform #0) (b :initform #1)))
|
||||
(define-method tostring (Rational) (lambda (r) (format "%50d / %d" r.a r.b)))
|
||||
(define-method normalize (Rational) (lambda (r) ;; divide a and b by gcd
|
||||
(let ((g (gcd r.a r.b)))
|
||||
(set! r.a (/ r.a g)) (set! r.b (/ r.b g))
|
||||
(when (< r.b 0) (set! r.a ( - r.a)) (set! r.b (- r.b))) ;; denominator > 0
|
||||
r)))
|
||||
|
||||
(define-method initialize (Rational) (lambda (r) (normalize r)))
|
||||
(define-method add (Rational) (lambda (r n) ;; + Rational any number
|
||||
(normalize (Rational (+ (* (+ #0 n) r.b) r.a) r.b))))
|
||||
(define-method add (Rational Rational) (lambda (r q) ;;; + Rational Rational
|
||||
(normalize (Rational (+ (* r.a q.b) (* r.b q.a)) (* r.b q.b)))))
|
||||
(define-method sub (Rational Rational) (lambda (r q)
|
||||
(normalize (Rational (- (* r.a q.b) (* r.b q.a)) (* r.b q.b)))))
|
||||
(define-method mul (Rational Rational) (lambda (r q)
|
||||
(normalize (Rational (* r.a q.a) (* r.b q.b)))))
|
||||
(define-method mul (Rational) (lambda (r n)
|
||||
(normalize (Rational (* r.a (+ #0 n)) r.b ))))
|
||||
(define-method div (Rational Rational) (lambda (r q)
|
||||
(normalize (Rational (* r.a q.b) (* r.b q.a)))))
|
||||
47
Task/Bernoulli-numbers/EchoLisp/bernoulli-numbers-2.echolisp
Normal file
47
Task/Bernoulli-numbers/EchoLisp/bernoulli-numbers-2.echolisp
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
;; Bernoulli numbers
|
||||
;; http://rosettacode.org/wiki/Bernoulli_numbers
|
||||
(define A (make-vector 100 0))
|
||||
|
||||
(define (B n)
|
||||
(for ((m (1+ n))) ;; #1 creates a large integer
|
||||
(vector-set! A m (Rational #1 (+ #1 m)))
|
||||
(for ((j (in-range m 0 -1)))
|
||||
(vector-set! A (1- j)
|
||||
(mul (sub (vector-ref A (1- j)) (vector-ref A j)) j))))
|
||||
(vector-ref A 0))
|
||||
|
||||
(for ((b (in-range 0 62 2))) (writeln b (B b))) →
|
||||
|
||||
0 1 / 1
|
||||
2 1 / 6
|
||||
4 -1 / 30
|
||||
6 1 / 42
|
||||
8 -1 / 30
|
||||
10 5 / 66
|
||||
12 -691 / 2730
|
||||
14 7 / 6
|
||||
16 -3617 / 510
|
||||
18 43867 / 798
|
||||
20 -174611 / 330
|
||||
22 854513 / 138
|
||||
24 -236364091 / 2730
|
||||
26 8553103 / 6
|
||||
28 -23749461029 / 870
|
||||
30 8615841276005 / 14322
|
||||
32 -7709321041217 / 510
|
||||
34 2577687858367 / 6
|
||||
36 -26315271553053477373 / 1919190
|
||||
38 2929993913841559 / 6
|
||||
40 -261082718496449122051 / 13530
|
||||
42 1520097643918070802691 / 1806
|
||||
44 -27833269579301024235023 / 690
|
||||
46 596451111593912163277961 / 282
|
||||
48 -5609403368997817686249127547 / 46410
|
||||
50 495057205241079648212477525 / 66
|
||||
52 -801165718135489957347924991853 / 1590
|
||||
54 29149963634884862421418123812691 / 798
|
||||
56 -2479392929313226753685415739663229 / 870
|
||||
58 84483613348880041862046775994036021 / 354
|
||||
60 -1215233140483755572040304994079820246041491 / 56786730
|
||||
|
||||
(B 1) → 1 / 2
|
||||
54
Task/Bernoulli-numbers/FreeBASIC/bernoulli-numbers.freebasic
Normal file
54
Task/Bernoulli-numbers/FreeBASIC/bernoulli-numbers.freebasic
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
' version 08-10-2016
|
||||
' compile with: fbc -s console
|
||||
' uses gmp
|
||||
|
||||
#Include Once "gmp.bi"
|
||||
|
||||
#Define max 60
|
||||
|
||||
Dim As Long n
|
||||
Dim As ZString Ptr gmp_str :gmp_str = Allocate(1000) ' 1000 char
|
||||
Dim Shared As Mpq_ptr tmp, big_j
|
||||
tmp = Allocate(Len(__mpq_struct)) :Mpq_init(tmp)
|
||||
big_j = Allocate(Len(__mpq_struct)) :Mpq_init(big_j)
|
||||
|
||||
Dim Shared As Mpq_ptr a(max), b(max)
|
||||
For n = 0 To max
|
||||
A(n) = Allocate(Len(__mpq_struct)) :Mpq_init(A(n))
|
||||
B(n) = Allocate(Len(__mpq_struct)) :Mpq_init(B(n))
|
||||
Next
|
||||
|
||||
Function Bernoulli(n As Integer) As Mpq_ptr
|
||||
|
||||
Dim As Long m, j
|
||||
|
||||
For m = 0 To n
|
||||
Mpq_set_ui(A(m), 1, m + 1)
|
||||
For j = m To 1 Step - 1
|
||||
Mpq_sub(tmp, A(j - 1), A(j))
|
||||
Mpq_set_ui(big_j, j, 1) 'big_j = j
|
||||
Mpq_mul(A(j - 1), big_j, tmp)
|
||||
Next
|
||||
Next
|
||||
|
||||
Return A(0)
|
||||
End Function
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
For n = 0 To max
|
||||
Mpq_set(B(n), Bernoulli(n))
|
||||
Mpq_get_str(gmp_str, 10, B(n))
|
||||
If *gmp_str <> "0" Then
|
||||
If *gmp_str = "1" Then *gmp_str = "1/1"
|
||||
Print Using "B(##) = "; n;
|
||||
Print Space(45 - InStr(*gmp_str, "/")); *gmp_str
|
||||
End If
|
||||
Next
|
||||
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" :Wend
|
||||
Print :Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
6
Task/Bernoulli-numbers/FunL/bernoulli-numbers.funl
Normal file
6
Task/Bernoulli-numbers/FunL/bernoulli-numbers.funl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import integers.choose
|
||||
|
||||
def B( n ) = sum( 1/(k + 1)*sum((if 2|r then 1 else -1)*choose(k, r)*(r^n) | r <- 0..k) | k <- 0..n )
|
||||
|
||||
for i <- 0..60 if i == 1 or 2|i
|
||||
printf( "B(%2d) = %s\n", i, B(i) )
|
||||
41
Task/Bernoulli-numbers/Phix/bernoulli-numbers-1.phix
Normal file
41
Task/Bernoulli-numbers/Phix/bernoulli-numbers-1.phix
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
include builtins\bigatom.e
|
||||
|
||||
constant NUM = 1, DEN = 2
|
||||
|
||||
type ba_frac(object r)
|
||||
return sequence(r) and length(r)=2 and bigatom(r[NUM]) and bigatom(r[DEN])
|
||||
end type
|
||||
|
||||
function ba_gcd(bigatom u, bigatom v)
|
||||
bigatom t
|
||||
u = ba_floor(ba_abs(u))
|
||||
v = ba_floor(ba_abs(v))
|
||||
while v!=BA_ZERO do
|
||||
t = u
|
||||
u = v
|
||||
v = ba_remainder(t, v)
|
||||
end while
|
||||
return u
|
||||
end function
|
||||
|
||||
function ba_frac_normalise(bigatom n, bigatom d)
|
||||
bigatom g
|
||||
if ba_compare(d,BA_ZERO)<0 then
|
||||
n = ba_sub(0,n)
|
||||
d = ba_sub(0,d)
|
||||
end if
|
||||
g = ba_gcd(n,d)
|
||||
return {ba_idivide(n,g),ba_idivide(d,g)}
|
||||
end function
|
||||
|
||||
function ba_frac_sub(ba_frac a, ba_frac b)
|
||||
bigatom {an,ad} = a,
|
||||
{bn,bd} = b
|
||||
return ba_frac_normalise(ba_sub(ba_multiply(an,bd),ba_multiply(bn,ad)),ba_multiply(ad,bd))
|
||||
end function
|
||||
|
||||
function ba_frac_mul(ba_frac a, ba_frac b)
|
||||
bigatom {an,ad} = a,
|
||||
{bn,bd} = b
|
||||
return ba_frac_normalise(ba_multiply(an,bn),ba_multiply(ad,bd))
|
||||
end function
|
||||
10
Task/Bernoulli-numbers/Phix/bernoulli-numbers-2.phix
Normal file
10
Task/Bernoulli-numbers/Phix/bernoulli-numbers-2.phix
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
sequence a = {}
|
||||
for m=0 to 60 do
|
||||
a = append(a,{ba_new(1),ba_new(m+1)})
|
||||
for j=m to 1 by -1 do
|
||||
a[j] = ba_frac_mul({ba_new(j),ba_new(1)},ba_frac_sub(a[j+1],a[j]))
|
||||
end for
|
||||
if a[1][1]!=BA_ZERO then
|
||||
printf(1,"B(%2d) = %44s / %s\n",{m,ba_sprint(a[1][1]),ba_sprint(a[1][2])})
|
||||
end if
|
||||
end for
|
||||
1
Task/Bernoulli-numbers/SPAD/bernoulli-numbers.spad
Normal file
1
Task/Bernoulli-numbers/SPAD/bernoulli-numbers.spad
Normal file
|
|
@ -0,0 +1 @@
|
|||
for n in 0..60 | (b:=bernoulli(n)$INTHEORY; b~=0) repeat print [n,b]
|
||||
22
Task/Bernoulli-numbers/Sidef/bernoulli-numbers-1.sidef
Normal file
22
Task/Bernoulli-numbers/Sidef/bernoulli-numbers-1.sidef
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
func bernoulli_number{}; # must be declared before first used
|
||||
|
||||
func bern_helper(n, k) {
|
||||
binomial(n, k) * (bernoulli_number(k) / (n - k + 1));
|
||||
}
|
||||
|
||||
func bern_diff(n, k, d) {
|
||||
n < k ? d : bern_diff(n, k + 1, d - bern_helper(n + 1, k));
|
||||
}
|
||||
|
||||
bernoulli_number = func(n) is cached {
|
||||
|
||||
n.is_one && return 1/2;
|
||||
n.is_odd && return 0;
|
||||
|
||||
n > 0 ? bern_diff(n - 1, 0, 1) : 1;
|
||||
}
|
||||
|
||||
range(0, 60).each { |i|
|
||||
var num = bernoulli_number(i) || next;
|
||||
printf("B(%2d) = %44s / %s\n", i, num.parts);
|
||||
}
|
||||
13
Task/Bernoulli-numbers/Sidef/bernoulli-numbers-2.sidef
Normal file
13
Task/Bernoulli-numbers/Sidef/bernoulli-numbers-2.sidef
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
func bernoulli_print {
|
||||
var a = []
|
||||
range(0, 60).each { |m|
|
||||
a << (m+1 -> inv)
|
||||
m.downto(1).each { |j|
|
||||
(a[j-1] -= a[j]) *= j
|
||||
}
|
||||
a[0] || next
|
||||
printf("B(%2d) = %44s / %s\n", m, a[0].parts)
|
||||
}
|
||||
}
|
||||
|
||||
bernoulli_print()
|
||||
39
Task/Bernoulli-numbers/jq/bernoulli-numbers-1.jq
Normal file
39
Task/Bernoulli-numbers/jq/bernoulli-numbers-1.jq
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# def negate:
|
||||
# def lessOrEqual(x; y): # x <= y
|
||||
# def long_add(x;y): # x+y
|
||||
# def long_minus(x;y): # x-y
|
||||
# def long_multiply(x;y) # x*y
|
||||
# def long_divide(x;y): # x/y => [q,r]
|
||||
# def long_div(x;y) # integer division
|
||||
# def long_mod(x;y) # %
|
||||
|
||||
# In all cases, x and y must be strings
|
||||
|
||||
def negate: (- tonumber) | tostring;
|
||||
|
||||
def lessOrEqual(num1; num2): (num1|tonumber) <= (num2|tonumber);
|
||||
|
||||
def long_add(num1; num2): ((num1|tonumber) + (num2|tonumber)) | tostring;
|
||||
|
||||
def long_minus(x;y): ((num1|tonumber) - (num2|tonumber)) | tostring;
|
||||
|
||||
# multiply two decimal strings, which may be signed (+ or -)
|
||||
def long_multiply(num1; num2):
|
||||
((num1|tonumber) * (num2|tonumber)) | tostring;
|
||||
|
||||
# return [quotient, remainder]
|
||||
# 0/0 = 1; n/0 => error
|
||||
def long_divide(xx;yy): # x/y => [q,r] imples x == (y * q) + r
|
||||
def ld(x;y):
|
||||
def abs: if . < 0 then -. else . end;
|
||||
(x|abs) as $x | (y|abs) as $y
|
||||
| (if (x >= 0 and y > 0) or (x < 0 and y < 0) then 1 else -1 end) as $sign
|
||||
| (if x >= 0 then 1 else -1 end) as $sx
|
||||
| [$sign * ($x / $y | floor), $sx * ($x % $y)];
|
||||
ld( xx|tonumber; yy|tonumber) | map(tostring);
|
||||
|
||||
def long_div(x;y):
|
||||
long_divide(x;y) | .[0];
|
||||
|
||||
def long_mod(x;y):
|
||||
((x|tonumber) % (y|tonumber)) | tostring;
|
||||
54
Task/Bernoulli-numbers/jq/bernoulli-numbers-2.jq
Normal file
54
Task/Bernoulli-numbers/jq/bernoulli-numbers-2.jq
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
# A fraction is represented by [numerator, denominator] in reduced form, with the sign on top
|
||||
|
||||
# a and b should be BigInt; return a BigInt
|
||||
def gcd(a; b):
|
||||
def long_abs: . as $in | if lessOrEqual("0"; $in) then $in else negate end;
|
||||
|
||||
# subfunction rgcd expects [a,b] as input
|
||||
# i.e. a ~ .[0] and b ~ .[1]
|
||||
def rgcd:
|
||||
.[0] as $a | .[1] as $b
|
||||
| if $b == "0" then $a
|
||||
else [$b, long_mod($a ; $b ) ] | rgcd
|
||||
end;
|
||||
|
||||
a as $a | b as $b
|
||||
| [$a,$b] | rgcd | long_abs ;
|
||||
|
||||
def normalize:
|
||||
.[0] as $p | .[1] as $q
|
||||
| if $p == "0" then ["0", "1"]
|
||||
elif lessOrEqual($q ; "0") then [ ($p|negate), ($q|negate)] | normalize
|
||||
else gcd($p; $q) as $g
|
||||
| [ long_div($p;$g), long_div($q;$g) ]
|
||||
end ;
|
||||
|
||||
# a and b should be fractions expressed in the form [p, q]
|
||||
def add(a; b):
|
||||
a as $a | b as $b
|
||||
| if $a[1] == "1" and $b[1] == "1" then [ long_add($a[0]; $b[0]) , "1"]
|
||||
elif $a[1] == $b[1] then [ long_add( $a[0]; $b[0]), $a[1] ] | normalize
|
||||
elif $a[0] == "0" then $b
|
||||
elif $b[0] == "0" then $a
|
||||
else [ long_add( long_multiply($a[0]; $b[1]) ; long_multiply($b[0]; $a[1])),
|
||||
long_multiply($a[1]; $b[1]) ]
|
||||
| normalize
|
||||
end ;
|
||||
|
||||
# a and/or b may be BigInts, or [p,q] fractions
|
||||
def multiply(a; b):
|
||||
a as $a | b as $b
|
||||
| if ($a|type) == "string" and ($b|type) == "string" then [ long_multiply($a; $b), "1"]
|
||||
else
|
||||
if $a|type == "string" then [ long_multiply( $a; $b[0]), $b[1] ]
|
||||
elif $b|type == "string" then [ long_multiply( $b; $a[0]), $a[1] ]
|
||||
else [ long_multiply( $a[0]; $b[0]), long_multiply($a[1]; $b[1]) ]
|
||||
end
|
||||
| normalize
|
||||
end ;
|
||||
|
||||
def minus(a; b):
|
||||
a as $a | b as $b
|
||||
| if $a == $b then ["0", "1"]
|
||||
else add($a; [ ($b[0]|negate), $b[1] ] )
|
||||
end ;
|
||||
10
Task/Bernoulli-numbers/jq/bernoulli-numbers-3.jq
Normal file
10
Task/Bernoulli-numbers/jq/bernoulli-numbers-3.jq
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# Using the algorithm in the task description:
|
||||
def bernoulli(n):
|
||||
reduce range(0; n+1) as $m
|
||||
( [];
|
||||
.[$m] = ["1", long_add($m|tostring; "1")] # i.e. 1 / ($m+1)
|
||||
| reduce ($m - range(0 ; $m)) as $j
|
||||
(.;
|
||||
.[$j-1] = multiply( [($j|tostring), "1"]; minus( .[$j-1] ; .[$j]) ) ))
|
||||
| .[0] # (which is Bn)
|
||||
;
|
||||
2
Task/Bernoulli-numbers/jq/bernoulli-numbers-4.jq
Normal file
2
Task/Bernoulli-numbers/jq/bernoulli-numbers-4.jq
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
range(0;61)
|
||||
| if . % 2 == 0 or . == 1 then "\(.): \(bernoulli(.) )" else empty end
|
||||
33
Task/Bernoulli-numbers/jq/bernoulli-numbers-5.jq
Normal file
33
Task/Bernoulli-numbers/jq/bernoulli-numbers-5.jq
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
$ jq -n -r -f Bernoulli.jq
|
||||
0: ["1","1"]
|
||||
1: ["1","2"]
|
||||
2: ["1","6"]
|
||||
4: ["-1","30"]
|
||||
6: ["1","42"]
|
||||
8: ["-1","30"]
|
||||
10: ["5","66"]
|
||||
12: ["-691","2730"]
|
||||
14: ["7","6"]
|
||||
16: ["-3617","510"]
|
||||
18: ["43867","798"]
|
||||
20: ["-174611","330"]
|
||||
22: ["854513","138"]
|
||||
24: ["-236364091","2730"]
|
||||
26: ["8553103","6"]
|
||||
28: ["-23749461029","870"]
|
||||
30: ["8615841276005","14322"]
|
||||
32: ["-7709321041217","510"]
|
||||
34: ["2577687858367","6"]
|
||||
36: ["-26315271553053477373","1919190"]
|
||||
38: ["2929993913841559","6"]
|
||||
40: ["-261082718496449122051","13530"]
|
||||
42: ["1520097643918070802691","1806"]
|
||||
44: ["-27833269579301024235023","690"]
|
||||
46: ["596451111593912163277961","282"]
|
||||
48: ["-5609403368997817686249127547","46410"]
|
||||
50: ["495057205241079648212477525","66"]
|
||||
52: ["-801165718135489957347924991853","1590"]
|
||||
54: ["29149963634884862421418123812691","798"]
|
||||
56: ["-2479392929313226753685415739663229","870"]
|
||||
58: ["84483613348880041862046775994036021","354"]
|
||||
60: ["-1215233140483755572040304994079820246041491","56786730"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue