R, java codes for solving Math puzzles