How to resolve the algorithm Gamma function step by step in the Crystal programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Gamma function step by step in the Crystal programming language
Table of Contents
Problem Statement
Implement one algorithm (or more) to compute the Gamma (
Γ
{\displaystyle \Gamma }
) function (in the real field only). If your language has the function as built-in or you know a library which has it, compare your implementation's results with the results of the built-in/library function. The Gamma function can be defined as: This suggests a straightforward (but inefficient) way of computing the
Γ
{\displaystyle \Gamma }
through numerical integration.
Better suggested methods:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Gamma function step by step in the Crystal programming language
Source code in the crystal programming language
# Taylor Series
def 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 ]
end
def taylor_gamma(x)
y = x.to_f - 1
1.0 / a.reverse.reduce(0) { |sum, an| sum * y + an }
end
# Lanczos Method
def p
[ 0.99999_99999_99809_93, 676.52036_81218_851, -1259.13921_67224_028,
771.32342_87776_5313, -176.61502_91621_4059, 12.50734_32786_86905,
-0.13857_10952_65720_12, 9.98436_95780_19571_6e-6, 1.50563_27351_49311_6e-7 ]
end
def lanczos_gamma(z)
# Reflection formula
z = z.to_f
if z < 0.5
Math::PI / (Math.sin(Math::PI * z) * lanczos_gamma(1 - z))
else
z -= 1
x = p[0]
(1..p.size - 1).each { |i| x += p[i] / (z + i) }
t = z + p.size - 1.5
Math.sqrt(2 * Math::PI) * t**(z + 0.5) * Math.exp(-t) * x
end
end
puts " Taylor Series Lanczos Method Builtin Function"
(1..27).each { |i| n = i/3.0; puts "gamma(%.2f) = %.14e %.14e %.14e" % [n, taylor_gamma(n), lanczos_gamma(n), Math.gamma(n)] }
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Gray code step by step in the 8080 Assembly programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Speech synthesis step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the JavaScript programming language