Skip to content

Commit 178b6ff

Browse files
authored
Merge pull request #339 from Ateng-labrador/lucky
feat: menambahkan algoritma lucky number
2 parents e30fb10 + 61ff6af commit 178b6ff

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

other/lucky_number.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Algoritma untuk mengecek apakah bilangan itu lucky number
2+
# https://en.wikipedia.org/wiki/Lucky_number
3+
def is_lucky_number(number):
4+
"""
5+
Lucky number adalah angka yang selamat dari persortiran
6+
angka berdasarkan posisi mirip Sieve.
7+
8+
>>> is_lucky_number(1)
9+
'Angka Lucky'
10+
>>> is_lucky_number(21)
11+
'Angka Lucky'
12+
>>> is_lucky_number(9)
13+
'Angka Lucky'
14+
>>> is_lucky_number(10)
15+
'Bukan Angka Lucky'
16+
>>> is_lucky_number(1232)
17+
'Bukan Angka Lucky'
18+
>>> is_lucky_number(700)
19+
'Bukan Angka Lucky'
20+
21+
"""
22+
benar , bukan = "Angka Lucky" , "Bukan Angka Lucky"
23+
MAX_NUMBER = 10000
24+
25+
idx = 1
26+
nbr = []
27+
for i in range(1 , MAX_NUMBER):
28+
if i % 2 != 0:
29+
nbr.append(i)
30+
31+
while idx < len(nbr):
32+
step = nbr[idx]
33+
if step > len(nbr):
34+
break
35+
36+
new_numbers = []
37+
pos = 1
38+
for val in nbr:
39+
if pos % step != 0:
40+
new_numbers.append(val)
41+
pos += 1
42+
nbr = new_numbers
43+
idx += 1
44+
45+
if number in nbr:
46+
return benar
47+
else:
48+
return bukan
49+
50+
51+
def main(args=None):
52+
import doctest
53+
54+
doctest.testmod()
55+
56+
print(is_lucky_number(1)) # Angka Lucky
57+
print(is_lucky_number(21)) # Angka Lucky
58+
print(is_lucky_number(9)) # Angka Lucky
59+
print(is_lucky_number(10)) # Bukan Angka Lucky
60+
print(is_lucky_number(80)) # Bukan Angka Lucky
61+
print(is_lucky_number(897)) # Bukan Angka Lucky
62+
63+
64+
if __name__ == "__main__":
65+
main()

0 commit comments

Comments
 (0)