'''
February 2019 Puzzler
Dave Vogel - dcvogel@gmail.com
https://sites.monroecc.edu/mathpuzzler/2019/02/01/february-2019-puzzle/
Start with 10,000 quarters, all "heads up" and lined up in a (long) row.
Number each quarter starting from the left and working to the right (1st,
2nd, 3rd, etc...). Then carry out the following procedure:
Step 1: Turn over every quarter
Step 2: Turn over every second quarter (2nd, 4th, 6th, etc...)
Step 3: Turn over every third quarter (3rd, 6th, 9th, etc...)
⋮
Step 10,000: Turn over the ten-thousandth quarter
So in general, in Step n, turn over every quarter that is in a position
that is a multiple of n.
At the end of Step 10,000, which coins will be heads up and which will
be tails up?
'''
from __future__ import print_function
class Coin(object):
'''
A Coin has 2 states 'H' (heads) or 'T' (tails). A flip() changes H to T
and T to H.
'''
def __init__(self):
self.face = 'H' #Coins start with heads up
def flip(self):
if self.face == 'H':
self.face = 'T'
else:
self.face = 'H'
def __repr__(self):
return self.face
if __name__ == '__main__':
coins = ['X'] #Start the coins array with a dummy entry
# so subscrips go 1 -> 10000 rather than
# 0 -> 9999
#Create the 10000 coins
numOfCoins = 10000
for n in range(1,numOfCoins+1):
coins.append(Coin())
for step in range(1,numOfCoins+1): #For each step 1 -> 10000
for coin in range(step,numOfCoins+1,step): # flip each coin that is
coins[coin].flip() # that is a multiple of step
#Display coins that are tails up
print('The following coins are tails up:')
for coin in range(1, numOfCoins+1):
if coins[coin].face == 'T':
print(coin)