# Puzzle | Heads or Tails

Last Updated : 16 Aug, 2019

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` `    ``print``(getTails(``50``)) `

Output:

```7
```

Share your thoughts in the comments