Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@
* [Exponential Function](https://github.com/bellshade/Python/blob/main/math/exponential_function.py)
* [Faktor Prima](https://github.com/bellshade/Python/blob/main/math/faktor_prima.py)
* [Fibonacci](https://github.com/bellshade/Python/blob/main/math/fibonacci.py)
* [Fungsi Gamma](https://github.com/bellshade/Python/blob/main/math/fungsi_gamma.py)
* [Geometric Sum](https://github.com/bellshade/Python/blob/main/math/geometric_sum.py)
* [Intergration](https://github.com/bellshade/Python/blob/main/math/intergration.py)
* [Is Prime](https://github.com/bellshade/Python/blob/main/math/is_prime.py)
Expand Down
63 changes: 63 additions & 0 deletions math/fungsi_gamma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import math


def fungsi_gamma(nilai: float) -> float:
"""
Hitung nilai fungsi gamma untuk `nilai`
fungsi ini hanya mengdukung bilangan bulat seperti
(1, 2, 3, 4 ... n) atau nilai pecahan, berkoma
(0.1, 2.5, 3.4, 3.3 ... n)

informasi relevan tentang fungsi gamma:
- https://en.wikipedia.org/wiki/Gamma_function
- https://mathworld.wolfram.com/GammaFunction.html

Parameter:
nilai(float): nilai yang diberikan

Return:
(float): hasil kalkulasi fungsi gamma

Contoh:
>>> fungsi_gamma(0.5)
1.7724538509055159
>>> hitung_gamma(1)
1.0
>>> hitung_gamma(3.5)
3.3233509704478426
"""
# memastikan nilai tidak kurang atau sama dengan 0
# atau negatif
if nilai <= 0:
raise ValueError("nilai harus lebih besar dari nol")

# membuat batasan maks dari nilai yang diberikan
# fungsi logika ini hanya bersifat opsional
if nilai > 175.5:
raise OverflowError("nilai rentang terlalu besar")

# memastikan nilai adalh bilangan bulat atau pecahan
if nilai - int(nilai) not in (0, 0.5):
raise NotImplementedError("nilai harus bilangan bulat atau berkoma")

# buat basis rekursi dari gamma gamma(0.5) = sqrt(pi)
if nilai == 0.5:
return math.sqrt(math.pi)

# buat basis rekursi jika gamma(1) = 1
if nilai == 1:
return 1.0

return (nilai - 1) * fungsi_gamma(nilai - 1)


if __name__ == "__main__":
import doctest

doctest.testmod()

# contoh pemanggilan
# print(fungsi_gamma(1))

# nilai: float = 2.5
# print(f"hasil fungsi_gamma({nilai}) adalah: {fungsi_gamma(nilai)}")
Loading