-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainscript.py
More file actions
32 lines (22 loc) · 845 Bytes
/
mainscript.py
File metadata and controls
32 lines (22 loc) · 845 Bytes
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
import numpy as np
def minimal_weighings(number_of_balls):
"""
Calculates the minimum number of weighings needed to
find the heavier ball among `number_of_balls` balls.
Args:
number_of_balls: The number of balls (int type).
Returns:
The minimum number of weighings (int type).
"""
if number_of_balls <= 1:
return 0
else:
# Calculate the minimum number of weighings
minimal_number_of_weighings = int(np.floor(np.log2(number_of_balls - 1)) / np.log2(3)) + 1
return minimal_number_of_weighings
# Get the number of balls from the user
number_of_balls = int(input("Enter the number of balls: "))
# Calculate the minimum number of weighings
minimal_number_of_weighings = minimal_weighings(number_of_balls)
# Display the result
print(f"The minimum number of weighings is: {minimal_number_of_weighings}")