Skip to content

Commit ead3ef7

Browse files
slowy07github-actions
and
github-actions
authored
chore: menambahkan fungsi gamma (#329)
* chore: menambahkan fungsi gamma fungsi gamma adalah generalisasi dari fungsi faktorial ke bilangan ril dan bilangan kompleks Signed-off-by: slowy07 <[email protected]> * docs: update DIRECTORY.md --------- Signed-off-by: slowy07 <[email protected]> Co-authored-by: github-actions <[email protected]>
1 parent 476f09c commit ead3ef7

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@
257257
* [Exponential Function](https://github.com/bellshade/Python/blob/main/math/exponential_function.py)
258258
* [Faktor Prima](https://github.com/bellshade/Python/blob/main/math/faktor_prima.py)
259259
* [Fibonacci](https://github.com/bellshade/Python/blob/main/math/fibonacci.py)
260+
* [Fungsi Gamma](https://github.com/bellshade/Python/blob/main/math/fungsi_gamma.py)
260261
* [Geometric Sum](https://github.com/bellshade/Python/blob/main/math/geometric_sum.py)
261262
* [Intergration](https://github.com/bellshade/Python/blob/main/math/intergration.py)
262263
* [Is Prime](https://github.com/bellshade/Python/blob/main/math/is_prime.py)

math/fungsi_gamma.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import math
2+
3+
4+
def fungsi_gamma(nilai: float) -> float:
5+
"""
6+
Hitung nilai fungsi gamma untuk `nilai`
7+
fungsi ini hanya mengdukung bilangan bulat seperti
8+
(1, 2, 3, 4 ... n) atau nilai pecahan, berkoma
9+
(0.1, 2.5, 3.4, 3.3 ... n)
10+
11+
informasi relevan tentang fungsi gamma:
12+
- https://en.wikipedia.org/wiki/Gamma_function
13+
- https://mathworld.wolfram.com/GammaFunction.html
14+
15+
Parameter:
16+
nilai(float): nilai yang diberikan
17+
18+
Return:
19+
(float): hasil kalkulasi fungsi gamma
20+
21+
Contoh:
22+
>>> fungsi_gamma(0.5)
23+
1.7724538509055159
24+
>>> hitung_gamma(1)
25+
1.0
26+
>>> hitung_gamma(3.5)
27+
3.3233509704478426
28+
"""
29+
# memastikan nilai tidak kurang atau sama dengan 0
30+
# atau negatif
31+
if nilai <= 0:
32+
raise ValueError("nilai harus lebih besar dari nol")
33+
34+
# membuat batasan maks dari nilai yang diberikan
35+
# fungsi logika ini hanya bersifat opsional
36+
if nilai > 175.5:
37+
raise OverflowError("nilai rentang terlalu besar")
38+
39+
# memastikan nilai adalh bilangan bulat atau pecahan
40+
if nilai - int(nilai) not in (0, 0.5):
41+
raise NotImplementedError("nilai harus bilangan bulat atau berkoma")
42+
43+
# buat basis rekursi dari gamma gamma(0.5) = sqrt(pi)
44+
if nilai == 0.5:
45+
return math.sqrt(math.pi)
46+
47+
# buat basis rekursi jika gamma(1) = 1
48+
if nilai == 1:
49+
return 1.0
50+
51+
return (nilai - 1) * fungsi_gamma(nilai - 1)
52+
53+
54+
if __name__ == "__main__":
55+
import doctest
56+
57+
doctest.testmod()
58+
59+
# contoh pemanggilan
60+
# print(fungsi_gamma(1))
61+
62+
# nilai: float = 2.5
63+
# print(f"hasil fungsi_gamma({nilai}) adalah: {fungsi_gamma(nilai)}")

0 commit comments

Comments
 (0)