- 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 check whether Amal can win stone game or not in Python

Suppose there are two players Amal and Bimal, they are playing a game, and with Amal starts first. Initially, there are n different stones in a pile. On each player's turn, he makes a move consisting of removing any square number (non-zero) of stones from the pile. Also, if one player is unable to make a move, he loses the game. So if we have n, we have to check whether Amal can win the game or not.

So, if the input is like n = 21, then the output will be True because at first Amal can take 16, then Bimal takes 4, then Amal takes 1 and wins the game.

To solve this, we will follow these steps −

squares := a new list

square := 1

increase := 3

while square <= n, do

insert square at the end of squares

square := square + increase

increase := increase + 2

insert square at the end of squares

dp := a blank list of size (n + 1)

dp[0] := False

for k in range 1 to n, do

s := 0

dp[k] := False

while squares[s] <= k and dp[k] is empty, do

if dp[k - squares[s]] is empty, then

dp[k] := True

s := s + 1

return last element of dp

## Example

Let us see the following implementation to get better understanding

def solve(n): squares = [] square = 1 increase = 3 while square <= n: squares.append(square) square += increase increase += 2 squares.append(square) dp = [None] * (n + 1) dp[0] = False for k in range(1, n + 1): s = 0 dp[k] = False while squares[s] <= k and not dp[k]: if not dp[k - squares[s]]: dp[k] = True s += 1 return dp[-1] n = 21 print(solve(n))

## Input

21

## Output

True

- Related Articles
- Program to check whether first player win in candy remove game or not in Python?
- Program to check person 1 can win the candy game by taking maximum score or not in Python
- Program to check whether first player can win a game where players can form string char by char in C++
- Python program to check whether we can pile up cubes or not
- Program to check whether we can take all courses or not in Python
- Program to check whether we can unlock all rooms or not in python
- Program to check whether all can get a seat or not in Python
- Program to check whether we can get N queens solution or not in Python
- Program to check whether one point can be converted to another or not in Python
- Program to check whether we can convert string in K moves or not using Python
- Program to check whether parentheses are balanced or not in Python
- Program to find winner of stone game in Python
- C++ program to check xor game results 0 or not
- Program to check whether two trees can be formed by swapping nodes or not in Python
- Program to check whether we can split list into consecutive increasing sublists or not in Python