mercredi 8 novembre 2017

Using for-loop for brain teaser

This is a little brain teaser of a R problem I trying to code.

Lets say you have 15 candles on a table. In three different rounds you will either light the given candles, or put them out, based on whether the candle is already lit or not. So if the given candle is already lit, then your action will be to put it out. On the other hand, if the candle is not lit, then your action will be to light it up.

Initially none of the candles are lit.

Firstly, you will light up every second candle, ie. candle number 2, 4, 6, 8, 10, 12 and 14.

Secondly, you will light up/put out every third candle, so candle number 3, 6, 9, 12 and 15.

Lastly, you will light up/put out every fifth candle, so candle number 5, 10 and 15.

In the end I would like to know which candles are lit up.

I have created a data frame with the two variables. The number of candles, and a binary variable, stating whether the candle is lit or not.

I am able to solve this manually with three different if-statements, but I would like to do it with a for-loop. Any suggestions on how I might do it?

Aucun commentaire:

Enregistrer un commentaire