reset password
Author Message
rabbott
Posts: 1649
Posted 22:05 Feb 03, 2014 |

I'm reconsidering the insight award to Daniel for the Multiplication Game. His program gets these results, half of which I think are wrong. (I changed Daniel's program to return 1 or 2 for which player wins rather than Stan or Ollie.)

  game(162)   // Should be 1                       //> res7: Int = 1    Right answer
  game(163)   // Should be 2                       //> res8: Int = 1    Wrong answer
  game(2916) // Should be 1                       //> res9: Int = 1    Right answer
  game(2917) // Should be 2                       //> res10: Int = 1  Wrong answer

Daniel, would you check this out and see if you can defend your program.

________________________

Regarding my suggestion that the recursive version include an expression like this: (2 to 9).find( x => ...)

The expression really should be: (2 to 9).exists(x => <multiply by x and see if you win>)

This generates a depth first search. The expression above asks whether there a number between 2 and 9 (inclusive) so that if it is used as a multiplier, then the current player wins. If so, the current player wins. If not, the other player wins. That's really the whole program. Can you put it into Scala? To do so use the line above as the condition of an if/else statement. It takes some time if you run it with a large number, but it seems to get the right answer.

 

Last edited by rabbott at 22:06 Feb 03, 2014.