Puzzle | Heads or Tails

50 coins are lined up on a table (initially all heads facing upwards). 50 people pass from the coins one by one in the following manner:

The first person comes and visits the coins numbered 1, 2, 3, 4, …. and flips the coins until an opposite face comes(For ex-if it was head initially, he/she flips it until the tail comes and vice versa.)
The second person comes and visits the coins numbered 2, 4, 6, 8….. and flips the coins.
Similarly, the third person comes and visits (3, 6, 9, 12, ……) and does the same.

The task is to find the final states of all the coins after 50th person passes i.e. whether a coin is facing heads or tails upwards?

Solution: The trick is to identify if a coin is being visited by people even or an odd number of times.
Pay attention to the coin number.

Only perfect squares have odd no. of factors, and every other number has even number of factors.

For all those coins which have an odd number of factors will be facing tails upwards and for all those coins which have even no. of factors will be facing heads upwards.

So, coins numbered 1, 4, 9, 16, 25…. will have tails and 2, 3, 5, 6, 7, 10, 11, … will have heads facing upwards.





from math import floor, sqrt
def getTails(n):
    return floor(sqrt(n))
if __name__ == "__main__":
    n = 50



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : unknownboy