Smallest number of turns needed to flip $P$ pencils, $N$ per turn, until they all point opposite their starting direction

87 Views Asked by At

Imagine you have P pencils all faced away from you and you can flip them N at a time. Anytime a pencil is flipped its direction changes 180 degrees. Every time you have flipped N pencils that is considered one turn. You may not flip the same pencil twice in one turn. The goal is to flip all the pencils so that if they all start away from you then when you are finished they all end up towards you. Also, you can assume whole number N and P, and that P is greater than or equal to N. If T is the numbers of turns, what is the smallest value of T given P and N. What would be some sort of algorithm for this. I know there probably isn't just one function as there are many different patterns I see in some of the data I got, but is there some group of functions or logic statements I can use to solve this problem in a way where I can just plug in numbers into a function if P and fit certain requirements? Thanks!

1

There are 1 best solutions below

6
On

After $T$ turns you have done $TN$ total flips of pencils. You need to have flipped each pencil an odd number of times.

If $P$ is odd and $N$ is even you will never get there because you need an odd number of total flips and get them an even number at a time.

If $P$ is odd and $N$ is odd you need $T$ to be odd as well. The minimum $T$ will be the lowest odd number that is at least $\frac PN$.

If $P$ is even you need $TN$ to be even as well. If $N$ is odd you need $T$ to be even, so the minimum $T$ will be the lowest even number that is at least $\frac PN$. There is an exception that $2$ is only acceptable if $\frac PN$ is exactly $2$. Otherwise you need $4$. For example, if $P=12,N=9$ you would like to flip nine pencils once and three pencils three times, but you can't flip a pencil three times when $T=2$.

If $P$ and $N$ are both even there is no restriction on the parity of $T$, so you just need the lowest whole number at least $\frac PN$ except that you need $3$ unless $\frac PN$ is whole.