Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to find out the number of rooms in which a prize can be hidden
Suppose in a game show there are 2n rooms arranged in a circle. One room contains a prize that participants must find. The rooms are numbered 1, 2, 3, ..., n, -n, -(n-1), ..., -1 in clockwise order. Each room has a door with a marking x that indicates the distance to the next room. If x is positive, the door opens to the xth room clockwise; if negative, it opens to the xth room counter-clockwise.
We need to find how many rooms can hide the prize such that participants cannot find it by following the door sequence.
Example
If the input is [[4, 2]], the output will be [2].
Here, there are 2×4 = 8 rooms numbered: 1, 2, 3, 4, -4, -3, -2, -1 clockwise. Starting from room 2, participants visit: 2 ? -4 ? -1 ? 1 ? 3 ? -2 ? -1 ? 1 ? 3 ? -2... Rooms 4 and -3 are never visited, so the prize can be hidden in 2 rooms.
Solution Approach
The solution uses number theory concepts including prime factorization and Euler's totient function to calculate unreachable rooms ?
import math
def prime_num_find(n):
p_nums = [2]
check = bytearray(n)
for value in range(3, n, 2):
if check[value]:
continue
p_nums.append(value)
for i in range(3 * value, n, 2 * value):
check[i] = 1
return p_nums
def factor_finder(p):
p_nums = prime_num_find(45000)
f_nums = {}
for value in p_nums:
if value * value > p:
break
while p % value == 0:
p //= value
f_nums[value] = f_nums.get(value, 0) + 1
if p > 1:
f_nums[p] = 1
return f_nums
def euler_func(p):
f_nums = factor_finder(p)
t_value = 1
for value in f_nums:
t_value *= (value - 1) * value ** (f_nums[value] - 1)
return t_value
def solve(input_array):
output = []
for item in input_array:
p, q = item[0], item[1]
r = 2 * p + 1
r //= math.gcd(r, q % r)
t_value = euler_func(r)
for value in factor_finder(t_value):
while t_value % value == 0 and pow(2, t_value // value, r) == 1:
t_value //= value
output.append(2 * p - t_value)
return output
# Test with the example
result = solve([[4, 2]])
print(result)
[2]
How It Works
Prime Number Generation: prime_num_find() uses the Sieve of Eratosthenes to generate prime numbers up to a given limit.
Prime Factorization: factor_finder() finds the prime factors and their powers for a given number.
Euler's Totient Function: euler_func() calculates ?(n), which counts integers less than n that are coprime to n.
Main Algorithm: The solution calculates the number of rooms that can be reached from the starting position, then subtracts from the total to find unreachable rooms.
Parameters
- n: Half the total number of rooms (total rooms = 2n)
- start_room: The room number where participants begin their search
Conclusion
This algorithm uses advanced number theory to solve the room reachability problem efficiently. It calculates how many rooms remain unreachable when following the door sequence, determining where a prize can be safely hidden.
