Python: Solving A Simple Puzzle During Lock-down

It is no surprise that the whole world is under lock-down for Covid-19. A puzzle came my way and I was ready for it.

The Puzzle:

Here is how the puzzle goes:

A master asked his employee to count the number of harvested coconuts while he ran an errand. When the master returned, he asked his employee what the count was to which the employee retorted, “When I count in Two’s, there is 1 remaining. I tried counting in Three’s, still there was 1 remaining. I tried counting in Four’s and yet there was 1 remaining. I kept going and tried counting in Five’s and still had got 1 remaining. I moved on to counting in Six’es and even then there was 1 remaining. Finally, when I counted in Seven’s, there was none remaining.

Immediately, the master knew how many coconuts there were. Do you?

Manually:

I started doing this in my mind with these assumptions

  1. The answer is a multiple of 7.
  2. It is an odd number (since divided by 2 yields 1)
  3. It ends in 1 or 6 (since dividing by 5 has a remainder of 1)
    • Since it has to be an odd number the answer has to end in 1

Quite simply, I tried going up manually for numbers ending in 1 that are divisible by 7. My math is weak and I quickly gave up!

Programmatically – Version 1:

I decided to use this as a learning opportunity and decided to sit down with my daughter and do a program in Python to solve this but I don’t use the “manual” logic from above. This is also my first Python program! Yay!

By the way, you can try it here online without having to download anything. Python is all about forced “Code Indentation” which I very much like.

Here is the first cut:

#Start with our first guess
guess = 7

for guessCounter in range(1, 10000):
  guess = guess + 7

  #Divide our guess by 2, 3, 4, 5 and 6
  for i in range(2, 7):

    #Assume our guess is good until proven otherwise!
    isGuessGood = True

    #If we get a dont get a remainder of 1 (modulus is 1), guess is bad
    if ((guess % i) != 1):
      #print("Guess number {} divisor {} modulus is NOT 1 but {}".format(guess, i, (guess % i)))
      isGuessGood = False
      break

  #If we get a remainder of 1 for mod by 2, 3, 4, 5, 6 then our guess is likely good
  if (isGuessGood == True):
    # ..confirm if it is still good for mod of 0 when dividing by 7
    if ((guess % 7) == 0):
      print("Guess number {} is our answer".format(guess))

      #Comment this break to find all the answers instead of just the first!!
      break

The code should be pretty straight-forward with my basic comments!

The output I got is:

Guess number 301 is our answer

When I commented out the last break to get the list of all answers here is the result:

Guess number 301 is our answer
Guess number 721 is our answer
Guess number 1141 is our answer
Guess number 1561 is our answer
Guess number 1981 is our answer
Guess number 2401 is our answer
Guess number 2821 is our answer
Guess number 3241 is our answer
Guess number 3661 is our answer
Guess number 4081 is our answer
Guess number 4501 is our answer
Guess number 4921 is our answer
Guess number 5341 is our answer
Guess number 5761 is our answer
Guess number 6181 is our answer
Guess number 6601 is our answer
Guess number 7021 is our answer
Guess number 7441 is our answer
Guess number 7861 is our answer
Guess number 8281 is our answer
Guess number 8701 is our answer
Guess number 9121 is our answer
Guess number 9541 is our answer
Guess number 9961 is our answer
..and so on

Programmatically – Version 2:

After we got v1 working (by mostly cutting/pasting from StackOverflow), my daughter wanted to be an interactive game to let the player input a guess and for the program to say that it is right or wrong. Like any game, it had to give the player [x] chances. So, that version is below with very minor changes to v1.

print("Your goal is to guess a number that has a remainder of 1 when divided by 2, 3, 4, 5 and 6 but a remainder of 0 when divided by 7. You only have 5 tries. Lets GO!")

#Get the input guess from the user
for guessCount in range(1,6):
  userGuess = input("Input your guess(It should be a multiple of 7): ")

  if (userGuess.isdigit() == False):
    print("Your input is not a number!")
    continue

  #Convert the input into an integer
  guess = (int)(userGuess)

  #Divide our guess by 2, 3, 4, 5 and 6
  for i in range(2, 7):

    #Assume our guess is good until proven otherwise!
    isGuessGood = True

    #If we get a dont get a remainder of 1 (modulus is 1), guess is bad
    if ((guess % i) != 1):
      #print("Guess number {} divisor {} modulus is NOT 1 but {}".format(guess, i, (guess % i)))
      isGuessGood = False
      break

  if (isGuessGood == False):
    print("Sorry, guess number {} is incorrect".format(guess))

  #If we get a remainder of 1 for mod by 2, 3, 4, 5, 6 then our guess is likely good
  if (isGuessGood == True):
    # ..confirm if it is still good for mod of 0 when dividing by 7
    if ((guess % 7) == 0):
      print("Guess number {} is correct!!".format(guess))
      break  

  if (guessCount == 5):
    print("Game over! Your 5 tries are over.")

Here is the output on trying to play it with bad input/guesses

Your goal is to guess a number that has a remainder of 1 when divided by 2, 3, 4, 5 and 6 but a remainder of 0 when divided by 7. You only have 5 tries. Lets GO!
Input your guess(It should be a multiple of 7): abc
Your input is not a number!
Input your guess(It should be a multiple of 7): 31
Sorry, guess number 31 is incorrect
Input your guess(It should be a multiple of 7): 44
Sorry, guess number 44 is incorrect
Input your guess(It should be a multiple of 7): 67
Sorry, guess number 67 is incorrect
Input your guess(It should be a multiple of 7): 71
Sorry, guess number 71 is incorrect
Game over! Your 5 tries are over.

…and then again with the right answer

Your goal is to guess a number that has a remainder of 1 when divided by 2, 3, 4, 5 and 6 but a remainder of 0 when divided by 7. You only have 5 tries. Lets GO!
Input your guess(It should be a multiple of 7): 301
Guess number 301 is correct!!

There it is! Please give it a try and see if you like it. It is a fun way to get initiated into Python!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s