
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find winner of a set element removal game in Python
Suppose We have a set of first n natural numbers {1..n}. Amal and Bimal are playing a game.The game rules are like below
Amal always plays first
During each move, the current player selects a prime number p from the set. The player then removes p and all of its multiples from the set.
Whoever has no move will lose the game If we have n, we have to find the winner name.
So, if the input is like n = 5, then the output will be Amal, because the initial set is {1,2,3,4,5}. Now let Amal selects a number p = 2, and removes 2, 4 from the set, so current set is {1,3,5}, there are two primes left, so Bimal may select any of them but there are no remaining elements to remove and finally Amal removes another prime and win the game.
To solve this, we will follow these steps −
- primes := an array of size 100000, initially all are 0
- sieve := an array of size 100000, initially all are 0
- for i in range 2 to 99999, do
- if sieve[i] is same as 0, then
- primes[i] := primes[i-1]+1
- for j in range i to 100000, update in each step by i, do
- sieve[j] := i
- otherwise,
- primes[i] := primes[i-1]
- if sieve[i] is same as 0, then
- From the main method do the following −
- return "Bimal" if primes[n] is odd otherwise "Amal"
Example
Let us see the following implementation to get better understanding −
primes = [0 for i in range(100001)] sieve = [0 for i in range(100001)] for i in range(2, 100000): if sieve[i] == 0: primes[i] = primes[i-1]+1 for j in range(i, 100001, i): sieve[j] = i else: primes[i] = primes[i-1] def solve(n): return "Bimal" if primes[n] % 2 == 0 else "Amal" n = 5 print(solve(n))
Input
5
Output
Amal
- Related Articles
- Program to find winner of array removal game in Python
- C++ program to find winner of ball removal game
- Program to find winner of stone game in Python
- Program to find winner of a rower reducing game in Python
- Program to find winner of a rower breaking game in Python
- Program to find winner of number reducing game in Python
- Program to find maximum score of brick removal game in Python
- Program to find the winner of an array game using Python
- C++ program to find winner of card game
- Python program to find score and name of winner of minion game
- C++ program to find winner of cell coloring game
- C++ Program to find winner of unique bidding game
- C++ Program to find winner name of stick crossing game
- C++ Program to find maximum score of bit removal game
- C++ program to find winner of typing game after delay timing
