Problem 2: DOLLARS AND CENTS (536 Puzzles and Curious Problems, Henry Ernest, Dudeney)

May 28, 2014, 2:22 PM

# Problem 2: DOLLARS AND CENTS (536 Puzzles and Curious Problems, Henry Ernest, Dudeney)

# A man entered a store and spent one-half of the money that was in his pocket. 
# When he came out he found that he had just as many cents as he had dollars when he went in 
# and half as many dollars as he had cents when he went in. 
# How much money did he have on him when he entered?

solution=FALSE
dollar.before=0
repeat {
  if(solution==TRUE) break
  dollar.before=dollar.before+1
  for (i in seq(0,98,2)) {
    cent.before=i
    money.before=dollar.before+0.01*cent.before
    money.after=money.before/2
    dollar.after=floor(money.after)
    cent.after=round(100*(money.after-dollar.after),0)
    if(cent.after==dollar.before && dollar.after==0.5*cent.before) {
      print(money.before)
      solution=TRUE
      break}
  }
}
[1] 99.98