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
47
Task/Gamma-function/FreeBASIC/gamma-function.freebasic
Normal file
47
Task/Gamma-function/FreeBASIC/gamma-function.freebasic
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Const pi = 3.1415926535897932
|
||||
Const e = 2.7182818284590452
|
||||
|
||||
Function gammaStirling (x As Double) As Double
|
||||
Return Sqr(2.0 * pi / x) * ((x / e) ^ x)
|
||||
End Function
|
||||
|
||||
Function gammaLanczos (x As Double) As Double
|
||||
Dim p(0 To 8) As Double = _
|
||||
{ _
|
||||
0.99999999999980993, _
|
||||
676.5203681218851, _
|
||||
-1259.1392167224028, _
|
||||
771.32342877765313, _
|
||||
-176.61502916214059, _
|
||||
12.507343278686905, _
|
||||
-0.13857109526572012, _
|
||||
9.9843695780195716e-6, _
|
||||
1.5056327351493116e-7 _
|
||||
}
|
||||
|
||||
Dim As Integer g = 7
|
||||
If x < 0.5 Then Return pi / (Sin(pi * x) * gammaLanczos(1-x))
|
||||
x -= 1
|
||||
Dim a As Double = p(0)
|
||||
Dim t As Double = x + g + 0.5
|
||||
|
||||
For i As Integer = 1 To 7
|
||||
a += p(i) / (x + i)
|
||||
Next
|
||||
|
||||
Return Sqr(2.0 * pi) * (t ^ (x + 0.5)) * Exp(-t) * a
|
||||
End Function
|
||||
|
||||
Print " x", " Stirling",, " Lanczos"
|
||||
Print
|
||||
For i As Integer = 1 To 20
|
||||
Dim As Double d = i / 10.0
|
||||
Print Using "#.##"; d;
|
||||
Print , Using "#.###############"; gammaStirling(d);
|
||||
Print , Using "#.###############"; gammaLanczos(d)
|
||||
Next
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
21
Task/Gamma-function/Nim/gamma-function.nim
Normal file
21
Task/Gamma-function/Nim/gamma-function.nim
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
const a = [
|
||||
1.00000000000000000000, 0.57721566490153286061, -0.65587807152025388108,
|
||||
-0.04200263503409523553, 0.16653861138229148950, -0.04219773455554433675,
|
||||
-0.00962197152787697356, 0.00721894324666309954, -0.00116516759185906511,
|
||||
-0.00021524167411495097, 0.00012805028238811619, -0.00002013485478078824,
|
||||
-0.00000125049348214267, 0.00000113302723198170, -0.00000020563384169776,
|
||||
0.00000000611609510448, 0.00000000500200764447, -0.00000000118127457049,
|
||||
0.00000000010434267117, 0.00000000000778226344, -0.00000000000369680562,
|
||||
0.00000000000051003703, -0.00000000000002058326, -0.00000000000000534812,
|
||||
0.00000000000000122678, -0.00000000000000011813, 0.00000000000000000119,
|
||||
0.00000000000000000141, -0.00000000000000000023, 0.00000000000000000002 ]
|
||||
|
||||
proc gamma(x): float =
|
||||
let y = x.float - 1.0
|
||||
result = a[a.high]
|
||||
for n in countdown(high(a) - 1, low(a)):
|
||||
result = result * y + a[n]
|
||||
result = 1.0 / result
|
||||
|
||||
for i in 1..10:
|
||||
echo gamma(i.float / 3.0)
|
||||
17
Task/Gamma-function/Oforth/gamma-function.oforth
Normal file
17
Task/Gamma-function/Oforth/gamma-function.oforth
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import: math
|
||||
|
||||
[
|
||||
676.5203681218851, -1259.1392167224028, 771.32342877765313,
|
||||
-176.61502916214059, 12.507343278686905, -0.13857109526572012,
|
||||
9.9843695780195716e-6, 1.5056327351493116e-7
|
||||
] const: Gamma.Lanczos
|
||||
|
||||
: gamma(z)
|
||||
| i t |
|
||||
z 0.5 < ifTrue: [ Pi dup z * sin 1.0 z - gamma * / return ]
|
||||
z 1.0 - ->z
|
||||
0.99999999999980993 Gamma.Lanczos size loop: i [ i Gamma.Lanczos at z i + / + ]
|
||||
z Gamma.Lanczos size + 0.5 - ->t
|
||||
2 Pi * sqrt *
|
||||
t z 0.5 + powf *
|
||||
t neg exp * ;
|
||||
18
Task/Gamma-function/Ring/gamma-function.ring
Normal file
18
Task/Gamma-function/Ring/gamma-function.ring
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
decimals(3)
|
||||
gamma = 0.577
|
||||
coeff = -0.655
|
||||
quad = -0.042
|
||||
qui = 0.166
|
||||
set = -0.042
|
||||
|
||||
for i=1 to 10
|
||||
see gammafunc(i / 3.0) + nl
|
||||
next
|
||||
|
||||
func recigamma z
|
||||
return z + gamma * pow(z,2) + coeff * pow(z,3) + quad * pow(z,4) + qui * pow(z,5) + set * pow(z,6)
|
||||
|
||||
func gammafunc z
|
||||
if z = 1 return 1
|
||||
but fabs(z) <= 0.5 return 1 / recigamma(z)
|
||||
else return (z - 1) * gammafunc(z-1) ok
|
||||
19
Task/Gamma-function/Sidef/gamma-function-1.sidef
Normal file
19
Task/Gamma-function/Sidef/gamma-function-1.sidef
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
var a = [ 1.00000_00000_00000_00000, 0.57721_56649_01532_86061, -0.65587_80715_20253_88108,
|
||||
-0.04200_26350_34095_23553, 0.16653_86113_82291_48950, -0.04219_77345_55544_33675,
|
||||
-0.00962_19715_27876_97356, 0.00721_89432_46663_09954, -0.00116_51675_91859_06511,
|
||||
-0.00021_52416_74114_95097, 0.00012_80502_82388_11619, -0.00002_01348_54780_78824,
|
||||
-0.00000_12504_93482_14267, 0.00000_11330_27231_98170, -0.00000_02056_33841_69776,
|
||||
0.00000_00061_16095_10448, 0.00000_00050_02007_64447, -0.00000_00011_81274_57049,
|
||||
0.00000_00001_04342_67117, 0.00000_00000_07782_26344, -0.00000_00000_03696_80562,
|
||||
0.00000_00000_00510_03703, -0.00000_00000_00020_58326, -0.00000_00000_00005_34812,
|
||||
0.00000_00000_00001_22678, -0.00000_00000_00000_11813, 0.00000_00000_00000_00119,
|
||||
0.00000_00000_00000_00141, -0.00000_00000_00000_00023, 0.00000_00000_00000_00002 ];
|
||||
|
||||
func gamma(x) {
|
||||
var y = (x - 1);
|
||||
1 / a.reverse.reduce {|sum, an| sum*y + an};
|
||||
}
|
||||
|
||||
for i in 1..10 {
|
||||
say ("%.14e" % gamma(i/3));
|
||||
}
|
||||
38
Task/Gamma-function/Sidef/gamma-function-2.sidef
Normal file
38
Task/Gamma-function/Sidef/gamma-function-2.sidef
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
func gamma(z) {
|
||||
var epsilon = 0.0000001
|
||||
func withinepsilon(x) {
|
||||
abs(x - abs(x)) <= epsilon
|
||||
}
|
||||
|
||||
var p = [
|
||||
676.5203681218851, -1259.1392167224028,
|
||||
771.32342877765313, -176.61502916214059,
|
||||
12.507343278686905, -0.13857109526572012,
|
||||
9.9843695780195716e-6, 1.5056327351493116e-7,
|
||||
]
|
||||
|
||||
var result;
|
||||
z = Complex(z)
|
||||
const pi = Complex.pi
|
||||
|
||||
if (z.real < 0.5) {
|
||||
result = (pi / (sin(pi * z) * gamma(Complex(1) - z)))
|
||||
}
|
||||
else {
|
||||
z -= 1
|
||||
var x = 0.99999999999980993
|
||||
|
||||
p.each_kv { |i, v|
|
||||
x += v/(z + i + 1)
|
||||
}
|
||||
|
||||
var t = (z + p.len - 0.5)
|
||||
result = (sqrt(pi*2) * t**(z+0.5) * exp(-t) * x)
|
||||
}
|
||||
|
||||
withinepsilon(result.im) ? result.real : result
|
||||
}
|
||||
|
||||
for i in 1..10 {
|
||||
say ("%.14e" % gamma(i/3));
|
||||
}
|
||||
11
Task/Gamma-function/Sidef/gamma-function-3.sidef
Normal file
11
Task/Gamma-function/Sidef/gamma-function-3.sidef
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
define e = Math.e;
|
||||
define pi = Math.pi;
|
||||
|
||||
func Γ(t) {
|
||||
t < 20 ? (__FUNC__(t + 1) / t)
|
||||
: (Math.sqrt(2*pi*t) * Math.pow(t/e + 1/(12*e*t), t) / t);
|
||||
}
|
||||
|
||||
for i in 1..10 {
|
||||
say ("%.14e" % Γ(i/3));
|
||||
}
|
||||
60
Task/Gamma-function/Visual-FoxPro/gamma-function.visual
Normal file
60
Task/Gamma-function/Visual-FoxPro/gamma-function.visual
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
LOCAL i As Integer, x As Double, o As lanczos
|
||||
CLOSE DATABASES ALL
|
||||
CLEAR
|
||||
CREATE CURSOR results (ZVal B(1), GamVal B(15))
|
||||
INDEX ON zval TAG ZVal COLLATE "Machine"
|
||||
SET ORDER TO 0
|
||||
o = CREATEOBJECT("lanczos")
|
||||
FOR i = 1 TO 20
|
||||
x = i/10
|
||||
INSERT INTO results VALUES (x, o.Gamma(x))
|
||||
ENDFOR
|
||||
UPDATE results SET GamVal = ROUND(GamVal, 0) WHERE ZVal = INT(ZVal)
|
||||
*!* This just creates the output text - it is not part of the algorithm
|
||||
DO cursor2txt.prg WITH "results", .T.
|
||||
|
||||
DEFINE CLASS lanczos As Session
|
||||
#DEFINE FPF 5.5
|
||||
#DEFINE HALF 0.5
|
||||
#DEFINE PY PI()
|
||||
DIMENSION LanCoeff[7]
|
||||
nSize = 0
|
||||
|
||||
PROCEDURE Init
|
||||
DODEFAULT()
|
||||
WITH THIS
|
||||
.LanCoeff[1] = 1.000000000190015
|
||||
.LanCoeff[2] = 76.18009172947146
|
||||
.LanCoeff[3] = -86.50532032941677
|
||||
.LanCoeff[4] = 24.01409824083091
|
||||
.LanCoeff[5] = -1.231739572450155
|
||||
.LanCoeff[6] = 0.0012086509738662
|
||||
.LanCoeff[7] = -0.000005395239385
|
||||
.nSize = ALEN(.LanCoeff)
|
||||
ENDWITH
|
||||
ENDPROC
|
||||
|
||||
FUNCTION Gamma(z)
|
||||
RETURN EXP(THIS.LogGamma(z))
|
||||
ENDFUNC
|
||||
|
||||
FUNCTION LogGamma(z)
|
||||
LOCAL a As Double, b As Double, i As Integer, j As Integer, lg As Double
|
||||
IF z < 0.5
|
||||
lg = LOG(PY/SIN(PY*z)) - THIS.LogGamma(1 - z)
|
||||
ELSE
|
||||
WITH THIS
|
||||
z = z - 1
|
||||
b = z + FPF
|
||||
a = .LanCoeff[1]
|
||||
FOR i = 2 TO .nSize
|
||||
j = i - 1
|
||||
a = a + .LanCoeff[i]/(z + j)
|
||||
ENDFOR
|
||||
lg = (LOG(SQRT(2*PY)) + LOG(a) - b) + LOG(b)*(z + HALF)
|
||||
ENDWITH
|
||||
ENDIF
|
||||
RETURN lg
|
||||
ENDFUNC
|
||||
|
||||
ENDDEFINE
|
||||
16
Task/Gamma-function/jq/gamma-function-1.jq
Normal file
16
Task/Gamma-function/jq/gamma-function-1.jq
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
def gamma:
|
||||
[
|
||||
1.00000000000000000000, 0.57721566490153286061, -0.65587807152025388108, -0.04200263503409523553,
|
||||
0.16653861138229148950, -0.04219773455554433675, -0.00962197152787697356, 0.00721894324666309954,
|
||||
-0.00116516759185906511, -0.00021524167411495097, 0.00012805028238811619, -0.00002013485478078824,
|
||||
-0.00000125049348214267, 0.00000113302723198170, -0.00000020563384169776, 0.00000000611609510448,
|
||||
0.00000000500200764447, -0.00000000118127457049, 0.00000000010434267117, 0.00000000000778226344,
|
||||
-0.00000000000369680562, 0.00000000000051003703, -0.00000000000002058326, -0.00000000000000534812,
|
||||
0.00000000000000122678, -0.00000000000000011813, 0.00000000000000000119, 0.00000000000000000141,
|
||||
-0.00000000000000000023, 0.00000000000000000002
|
||||
] as $a
|
||||
| (. - 1) as $y
|
||||
| ($a|length) as $n
|
||||
| reduce range(2; 1+$n) as $an
|
||||
($a[$n-1]; (. * $y) + $a[$n - $an])
|
||||
| 1 / . ;
|
||||
16
Task/Gamma-function/jq/gamma-function-2.jq
Normal file
16
Task/Gamma-function/jq/gamma-function-2.jq
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
# for reals, but easily extended to complex values
|
||||
def gamma_by_lanczos:
|
||||
def pow(x): if x == 0 then 1 elif x == 1 then . else x * log | exp end;
|
||||
. as $x
|
||||
| ((1|atan) * 4) as $pi
|
||||
| if $x < 0.5 then $pi / ((($pi * $x) | sin) * ((1-$x)|gamma_by_lanczos ))
|
||||
else
|
||||
[ 0.99999999999980993, 676.5203681218851, -1259.1392167224028,
|
||||
771.32342877765313, -176.61502916214059, 12.507343278686905,
|
||||
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7] as $p
|
||||
| ($x - 1) as $x
|
||||
| ($x + 7.5) as $t
|
||||
| reduce range(1; $p|length) as $i
|
||||
($p[0]; . + ($p[$i]/($x + $i) ))
|
||||
* ((2*$pi) | sqrt) * ($t | pow($x+0.5)) * ((-$t)|exp)
|
||||
end;
|
||||
5
Task/Gamma-function/jq/gamma-function-3.jq
Normal file
5
Task/Gamma-function/jq/gamma-function-3.jq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def gamma_by_stirling:
|
||||
def pow(x): if x == 0 then 1 elif x == 1 then . else x * log | exp end;
|
||||
((1|atan) * 8) as $twopi
|
||||
| . as $x
|
||||
| (($twopi/$x) | sqrt) * ( ($x / (1|exp)) | pow($x));
|
||||
6
Task/Gamma-function/jq/gamma-function-4.jq
Normal file
6
Task/Gamma-function/jq/gamma-function-4.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def pad(n): tostring | . + (n - length) * " ";
|
||||
|
||||
" i: gamma lanczos tgamma",
|
||||
(range(1;11)
|
||||
| . / 3.0
|
||||
| "\(pad(18)): \(gamma|pad(18)) : \(gamma_by_lanczos|pad(18)) : \(tgamma)")
|
||||
12
Task/Gamma-function/jq/gamma-function-5.jq
Normal file
12
Task/Gamma-function/jq/gamma-function-5.jq
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
$ jq -M -r -n -f Gamma_function_Stirling.jq
|
||||
i: gamma lanczos tgamma
|
||||
0.3333333333333333: 2.6789385347077483 : 2.6789385347077483 : 2.678938534707748
|
||||
0.6666666666666666: 1.3541179394264005 : 1.3541179394263998 : 1.3541179394264005
|
||||
1 : 1 : 0.9999999999999998 : 1
|
||||
1.3333333333333333: 0.8929795115692493 : 0.8929795115692494 : 0.8929795115692493
|
||||
1.6666666666666667: 0.9027452929509336 : 0.9027452929509342 : 0.9027452929509336
|
||||
2 : 1 : 1.0000000000000002 : 1
|
||||
2.3333333333333335: 1.190639348758999 : 1.1906393487589995 : 1.190639348758999
|
||||
2.6666666666666665: 1.5045754882515399 : 1.5045754882515576 : 1.5045754882515558
|
||||
3 : 1.9999999999939684 : 2.0000000000000013 : 2
|
||||
3.3333333333333335: 2.778158479338573 : 2.778158480437665 : 2.7781584804376647
|
||||
Loading…
Add table
Add a link
Reference in a new issue