Gamma function: Difference between revisions

Added Easylang
(→‎{{header|AWK}}: Remove dead line of code pertaining to unfinished Lanczos calculation.)
(Added Easylang)
 
(14 intermediate revisions by 10 users not shown)
Line 492:
gamma( 70)= 1.711224524e98, 1.711224524281e98, 1.711224524281e98, 7.57303907062e-29, 1.709188578191e98
</pre>
 
=={{header|ANSI Standard BASIC}}==
 
{{trans|BBC Basic}} - Lanczos method.
<syntaxhighlight lang="ansi standard basic">100 DECLARE EXTERNAL FUNCTION FNlngamma
110
120 DEF FNgamma(z) = EXP(FNlngamma(z))
130
140 FOR x = 0.1 TO 2.05 STEP 0.1
150 PRINT USING$("#.#",x), USING$("##.############", FNgamma(x))
160 NEXT x
170 END
180
190 EXTERNAL FUNCTION FNlngamma(z)
200 DIM lz(0 TO 6)
210 RESTORE
220 MAT READ lz
230 DATA 1.000000000190015, 76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.0012086509738662, -0.000005395239385
240 IF z < 0.5 THEN
250 LET FNlngamma = LOG(PI / SIN(PI * z)) - FNlngamma(1.0 - z)
260 EXIT FUNCTION
270 END IF
280 LET z = z - 1.0
290 LET b = z + 5.5
300 LET a = lz(0)
310 FOR i = 1 TO 6
320 LET a = a + lz(i) / (z + i)
330 NEXT i
340 LET FNlngamma = (LOG(SQR(2*PI)) + LOG(a) - b) + LOG(b) * (z+0.5)
350 END FUNCTION</syntaxhighlight>
 
=={{header|Arturo}}==
Line 703 ⟶ 673:
</pre>
 
=={{header|BASIC256BASIC}}==
==={{header|ANSI BASIC}}===
{{trans|BBC BASIC}} - Lanczos method.
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">100 DECLARE EXTERNAL FUNCTION FNlngamma
110
120 DEF FNgamma(z) = EXP(FNlngamma(z))
130
140 FOR x = 0.1 TO 2.05 STEP 0.1
150 PRINT USING$("#.#",x), USING$("##.############", FNgamma(x))
160 NEXT x
170 END
180
190 EXTERNAL FUNCTION FNlngamma(z)
200 DIM lz(0 TO 6)
210 RESTORE
220 MAT READ lz
230 DATA 1.000000000190015, 76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.0012086509738662, -0.000005395239385
240 IF z < 0.5 THEN
250 LET FNlngamma = LOG(PI / SIN(PI * z)) - FNlngamma(1.0 - z)
260 EXIT FUNCTION
270 END IF
280 LET z = z - 1.0
290 LET b = z + 5.5
300 LET a = lz(0)
310 FOR i = 1 TO 6
320 LET a = a + lz(i) / (z + i)
330 NEXT i
340 LET FNlngamma = (LOG(SQR(2*PI)) + LOG(a) - b) + LOG(b) * (z+0.5)
350 END FUNCTION</syntaxhighlight>
{{out}}
<pre>
.1 9.513507698670
.2 4.590843712000
.3 2.991568987689
.4 2.218159543760
.5 1.772453850902
.6 1.489192248811
.7 1.298055332647
.8 1.164229713725
.9 1.068628702119
1.0 1.000000000000
1.1 .951350769867
1.2 .918168742400
1.3 .897470696306
1.4 .887263817503
1.5 .886226925453
1.6 .893515349288
1.7 .908638732853
1.8 .931383770980
1.9 .961765831907
2.0 1.000000000000
</pre>
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}} - Stirling method.
 
Line 737 ⟶ 761:
end function</syntaxhighlight>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
Uses the Lanczos approximation.
Line 1,260 ⟶ 1,284:
140,0 9,615723196940235E238
170,0 4,269068009004271E304</pre>
 
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight>
e = 2.718281828459
func stirling x .
return sqrt (2 * pi / x) * pow (x / e) x
.
print " X Stirling"
for i to 20
d = i / 10
numfmt 2 4
write d & " "
numfmt 3 4
print stirling d
.
</syntaxhighlight>
 
 
=={{header|Elixir}}==
Line 1,354 ⟶ 1,396:
90 : 1.65079551625067E+136
100 : 9.33262154536104E+155
</pre>
 
{{Trans|C#}}
The C# version can be translated to F# to support complex numbers:
 
<syntaxhighlight lang="f sharp">
open System.Numerics
open System
 
let rec gamma (z: Complex) =
let mutable z = z
let lanczosCoefficients = [| 676.520368121885; -1259.1392167224; 771.323428777653; -176.615029162141; 12.5073432786869; -0.13857109526572; 9.98436957801957E-06; 1.50563273514931E-07 |]
 
if z.Real < 0.5 then
Math.PI / (sin (Math.PI * z) * gamma (1.0 - z))
else
let mutable x = Complex.One
z <- z - 1.0
 
for i = 0 to lanczosCoefficients.Length - 1 do
x <- x + lanczosCoefficients.[i] / (z + Complex(i, 0) + 1.0)
 
let t = z + float lanczosCoefficients.Length - 0.5
sqrt (2.0 * Math.PI) * (t ** (z + 0.5)) * exp (-t) * x
 
Seq.iter (fun i -> printfn "Gamma(%f) = %A" i (gamma (Complex(i, 0)))) [ 0 .. 100 ]
Seq.iter2 (fun i j -> printfn "Gamma(%f + i%f) = %A" i j (gamma (Complex(i, j)))) [ 0 .. 100 ] [ 0 .. 100 ]
</syntaxhighlight>
 
{{out}}
<pre>
Gamma(0.000000) = (NaN, NaN)
Gamma(1.000000) = (1,0000000000000049, 0)
Gamma(2.000000) = (1,0000000000000115, 0)
Gamma(3.000000) = (2,0000000000000386, 0)
Gamma(4.000000) = (6,000000000000169, 0)
Gamma(5.000000) = (24,00000000000084, 0)
Gamma(6.000000) = (120,00000000000514, 0)
Gamma(7.000000) = (720,0000000000364, 0)
Gamma(8.000000) = (5040,000000000289, 0)
Gamma(9.000000) = (40320,000000002554, 0)
Gamma(10.000000) = (362880,00000002526, 0)
Gamma(11.000000) = (3628800,0000002664, 0)
Gamma(12.000000) = (39916800,000003114, 0)
Gamma(13.000000) = (479001600,00004023, 0)
Gamma(14.000000) = (6227020800,000546, 0)
Gamma(15.000000) = (87178291200,00801, 0)
Gamma(16.000000) = (1307674368000,1267, 0)
Gamma(17.000000) = (20922789888002,08, 0)
Gamma(18.000000) = (355687428096034,7, 0)
Gamma(19.000000) = (6402373705728658, 0)
Gamma(20.000000) = (1,2164510040884514E+17, 0)
Gamma(21.000000) = (2,4329020081769083E+18, 0)
Gamma(22.000000) = (5,109094217171516E+19, 0)
Gamma(23.000000) = (1,1240007277777331E+21, 0)
Gamma(24.000000) = (2,5852016738887927E+22, 0)
Gamma(25.000000) = (6,20448401733312E+23, 0)
Gamma(26.000000) = (1,5511210043332811E+25, 0)
Gamma(27.000000) = (4,032914611266542E+26, 0)
Gamma(28.000000) = (1,0888869450419632E+28, 0)
Gamma(29.000000) = (3,048883446117507E+29, 0)
Gamma(30.000000) = (8,841761993740793E+30, 0)
Gamma(31.000000) = (2,652528598122235E+32, 0)
Gamma(32.000000) = (8,222838654178949E+33, 0)
Gamma(33.000000) = (2,631308369337265E+35, 0)
Gamma(34.000000) = (8,683317618812963E+36, 0)
Gamma(35.000000) = (2,9523279903964145E+38, 0)
Gamma(36.000000) = (1,0333147966387422E+40, 0)
Gamma(37.000000) = (3,719933267899472E+41, 0)
Gamma(38.000000) = (1,3763753091228064E+43, 0)
Gamma(39.000000) = (5,230226174666675E+44, 0)
Gamma(40.000000) = (2,0397882081200028E+46, 0)
Gamma(41.000000) = (8,159152832480012E+47, 0)
Gamma(42.000000) = (3,3452526613168034E+49, 0)
Gamma(43.000000) = (1,4050061177530564E+51, 0)
Gamma(44.000000) = (6,041526306338149E+52, 0)
Gamma(45.000000) = (2,658271574788788E+54, 0)
Gamma(46.000000) = (1,1962222086549537E+56, 0)
Gamma(47.000000) = (5,502622159812779E+57, 0)
Gamma(48.000000) = (2,586232415112008E+59, 0)
Gamma(49.000000) = (1,2413915592537631E+61, 0)
Gamma(50.000000) = (6,082818640343433E+62, 0)
Gamma(51.000000) = (3,04140932017172E+64, 0)
Gamma(52.000000) = (1,551118753287575E+66, 0)
Gamma(53.000000) = (8,065817517095389E+67, 0)
Gamma(54.000000) = (4,27488328406056E+69, 0)
Gamma(55.000000) = (2,308436973392699E+71, 0)
Gamma(56.000000) = (1,2696403353659833E+73, 0)
Gamma(57.000000) = (7,109985878049497E+74, 0)
Gamma(58.000000) = (4,0526919504882125E+76, 0)
Gamma(59.000000) = (2,350561331283167E+78, 0)
Gamma(60.000000) = (1,386831185457067E+80, 0)
Gamma(61.000000) = (8,3209871127424E+81, 0)
Gamma(62.000000) = (5,075802138772854E+83, 0)
Gamma(63.000000) = (3,1469973260391715E+85, 0)
Gamma(64.000000) = (1,9826083154046777E+87, 0)
Gamma(65.000000) = (1,2688693218589942E+89, 0)
Gamma(66.000000) = (8,247650592083449E+90, 0)
Gamma(67.000000) = (5,443449390775078E+92, 0)
Gamma(68.000000) = (3,647111091819299E+94, 0)
Gamma(69.000000) = (2,48003554243712E+96, 0)
Gamma(70.000000) = (1,711224524281613E+98, 0)
Gamma(71.000000) = (1,1978571669971308E+100, 0)
Gamma(72.000000) = (8,504785885679606E+101, 0)
Gamma(73.000000) = (6,123445837689312E+103, 0)
Gamma(74.000000) = (4,470115461513189E+105, 0)
Gamma(75.000000) = (3,307885441519755E+107, 0)
Gamma(76.000000) = (2,4809140811398187E+109, 0)
Gamma(77.000000) = (1,8854947016662648E+111, 0)
Gamma(78.000000) = (1,451830920283022E+113, 0)
Gamma(79.000000) = (1,1324281178207572E+115, 0)
Gamma(80.000000) = (8,946182130783977E+116, 0)
Gamma(81.000000) = (7,1569457046271725E+118, 0)
Gamma(82.000000) = (5,797126020748008E+120, 0)
Gamma(83.000000) = (4,753643337013366E+122, 0)
Gamma(84.000000) = (3,945523969721089E+124, 0)
Gamma(85.000000) = (3,31424013456571E+126, 0)
Gamma(86.000000) = (2,8171041143808564E+128, 0)
Gamma(87.000000) = (2,4227095383675335E+130, 0)
Gamma(88.000000) = (2,1077572983797526E+132, 0)
Gamma(89.000000) = (1,8548264225741817E+134, 0)
Gamma(90.000000) = (1,650795516091023E+136, 0)
Gamma(91.000000) = (1,4857159644819212E+138, 0)
Gamma(92.000000) = (1,3520015276785438E+140, 0)
Gamma(93.000000) = (1,2438414054642616E+142, 0)
Gamma(94.000000) = (1,1567725070817618E+144, 0)
Gamma(95.000000) = (1,0873661566568553E+146, 0)
Gamma(96.000000) = (1,032997848824012E+148, 0)
Gamma(97.000000) = (9,916779348710516E+149, 0)
Gamma(98.000000) = (9,619275968249195E+151, 0)
Gamma(99.000000) = (9,42689044888421E+153, 0)
Gamma(100.000000) = (9,33262154439535E+155, 0)
Gamma(0.000000 + i0.000000) = (NaN, NaN)
Gamma(1.000000 + i1.000000) = (0,49801566811835923, -0,15494982830180806)
Gamma(2.000000 + i2.000000) = (0,11229424234632254, 0,3236128855019324)
Gamma(3.000000 + i3.000000) = (-0,4401134076370088, -0,0636372431263299)
Gamma(4.000000 + i4.000000) = (0,7058649325913451, -0,49673908399741584)
Gamma(5.000000 + i5.000000) = (-0,9743952418053669, 2,0066898827226805)
Gamma(6.000000 + i6.000000) = (1,0560845455210948, -7,123931816061554)
Gamma(7.000000 + i7.000000) = (-0,26095668519941206, 27,88827411508434)
Gamma(8.000000 + i8.000000) = (1,8442848156317595, -125,96060801752867)
Gamma(9.000000 + i9.000000) = (-94,00399991734474, 643,3621714431141)
Gamma(10.000000 + i10.000000) = (1423,851941789479, -3496,081973308168)
Gamma(11.000000 + i11.000000) = (-16211,00700465313, 18168,810510285286)
Gamma(12.000000 + i12.000000) = (158471,8890918886, -68793,30331463458)
Gamma(13.000000 + i13.000000) = (-1329505,1052081874, -142199,12520863872)
Gamma(14.000000 + i14.000000) = (8576976,67312178, 7218722,503716219)
Gamma(15.000000 + i15.000000) = (-20768001,573587183, -99064686,32101583)
Gamma(16.000000 + i16.000000) = (-490395650,85195, 847486174,9207268)
Gamma(17.000000 + i17.000000) = (9782747798,66319, -2523864726,357996)
Gamma(18.000000 + i18.000000) = (-91408144092,80728, -62548333665,79536)
Gamma(19.000000 + i19.000000) = (80368797570,63837, 1283152922013,1064)
Gamma(20.000000 + i20.000000) = (12322153606702,379, -9813622771583,531)
Gamma(21.000000 + i21.000000) = (-191651224429571,5, -67416801166719,305)
Gamma(22.000000 + i22.000000) = (476610838765573,75, 2709614130691551,5)
Gamma(23.000000 + i23.000000) = (31282423285710508, -23340622982977492)
Gamma(24.000000 + i24.000000) = (-5,062346571412891E+17, -2,8075410996386413E+17)
Gamma(25.000000 + i25.000000) = (-1,1135374386470528E+18, 8,889271476011264E+18)
Gamma(26.000000 + i26.000000) = (1,4103207063357242E+20, -3,1111347801608966E+19)
Gamma(27.000000 + i27.000000) = (-1,20394153792971E+21, -2,100813378567903E+21)
Gamma(28.000000 + i28.000000) = (-2,9772583255514483E+22, 2,9845666640271078E+22)
Gamma(29.000000 + i29.000000) = (6,474322395366405E+23, 4,002110222682179E+23)
Gamma(30.000000 + i30.000000) = (4,982468347052982E+24, -1,3332730971666784E+25)
Gamma(31.000000 + i31.000000) = (-2,701233953257487E+26, -5,3335652308619844E+25)
Gamma(32.000000 + i32.000000) = (-3,5481927258667466E+26, 5,492417944693437E+27)
Gamma(33.000000 + i33.000000) = (1,13499763334942E+29, -3,936060260026878E+27)
Gamma(34.000000 + i34.000000) = (-2,497617380231244E+29, -2,4036706229026682E+30)
Gamma(35.000000 + i35.000000) = (-5,244047476964302E+31, 7,550247174479453E+30)
Gamma(36.000000 + i36.000000) = (1,8326953464053433E+32, 1,1815797667494789E+33)
Gamma(37.000000 + i37.000000) = (2,7496330950708185E+34, -3,7903032739968576E+33)
Gamma(38.000000 + i38.000000) = (-6,153702881069039E+34, -6,593476804299637E+35)
Gamma(39.000000 + i39.000000) = (-1,622188754583588E+37, 3,702408035066972E+35)
Gamma(40.000000 + i40.000000) = (-2,9787072201603815E+37, 4,069596487794187E+38)
Gamma(41.000000 + i41.000000) = (1,0327223226588694E+40, 2,028448840265614E+39)
Gamma(42.000000 + i42.000000) = (9,258591867044077E+40, -2,623834405446389E+41)
Gamma(43.000000 + i43.000000) = (-6,5825696143104E+42, -3,6674407207212435E+42)
Gamma(44.000000 + i44.000000) = (-1,3470021394042014E+44, 1,5970917674147318E+44)
Gamma(45.000000 + i45.000000) = (3,611191294816286E+45, 4,700641025433376E+45)
Gamma(46.000000 + i46.000000) = (1,572050028597055E+47, -6,978410499639601E+46)
Gamma(47.000000 + i47.000000) = (-8,064316457005957E+47, -5,037500759309252E+48)
Gamma(48.000000 + i48.000000) = (-1,5346852327090097E+50, -1,8750204416673013E+49)
Gamma(49.000000 + i49.000000) = (-1,963123049571723E+51, 4,3640542056764855E+51)
Gamma(50.000000 + i50.000000) = (1,112141672863102E+53, 1,0242389193853564E+53)
Gamma(51.000000 + i51.000000) = (4,3124175139534486E+54, -2,2723736598857867E+54)
Gamma(52.000000 + i52.000000) = (-1,9794169465380243E+55, -1,5907071359978385E+56)
Gamma(53.000000 + i53.000000) = (-5,198770735397539E+57, -1,364053254830618E+57)
Gamma(54.000000 + i54.000000) = (-1,120153853109903E+59, 1,4557034579325553E+59)
Gamma(55.000000 + i55.000000) = (3,0502206279504673E+60, 5,6213787579421754E+60)
Gamma(56.000000 + i56.000000) = (2,263901598439912E+62, -1,3869357395249425E+61)
Gamma(57.000000 + i57.000000) = (3,1337184675942048E+63, -7,566798379913671E+63)
Gamma(58.000000 + i58.000000) = (-1,9547779277051327E+65, -2,289059342042218E+65)
Gamma(59.000000 + i59.000000) = (-1,0990589957539216E+67, 2,43674092519427E+66)
Gamma(60.000000 + i60.000000) = (-1,2138821648921022E+68, 4,1070828497360523E+68)
Gamma(61.000000 + i61.000000) = (1,1429060333301634E+70, 1,199617402383565E+70)
Gamma(62.000000 + i62.000000) = (6,356301051435098E+71, -1,4385777048397157E+71)
Gamma(63.000000 + i63.000000) = (8,496122222382356E+72, -2,4629448359526944E+73)
Gamma(64.000000 + i64.000000) = (-6,555666234065589E+74, -8,308825832655404E+74)
Gamma(65.000000 + i65.000000) = (-4,35298630131942E+76, 3,566503620394996E+75)
Gamma(66.000000 + i66.000000) = (-9,093720755985274E+77, 1,5886817012950856E+78)
Gamma(67.000000 + i67.000000) = (3,276704637172729E+79, 7,067540192000104E+79)
Gamma(68.000000 + i68.000000) = (3,3000031716552424E+81, 6,606403155114497E+80)
Gamma(69.000000 + i69.000000) = (1,1027558271573558E+83, -9,8053446601259E+82)
Gamma(70.000000 + i70.000000) = (-4,322638823338879E+83, -6,551055369074648E+84)
Gamma(71.000000 + i71.000000) = (-2,4300476213174805E+86, -1,695901195606269E+86)
Gamma(72.000000 + i72.000000) = (-1,3066738432408389E+88, 3,647419995967721E+87)
Gamma(73.000000 + i73.000000) = (-2,631035116210543E+89, 5,722329713478048E+89)
Gamma(74.000000 + i74.000000) = (1,2168690154152985E+91, 2,7033309001464033E+91)
Gamma(75.000000 + i75.000000) = (1,3482397421660745E+93, 4,28037329878204E+92)
Gamma(76.000000 + i76.000000) = (5,937687180258459E+94, -3,3970662984923895E+94)
Gamma(77.000000 + i77.000000) = (7,877518886977203E+95, -3,258429077036584E+96)
Gamma(78.000000 + i78.000000) = (-8,893580405683355E+97, -1,4068641589865653E+98)
Gamma(79.000000 + i79.000000) = (-8,171120040126758E+99, -1,8182361788970335E+99)
Gamma(80.000000 + i80.000000) = (-3,620672062418723E+101, 2,252383956928435E+101)
Gamma(81.000000 + i81.000000) = (-5,470323981863164E+102, 2,130475015794634E+103)
Gamma(82.000000 + i82.000000) = (5,5003853678128614E+104, 1,0085770500872239E+105)
Gamma(83.000000 + i83.000000) = (5,740274728712029E+106, 1,986132310865518E+106)
Gamma(84.000000 + i84.000000) = (3,002634263016605E+108, -1,245707037705857E+108)
Gamma(85.000000 + i85.000000) = (7,865153067057636E+109, -1,575289481058232E+110)
Gamma(86.000000 + i86.000000) = (-2,2903303039804873E+111, -9,374401780068583E+111)
Gamma(87.000000 + i87.000000) = (-4,291880277570836E+113, -3,196185984412844E+113)
Gamma(88.000000 + i88.000000) = (-2,9995707183640408E+115, 1,1845722184191957E+114)
Gamma(89.000000 + i89.000000) = (-1,2943245318203042E+117, 1,1073023439932073E+117)
Gamma(90.000000 + i90.000000) = (-2,0007723042777158E+118, 9,568042990491017E+118)
Gamma(91.000000 + i91.000000) = (2,4147687416625443E+120, 5,132960890087842E+120)
Gamma(92.000000 + i92.000000) = (2,9270673985442265E+122, 1,5846423875074053E+122)
Gamma(93.000000 + i93.000000) = (1,9583971561168918E+124, -2,5166936071920716E+123)
Gamma(94.000000 + i94.000000) = (8,735240498823332E+125, -7,993043748530299E+125)
Gamma(95.000000 + i95.000000) = (1,6159775690644545E+127, -6,9922194609237035E+127)
Gamma(96.000000 + i96.000000) = (-1,569012926564151E+129, -4,10649302643008E+129)
Gamma(97.000000 + i97.000000) = (-2,2080981691082815E+131, -1,5902953016632018E+131)
Gamma(98.000000 + i98.000000) = (-1,6995881449793942E+133, -8,982249136857376E+131)
Gamma(99.000000 + i99.000000) = (-9,394719018633069E+134, 5,234766160326565E+134)
Gamma(100.000000 + i100.000000) = (-3,3597454530316526E+136, 5,986962556433683E+136)
</pre>
 
Line 2,046 ⟶ 2,321:
<pre>2.220446049250313e-16</pre>
 
=={{header|Koka}}==
Based on OCaml implementation
<syntaxhighlight lang="koka">
import std/num/float64
 
fun gamma-lanczos(x)
val g = 7.0
// Coefficients used by the GNU Scientific Library
val c = [0.99999999999980993, 676.5203681218851, -1259.1392167224028,
771.32342877765313, -176.61502916214059, 12.507343278686905,
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7]
fun ag(z: float64, d: int)
if d == 0 then c[0].unjust + ag(z, 1)
elif d < 8 then c[d].unjust / (z + d.float64) + ag(z, d.inc)
else c[d].unjust / (z + d.float64)
fun gamma(z)
val z' = z - 1.0
val p = z' + g + 0.5
sqrt(2.0 * pi) * pow(p, (z' + 0.5)) * exp(0.0 - p) * ag(z', 0)
gamma(x)
 
val e = exp(1.0)
fun gamma-stirling(x)
sqrt(2.0 * pi / x) * pow(x / e, x)
 
fun gamma-stirling2(x')
// Extended Stirling method seen in Abramowitz and Stegun
val d = [1.0/12.0, 1.0/288.0, -139.0/51840.0, -571.0/2488320.0]
fun corr(z, x, n)
if n < d.length - 1 then d[n].unjust / x + corr(z, x*z, n.inc)
else d[n].unjust / x
fun gamma(z)
gamma-stirling(z)*(1.0 + corr(z, z, 0))
gamma(x')
 
fun mirror(gma, z)
if z > 0.5 then gma(z) else pi / sin(pi * z) / gma(1.0 - z)
 
fun main()
println("z\tLanczos\t\t\tStirling\t\tStirling2")
for(1, 20) fn(i)
val z = i.float64 / 10.0
println(z.show(1) ++ "\t" ++ mirror(gamma-lanczos, z).show ++ "\t" ++
mirror(gamma-stirling, z).show ++ "\t" ++ mirror(gamma-stirling2, z).show)
for(1, 7) fn(i)
val z = 10.0 * i.float64
println(z.show ++ "\t" ++ gamma-lanczos(z).show ++ "\t" ++
gamma-stirling(z).show ++ "\t" ++ gamma-stirling2(z).show)
</syntaxhighlight>
 
{{out}}
 
<pre>
z Lanczos Stirling Stirling2
0.1 9.5135076986687359 10.405084329555955 9.5210418318004439
0.2 4.5908437119988017 5.0739927535371763 4.596862295030256
0.3 2.9915689876875904 3.3503395433773222 2.9984402802949961
0.4 2.218159543757686 2.5270578096699556 2.2277588907113128
0.5 1.7724538509055157 2.0663656770612464 1.7883901437260497
0.6 1.4891922488128184 1.3071588574483559 1.4827753636029286
0.7 1.2980553326475577 1.1590532921139201 1.2950806801024195
0.8 1.1642297137253037 1.0533709684256085 1.1627054102439229
0.9 1.068628702119319 0.97706150787769541 1.0677830813298756
1.0 1.0000000000000002 0.92213700889578909 0.99949946853364036
1.1 0.95135076986687361 0.88348995316870382 0.95103799705518899
1.2 0.91816874239976076 0.85775533539659088 0.91796405783487933
1.3 0.89747069630627774 0.84267825944839203 0.8973312868034562
1.4 0.88726381750307537 0.8367445486370817 0.88716548484542823
1.5 0.88622692545275827 0.83895655252649626 0.88615538430170204
1.6 0.89351534928769061 0.8486932421525738 0.89346184003019224
1.7 0.90863873285329122 0.86562147179388405 0.90859770150945562
1.8 0.93138377098024272 0.8896396352879945 0.93135158986107858
1.9 0.96176583190738729 0.92084272189422933 0.96174006762796482
2.0 1.0000000000000002 0.95950217574449159 0.99997898067003532
10 362880.00000000105 359869.56187410367 362879.99717458693
20 1.2164510040883245e+17 1.2113934233805675e+17 1.2164510037907557e+17
30 8.841761993739658e+30 8.8172365307655063e+30 8.8417619934546387e+30
40 2.0397882081197221e+46 2.0355431612365591e+46 2.0397882081041343e+46
50 6.0828186403425409e+62 6.0726891878763362e+62 6.0828186403274418e+62
60 1.3868311854568534e+80 1.3849063858294502e+80 1.3868311854555093e+80
70 1.7112245242813438e+98 1.7091885781910795e+98 1.711224524280615e+98
</pre>
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.0.6
Line 2,108 ⟶ 2,465:
2.00 0.959502175744492 1.000000000000000
</pre>
 
=={{header|Lambdatalk}}==
Following Javascript, with Lanczos approximation.
 
<syntaxhighlight lang="Scheme">
 
{def gamma.p
{A.new 0.99999999999980993
676.5203681218851
-1259.1392167224028
771.32342877765313
-176.61502916214059
12.507343278686905
-0.13857109526572012
9.9843695780195716e-6
1.5056327351493116e-7
}}
-> gamma.p
 
{def gamma.rec
{lambda {:x :a :i}
{if {< :i {A.length {gamma.p}}}
then {gamma.rec :x
{+ :a {/ {A.get :i {gamma.p}} {+ :x :i}} }
{+ :i 1}}
else :a
}}}
-> gamma.rec
 
{def gamma
{lambda {:x}
{if {< :x 0.5}
then {/ {PI}
{* {sin {* {PI} :x}}
{gamma {- 1 :x}}}}
else {let { {:x {- :x 1}}
{:t {+ {- :x 1} 7 0.5}}
} {* {sqrt {* 2 {PI}}}
{pow :t {+ :x 0.5}}
{exp -:t}
{gamma.rec :x {A.first {gamma.p}} 1}}
}}}}
-> gamma
 
{S.map {lambda {:i}
{div} Γ(:i) = {gamma :i}}
{S.serie -5.5 5.5 0.5}}
 
Γ(-5.5) = 0.010912654781909836
Γ(-5) = -42755084646679.17
Γ(-4.5) = -0.06001960130050417
Γ(-4) = 267219279041745.34
Γ(-3.5) = 0.27008820585226917
Γ(-3) = -1425169488222640
Γ(-2.5) = -0.9453087204829418
Γ(-2) = 6413262697001885
Γ(-1.5) = 2.363271801207352
Γ(-1) = -25653050788007544
Γ(-0.5) = -3.5449077018110295
Γ(0) = Infinity
Γ(0.5) = 1.7724538509055159
Γ(1) = 0.9999999999999998
Γ(1.5) = 0.8862269254527586
Γ(2) = 1.0000000000000002
Γ(2.5) = 1.3293403881791384
Γ(3) = 2.000000000000001
Γ(3.5) = 3.3233509704478426
Γ(4) = 6.000000000000007
Γ(4.5) = 11.631728396567446
Γ(5) = 23.999999999999996
Γ(5.5) = 52.34277778455358
</syntaxhighlight>
 
=={{header|Limbo}}==
Line 3,606 ⟶ 4,035:
{
z <- z - 1
x <- p[1] + sum(p[-1]/seq.int(z+1, z+g+1))
for (i in 1:8) {
x <- x+p[i+1]/(z+i)
}
tt <- z + g + 0.5
sqrt(2*pi) * tt^(z+0.5) * exp(-tt) * x
Line 3,926 ⟶ 4,358:
:<math>RecGamma(a) = \frac{1}{\Gamma(a)}</math>;
:<math>Pochhammer(a,x) = \frac{\Gamma(a+x)}{\Gamma(x)}</math>.
 
=={{header|RPL}}==
{{trans|Ada}}
≪ 1 -
{ 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 }
→ y a
≪ a DUP SIZE GET
a SIZE 1 - 1 '''FOR''' n
y * a n GET +
-1 '''STEP'''
INV
≫ ≫ '<span style="color:blue>GAMMA</span>' STO
 
.3 <span style="color:blue>GAMMA</span>
The built-in FACT instruction is obviously based on a similar Taylor formula, since it returns same results:
.3 1 - FACT
{{out}}
<pre>
2: 2.99156898769
1: 2.99156898769
</pre>
 
=={{header|Ruby}}==
Line 3,971 ⟶ 4,433:
2.0
2.7781584804376647
</pre>
 
=={{header|Rust}}==
====Stirling====
{{trans|AWK}}
 
<syntaxhighlight lang="rust">
use std::f64::consts;
 
fn main() {
let gamma = |x: f64| { assert_ne!(x, 0.0); (2.0*consts::PI/x).sqrt() * (x * (x/consts::E).ln()).exp()};
(1..=20).for_each(|x| {
let x = f64::from(x) / 10.0;
println!("{:.02} => {:.10}", x, gamma(x));
});
}
</syntaxhighlight>
 
{{out}}
<pre>
0.10 => 5.6971871490
0.20 => 3.3259984240
0.30 => 2.3625300363
0.40 => 1.8414763359
0.50 => 1.5203469011
0.60 => 1.3071588574
0.70 => 1.1590532921
0.80 => 1.0533709684
0.90 => 0.9770615079
1.00 => 0.9221370089
1.10 => 0.8834899532
1.20 => 0.8577553354
1.30 => 0.8426782594
1.40 => 0.8367445486
1.50 => 0.8389565525
1.60 => 0.8486932422
1.70 => 0.8656214718
1.80 => 0.8896396353
1.90 => 0.9208427219
2.00 => 0.9595021757
</pre>
 
Line 4,708 ⟶ 5,210:
2.000000
2.778158</pre>
 
===Stirling===
 
<syntaxhighlight lang="txrlisp">(defun gamma (x)
(* (sqrt (/ (* 2 %pi%)
x))
(expt (/ x %e%) x)))
 
(each ((i 1..11))
(put-line (pic "##.######" (gamma (/ i 3.0)))))</syntaxhighlight>
 
{{out}}
 
<pre> 2.156976
1.202851
0.922137
0.839743
0.859190
0.959502
1.149106
1.458490
1.945403
2.709764</pre>
 
===Lanczos===
 
{{trans|Haskell}}
 
The Haskell version calculates the natural log of the gamma function, which is why the function is called <code>gammaln</code>; we correct that here by calling <code>exp</code>:
 
<syntaxhighlight lang="txrlisp">(defun gamma (x)
(let* ((cof #(76.18009172947146 -86.50532032941677
24.01409824083091 -1.231739572450155
0.001208650973866179 -0.000005395239384953))
(ser0 1.000000000190015)
(x55 (+ x 5.5))
(tmp (- x55 (* (+ x 0.5) (log x55))))
(ser (+ ser0 (sum [mapcar / cof (succ x)]))))
(exp (- (log (/ (* 2.5066282746310005 ser) x)) tmp))))
 
(each ((i (rlist 0.1..1.0..0.1 2..10)))
(put-line (pic "##.# ######.######" i (gamma i))))</syntaxhighlight>
 
{{out}}
 
<pre> 0.1 9.513508
0.2 4.590844
0.3 2.991569
0.4 2.218160
0.5 1.772454
0.6 1.489192
0.7 1.298055
0.8 1.164230
0.9 1.068629
1.0 1.000000
2.0 1.000000
3.0 2.000000
4.0 6.000000
5.0 24.000000
6.0 120.000000
7.0 720.000000
8.0 5040.000000
9.0 40320.000000
10.0 362880.000000
</pre>
 
From Wikipedia Python code. Output is identical to above.
 
<syntaxhighlight lang="txrlisp">(defun gamma (x)
(if (< x 0.5)
(/ %pi%
(* (sin (* %pi% x))
(gamma (- 1 x))))
(let* ((cof #(676.5203681218851 -1259.1392167224028
771.32342877765313 -176.61502916214059
12.507343278686905 -0.13857109526572012
9.9843695780195716e-6 1.5056327351493116e-7))
(ser0 0.99999999999980993)
(z (pred x))
(tmp (+ z (len cof) -0.5))
(ser (+ ser0 (sum [mapcar / cof (succ z)]))))
(* (sqrt (* 2 %pi%))
(expt tmp (+ z 0.5))
(exp (- tmp))
ser))))
 
(each ((i (rlist 0.1..1.0..0.1 2..10)))
(put-line (pic "##.# ######.######" i (gamma i))))</syntaxhighlight>
 
=={{header|Visual FoxPro}}==
Line 4,839 ⟶ 5,429:
{{libheader|Wren-math}}
The ''gamma'' method in the Math class is based on the Lanczos approximation.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./math" for Math
 
var stirling = Fn.new { |x| (2 * Num.pi / x).sqrt * (x / Math.e).pow(x) }
Line 4,847 ⟶ 5,437:
for (i in 1..20) {
var d = i / 10
Fmt.print("$4.2f\t$16.14f\t$16.14f", d, stirling.call(d), Math.gamma(d))
System.write("%(Fmt.f(4, d, 2))\t")
System.write("%(Fmt.f(16, stirling.call(d), 14))\t")
System.print("%(Fmt.f(16, Math.gamma(d), 14))")
}</syntaxhighlight>
 
Line 4,858 ⟶ 5,446:
0.10 5.69718714897717 9.51350769866875
0.20 3.32599842402239 4.59084371199881
0.30 2.36253003626962 2.9915689876876099156898768759
0.40 1.84147633593624 2.21815954375769
0.50 1.52034690106628 1.77245385090552
Line 4,876 ⟶ 5,464:
1.90 0.92084272189423 0.96176583190739
2.00 0.95950217574449 1.00000000000000
</pre>
 
=={{header|XPL0}}==
{{trans|Ada}}
<syntaxhighlight lang "XPL0">function real Gamma (X);
real X, A, Y, Sum;
integer N;
begin
A \constant array (0..29) of Long_Float\ :=
[ 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
];
Y := X - 1.0;
Sum := A (29);
for N:= 29-1 downto 0 do
Sum := Sum * Y + A (N);
return 1.0 / Sum;
end \Gamma\;
 
\Test program:
integer I;
begin
Format(0, 14);
for I:= 1 to 10 do
[RlOut(0, Gamma (Float (I) / 3.0)); CrLf(0)];
end</syntaxhighlight>
{{out}}
<pre>
2.67893853470775E+000
1.35411793942640E+000
1.00000000000000E+000
8.92979511569249E-001
9.02745292950934E-001
1.00000000000000E+000
1.19063934875900E+000
1.50457548825154E+000
1.99999999999397E+000
2.77815847933857E+000
</pre>
 
2,042

edits