-
Notifications
You must be signed in to change notification settings - Fork 0
/
lucky_ticket
73 lines (59 loc) · 2.05 KB
/
lucky_ticket
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import time
class LuckyNumberCounter:
def __init__(self, digitCount):
self.digitCount = digitCount
self.numberCount = 10 ** digitCount
def last_number(self):
number = ""
for i in range(self.digitCount):
number += "9"
return int(number)
def isLuckyNumber(self, number):
f_hal = 0
s_hal = 0
half = int(self.digitCount / 2)
num_dig = self.digitCount
i = str(number)
first = list(i)
if len(first) != num_dig:
first = (['0'] * (num_dig - len(first))) + first
print(first)
for j in range(int(len(first) / 2)):
f_hal += int(first[j])
for k in range(half, len(first)):
s_hal += int(first[k])
if f_hal == s_hal:
return True
else:
return False
def getLuckyNumberCount(self):
number = self.digitCount
last_number = self.last_number()
half = int(self.digitCount / 2)
count = 0
for i in range(0, last_number + 1):
f_hal = 0
s_hal = 0
i = str(i)
first = list(i)
if len(first) != number:
first = (['0'] * (number - len(first))) + first
# print(first)
for j in range(int(len(first) / 2)):
f_hal += int(first[j])
for k in range(half, len(first)):
s_hal += int(first[k])
if f_hal == s_hal:
count += 1
return count
digits = 6
lnc = LuckyNumberCounter(digits)
# print(lnc.last_number())
t_start = time.time_ns() # to measure execution speed
luckyNumbersTotal = lnc.getLuckyNumberCount()
print(luckyNumbersTotal)
print(f"Time elapsed: {(time.time_ns() - t_start) / 1e9} s") # to measure execution speed
luckyNumbersProbab = luckyNumbersTotal / lnc.numberCount
print(lnc.digitCount, "digit lucky numbers count:", luckyNumbersTotal)
print("Probability of getting a lucky number is:", luckyNumbersProbab, end=" ")
print("so chance is 1 in", 1 / luckyNumbersProbab)