[PYTHON] Let's reproduce the math teaching tool "Jamaica" ❗️ vol.03 "Aiming for a solution in Jamaica ~ Round 1 ~"

Introduction

This series is about a certain ** math teaching tool "Jamaica" </ font> ** It is a record of struggling to achieve the following objectives.

** ✔︎ </ font> 1. ** *** You will be able to run the "Jamaica" game with a program that uses image display. *** *** ** ▶ ︎ </ font> 2. ** *** Create a program that searches and presents the existence of solutions and their contents for any combination of dice in "Jamaica". *** ***

Review up to the last time

Until the last time, we dealt with the following contents.

*** §1. Reproduce "Jamaica" </ font> *** *** Task.001 Display "Dice roll" as an image *** *** Task.002 "Display the dice roll image according to a random number" *** *** Task.003 "Create a function for displaying dice roll images" ***

This will

--Roll the dice randomly --Display side by side the dice roll images corresponding to the result of rolling

You can now perform the basic Jamaican behavior with just a few lines of code.

** _____ [Reference article] _____ ** [^ ref001] Recollection | Let's reproduce the math teaching tool "Jamaica" ❗️'s previous article (vol.01) [^ ref002] Recollection | Let's reproduce the math teaching tool "Jamaica" ❗️'s previous article (vol.02)

task003_jmcdisplay


### function trial START ###
libimports()
figPlace = figplace()
jmcDice = jmc_diceroll(printOption=True)
jmc_display(figPlace, jmcDice, sizeRatio=1.4)
### function trial END ###
> Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
>Jamaican Dice Roll Results:[ 1  5  6  5  5 30  2]

qiita005_jamaica_task003_jmcdisplay.png

§2. Search for a solution for "Jamaica" </ font>

■ In Jamaica, the purpose is to create the sum of the black dice by performing four arithmetic operations (addition, subtraction, multiplication and division) using each number of the white dice once.

■ The numbers on the white dice can be calculated in any order.

Example)White dice: 2, 2, 3, 3,4 Black dice: 40,In the case of 5

  [1]White dice once → 2*2*3*4 - 3 = 48 - 3 = 45
  [2]Black dice sum → 40+ 5 = 45

Since it can be calculated, this set of four arithmetic operations is a solution.

Task.004 "Preparing four arithmetic functions based on Jamaican rules"

■ Calculation results that cannot be subtracted or divided must not continue after that. Example) White dice: 2, 2, 4, 5, 6 Black dice: 40, 5 Subtraction: 2-6 = -4 Division: 6/4 = 3/2 Cannot be used in intermediate calculations. ** * There is a rule that allows irreducible fractions. ** **

■ From the above, create the following four arithmetic functions.

A function that calculates the sum of two numbers: add()
A function that calculates the difference between two numbers: substract()* The result of subtracting the larger one from the smaller one is nan.
A function that calculates the product of two numbers: multiply()
A function that calculates the quotient of two numbers: divide()* If the result is not an integer, you need an option to accept or not.

** _____ [Reference article] _____ ** [^ ref003] Task004 | [What is NAN? ] Meaning and usage ➡︎ I realized that it would be good to use np.nan to indicate that output is not possible. (We hope that there will be no unintended outliers when determining the results later.)

task004_calcfunctions


def add(var1, var2):
  return var1 + var2

def substract(var1, var2):
  if var1 >= var2: #If the first argument is more than the second argument, perform subtraction
    return var1 - var2
  else: #If the first argument is smaller than the second argument, output nan so that the calculation does not proceed any further.
    return np.nan

def multiply(var1, var2):
  return var1 * var2

def divide(var1, var2, fracOption): #The third argument, fracOption, is an option that allows irreducible fractions only when True.
  if var2 == 0: #When dividing by 0, output nan so that the calculation does not proceed any further.
    return np.nan
  elif isinstance(var1, int) and isinstance(var1, int): #When both numbers are integers
    if not fracOption == True: #If you do not allow fractions
      if var1 >= var2 and var1 % var2 ==0: #Furthermore, if the first number is the second or more and is divisible, the quotient is output.
        return var1 // var2
      else: #If the first number is less than the second or not divisible, it prints nan so that the calculation does not proceed any further.
        return np.nan
    else: #If you want to allow fractions, just perform the division
      return var1 / var2
  else: #2 When either of the numbers is not an integer
    if not fracOption ==True: #If you don't allow fractions, print nan so that the calculation doesn't go any further.
      return np.nan
    else: #If you want to allow fractions, just perform the division
      return var1 / var2

task004_calcdisplay


### function trial START ###

#Whether to output the calculation result of substract with nan?
print('___Check subtraction___')
for num in range(8):
  print('6-',num,' = ',substract(6,num))

#The calculation result of divide is fracOption=Check with True
print('___Check divide 1: fracOption=True & varA is int___')
for num in range(8):
  print('6/',num,' = ',divide(6,num,fracOption=True))
#The calculation result of divide is fracOption=Check with False
print('___Check divide 2: fracOption=False & varA is int___')
for num in range(8):
  print('6/',num,' = ',divide(6,num,fracOption=False))
#The calculation result of divide is fracOption=True &Check the number to be divided with float
print('___Check divide 3: fracOption=True & varA is float___')
for num in range(8):
  print('6.5/',num,' = ',divide(6.5,num,fracOption=True))
#The calculation result of divide is fracOption=False &Check the number to be divided with float
print('___Check divide 4: fracOption=False & varA is float___')
for num in range(8):
  print('6.5/',num,' = ',divide(6.5,num,fracOption=False))

### function trial END ###
___Check subtraction___
6- 0  =  6
6- 1  =  5
6- 2  =  4
6- 3  =  3
6- 4  =  2
6- 5  =  1
6- 6  =  0
6- 7  =  nan
___Check divide 1: fracOption=True___
6/ 0  =  nan
6/ 1  =  6.0
6/ 2  =  3.0
6/ 3  =  2.0
6/ 4  =  1.5
6/ 5  =  1.2
6/ 6  =  1.0
6/ 7  =  0.8571428571428571
___Check divide 2: fracOption=False___
6/ 0  =  nan
6/ 1  =  6
6/ 2  =  3
6/ 3  =  2
6/ 4  =  nan
6/ 5  =  nan
6/ 6  =  1
6/ 7  =  nan
___Check divide 3: fracOption=True & varA is float___
6.5/ 0  =  nan
6.5/ 1  =  6.5
6.5/ 2  =  3.25
6.5/ 3  =  2.1666666666666665
6.5/ 4  =  1.625
6.5/ 5  =  1.3
6.5/ 6  =  1.0833333333333333
6.5/ 7  =  0.9285714285714286
___Check divide 4: fracOption=False & varA is float___
6.5/ 0  =  nan
6.5/ 1  =  nan
6.5/ 2  =  nan
6.5/ 3  =  nan
6.5/ 4  =  nan
6.5/ 5  =  nan
6.5/ 6  =  nan
6.5/ 7  =  nan

Task.005 "Consider a function to search for a Jamaican solution"

■ The following ideas can be considered as a function that uses white dice to advance calculations that are candidates for solutions in Jamaica.

[step 1]5 white dice, that is, 5 integer values(1 or more and 6 or less)Select any two of them and arrange them (5P2)=20 combinations) and four arithmetic operations (4 types of addition, subtraction, multiplication and division) are performed.
[Step 2]the above[1]One number generated in and the remaining three integer values(1 or more and 6 or less)Select any two of them and arrange them(4P2=12 combinations), Perform four arithmetic operations (4 types of addition, subtraction, multiplication and division).
[Step 3]the above[1][2]Continue until one number is output in the same flow as.

■ The above function will have to change its output depending on the following input conditions:

(1)Set of input rolls and calculation results (np.array format)
(2) (1)Number of comprehensions of(=Array length)If it is 1, stop the calculation and make a match judgment.
(3)Whether to allow fractions(fracOption == True or False)

■ Also, since it is necessary to output a record of which number and order the four arithmetic operations were applied to, the following output will also have to be left.

(4)About the four arithmetic operations that calculated two numbers(_varA_ _Operation symbol_ _varB_)To record
Example) 2 *In the case of 5, "(2 * 5)To record

■ Considering the above, first define the following function.

■ Outputs calcDice, which stores calculated elements as calculable numbers, and calcDNames, which is a string of them.

[Function 1]  transForClac()
___Input: Array jmcDice containing the results of 7 dice
_____ Output: Array calcDice containing the sum of 5 white dice and 2 black dice

[Function 2] makeCalcName()
___Input: Array calcDice that stores the sum of 5 white dice and 2 black dice
_____ Output: Numbers converted to strings(A type that can be updated to a string of any length)Array calcDNames

task005_calcfunctions_1&2


# [Function 1]  transForClac()
#___Input: Array jmcDice containing the results of 7 dice
#_____ Output: Array calcDice containing the sum of 5 white dice and 2 black dice
###################################################

def transForCalc(jmcDice, printOption):
  calcDice = jmcDice.astype(np.float64) #When updating the calculation element later, np.nan(float64 format)Create a redefined calcDice with a float64 array from the beginning so that it can be overwritten with.
  calcDice[5] = calcDice[5] + calcDice[6] #Calculate the sum of the values of the black dice and index=5(That is the sixth)Update to the value of the dice
  calcDice = np.delete(calcDice, 6) #No longer needed, index=Delete the value of the 6th (ie 7th) dice
  if printOption == True:
    print('List of calculated elements: ', calcDice)
  return calcDice

# [Function 2] makeCalcName()
#___Input: Array calcDice that stores the sum of 5 white dice and 2 black dice
#_____ Output: Numbers converted to strings(A type that can be updated to a string of any length)Array calcDNames
###################################################

def makeCalcName(calcDice, printOption):
  calcDNames = np.empty(calcDice.shape[0],dtype=object) #Object format np so that strings of different lengths can be stored.Define an array (the length is the same as the length of the calcDice array)
  for i in range(calcDNames.shape[0]):
    calcDNames[i] = str(int(calcDice[i])) #Each element of calcDice that has been converted into characters is stored in sequence.
  if printOption == True:
    print('List of characterized arrays: ', calcDNames)
  return calcDNames

### fanction trial START ###
jmcDice = jmc_diceroll(printOption=True)
calcDice = transForCalc(jmcDice, printOption=True)
calcDNames = makeCalcName(calcDice, printOption=True)
### fanction trial END ###
>Jamaican Dice Roll Results:[ 2  3  1  4  4 50  5]
>List of calculated elements:  [ 2.  3.  1.  4.  4. 55.]
>List of characterized arrays:  ['2' '3' '1' '4' '4' '55']

_________________________________________

■ I was able to create an array of only calculated elements, calcDice, and calcDNames, which is a string of them.

■ This time, you will need to select your favorite two numbers and practice one of the four arithmetic operations.

■ Therefore, we will define the following functions that perform four arithmetic operations on the selected two numbers by inputting arithOpeNo, which specifies which four arithmetic operations to perform.

[Function 3] arithOperator()
___Input 1: The first of the two numbers you want to calculate var1
_____ Input 2: The second of the two numbers you want to calculate var2
______ Input 3: The number to specify which of the four arithmetic operations to use arithOpeNo
___Input 4: Selection option of whether to allow fractions fracOption
_____ Output: Result of four arithmetic operations on two numbers
 
[Function 4] arithMarker()
___Input: Number to specify which of the four arithmetic operations to use arithOpeNo
___ Output: Mark used for calculation record of calculation of 2 numbers

task005_calcfunctions_3&4


# [Function 3] arithOperator()
#___Input 1: The first of the two numbers you want to calculate var1
#_____ Input 2: The second of the two numbers you want to calculate var2
#______ Input 3: The number to specify which of the four arithmetic operations to use arithOpeNo
#___Input 4: Selection option of whether to allow fractions fracOption
#_____ Output: Result of four arithmetic operations on two numbers
###################################################

def arithOperator(var1, var2, arithOpeNo, fracOption):
  if arithOpeNo == 0: #Addition when 0 is specified for arithOpeNo
    return add(var1, var2)
  elif arithOpeNo == 1: #Subtract when 1 is specified for arithOpeNo
    return substract(var1, var2)
  elif arithOpeNo == 2: #Multiply by specifying 2 for arithOpeNo
    return multiply(var1, var2)
  elif arithOpeNo == 3: #Divide by specifying 3 for arithOpeNo
    return divide(var1, var2, fracOption)
  else: #arithOpeNo is 0,1,2,If it is not 3, np so that the operation result will not be output after that..Output nan
    return np.nan

# [Function 4] arithMarker()
#___Input: Number to specify which of the four arithmetic operations to use arithOpeNo
#___ Output: Mark used for calculation record of calculation of 2 numbers
###################################################

def arithMarker(arithOpeNo):
  if arithOpeNo == 0: #Addition when 0 is specified for arithOpeNo
    return ' + '
  elif arithOpeNo == 1: #Subtract when 1 is specified for arithOpeNo
    return ' - '
  elif arithOpeNo == 2: #Multiply by specifying 2 for arithOpeNo
    return ' * '
  elif arithOpeNo == 3: #Divide by specifying 3 for arithOpeNo
    return ' / '
  else: #arithOpeNo is 0,1,2,If it is not 3, then it cannot be calculated|???|To display
    return ' |???| '

### fanction trial START ###
var1 = 19
var2 = 4
print('___Results of four arithmetic operations___ for ', var1, ' & ', var2)
for i in range(6):
  print(arithOperator(var1, var2, arithOpeNo=i, fracOption=True))

print('___Mark of four arithmetic operations___')
for i in range(6):
  print(arithMarker(arithOpeNo=i))
### fanction trial END ###
___Results of four arithmetic operations___ for  19  &  4
23
15
76
4.75
nan
nan
___Mark of four arithmetic operations___
 + 
 - 
 * 
 / 
 |???| 
 |???| 

_________________________________________

■ You can now use the specified four arithmetic keys as input to calculate your favorite two numbers and output what kind of arithmetic operations you have performed.

■ Next, specify the desired 2 numbers of the calcDice array with index, and define the function renewDice () that outputs the array that stores the result of calculating the 2 numbers according to the key arithOpeNo of the favorite four arithmetic operations. I will.

■ At the same time as the above, a function that specifies a favorite 2 numbers in the calcDNames array with index and outputs an array that stores a record of how the 2 numbers are calculated according to the key arithOpeNo of the favorite four arithmetic operations. We will define renewDNames ().

** _____ [Reference article] _____ ** [^ ref004] Task005 | How to write conditional branch by if statement in Python ➡︎ Among the ways to write if statements, I especially referred to the line breaks in conditional expressions.

task005_calcfunctions_5&6


# [Function 5] renewDice()
#___Input 1: Array that saves the dice result as a calculation element calcDices
#_____ Input 2: The first of the two indexes you want to calculate ind1
#_____ Input 3: The second of the two indexes you want to calculate ind2
#______ Input 4: The number to specify which of the four arithmetic operations to use arithOpeNo
#___Input 5: Option to select whether to allow fractions fracOption
#_____ Output: An array that newly stores the result of calculating two numbers
###################################################

def renewDice(calcDice, ind1, ind2, arithOpeNo, fracOption):
  newDice = np.copy(calcDice) #Get a copy of calcDice without overwriting the array
  if newDice.shape[0] -1 > ind1 \
    and newDice.shape[0] -1 > ind2 \
    and ind1 != ind2:
    #ind1,Range where ind2 indicates white dice(0 or more new Dice length-2 or less)When ind1 and ind2 are different
      newDice[ind1] = arithOperator(newDice[ind1], newDice[ind2], arithOpeNo, fracOption)
      #Performs the four arithmetic operations specified by arithOpeNo to the value of the specified index.
  else:
    for i in range(newDice.shape[0] - 1):
      newDice[i] = np.nan #Np all computational elements.Replace with nan
  newDice = np.delete(newDice, ind2)
  #In common, delete the value of ind2 and change the length of the dice calculation element array.-1
  return newDice

# [Function 6] renewDNames()
#___Input 1: Array of dice roll results in characters calcDNames
#_____ Input 2: The first of the two indexes you want to calculate ind1
#_____ Input 3: The second of the two indexes you want to calculate ind2
#______ Input 4: The number to specify which of the four arithmetic operations to use arithOpeNo
#___Input 5: Option to select whether to allow fractions fracOption
#_____ Output: An array that keeps the calculation record of calculating 2 numbers
###################################################

def renewDNames(calcDNames, ind1, ind2, arithOpeNo, fracOption):
  newDNames = np.copy(calcDNames) #Get a copy of calcDNames without overwriting the array
  if newDNames.shape[0] - 1 > ind1 \
    and newDNames.shape[0] -1 > ind2 \
    and ind1 != ind2:
    #ind1,Range where ind2 indicates white dice(0 or more new Dice length-2 or less)When ind1 and ind2 are different
      newDNames[ind1] = '(' + newDNames[ind1] + arithMarker(arithOpeNo) + newDNames[ind2] + ')'
      #Keep a record of the four arithmetic operations specified by arithOpeNo
  else:
    for i in range(newDNames.shape[0] - 1):
      newDNames[i] = '|???|' #A record of all computational elements|???|Replace with
  newDNames = np.delete(newDNames, ind2)
  #In common, delete the value of ind2 and change the length of the dice calculation element array.-1
  return newDNames

### fanction trial START ###
calcDice = transForCalc(jmcDice, printOption=True)
calcDNames = makeCalcName(calcDice, printOption=True)
print('\n___index=1,List of calculated elements updated by multiplication by 4th___\n', renewDice(calcDice, 1, 4, arithOpeNo=2, fracOption=False))
print('___index=1,List of characterized arrays updated by multiplication by 4th___\n', renewDNames(calcDNames, ind1=1, ind2=4, arithOpeNo=2, fracOption=False))
### fanction trial END ###
>List of calculated elements:  [ 2.  3.  1.  4.  4. 55.]
>List of characterized arrays:  ['2' '3' '1' '4' '4' '55']
>
> ___index=1,List of calculated elements updated by multiplication by 4th___
>  [ 2. 12.  1.  4. 55.]
> ___index=1,List of characterized arrays updated by multiplication by 4th___
>  ['2' '(3 * 4)' '1' '4' '55']

_________________________________________

■ You can now specify two indexes of the array you want to calculate, perform your favorite four arithmetic operations, and update the array of calculation elements and the characterized array of calculation records.

■ Therefore, we will finally define a function that searches all Jamaican solutions.

■ The following concepts are used in the solution search.

[step 1]5 white dice, that is, 5 integer values(1 or more and 6 or less)Select any two of them and arrange them (5P2)=20 combinations) and four arithmetic operations (4 types of addition, subtraction, multiplication and division) are performed.
[Step 2]the above[1]One number generated in and the remaining three integer values(1 or more and 6 or less)Select any two of them and arrange them(4P2=12 combinations), Perform four arithmetic operations (4 types of addition, subtraction, multiplication and division).
[Step 3]the above[1][2]Continue until one number is output in the same flow as.

■ Therefore, define the following function that updates the array "recursively" to perform a solution search.

[Function to search for a solution] jmcReduce()
___Input 1: Array calcDice that saves the result of the dice roll as a calculation element
___Input 2: An array of dice roll results in characters calcDNames
___Input 3: Selection option of whether to allow fractions fracOption
_____ Output: jmcReduce with calcDice and calcDNames as arguments, updated with the results of the two arithmetic operations of the specified index.()
___Repeat: Search only the index corresponding to the white dice(Two indexes i, j (i ≠ j)Scan with)
_____ Iteration: The keys of the four arithmetic operations using two numbers represent addition, subtraction, multiplication, and division 0, 1, 2,Search all with 3(Scan with k)

** _____ [Reference article] _____ ** [^ ref005] Task005 | Simplest example for understanding recursive functions ➡︎ When defining the search function recursively, I referred to it to re-understand the basic properties of the recursive function.

task005_jmcReduce


# [Function to search for a solution] jmcReduce()
#___Input 1: Array calcDice that saves the result of the dice roll as a calculation element
#___Input 2: An array of dice roll results in characters calcDNames
#___Input 3: Selection option of whether to allow fractions fracOption
#_____ Output: jmcReduce with calcDice and calcDNames as arguments, updated with the results of the two arithmetic operations of the specified index.()
#___Repeat: Search only the index corresponding to the white dice(Two indexes i, j (i ≠ j)Scan with)
#_____ Iteration: The keys of the four arithmetic operations using two numbers represent addition, subtraction, multiplication, and division 0, 1, 2,Search all with 3(Scan with k)
###################################################

def jmcReduce(calcDice, calcDNames, fracOption):
  newDice = np.copy(calcDice) #Duplicate to not modify input 1
  newDNames = np.copy(calcDNames) #Duplicate to not modify input 2
  for i in range(newDice.shape[0] - 1): #The first index is the range of white dice(Array length of newDice greater than or equal to 0-2 or less)Loop processing with
    for j in range(newDice.shape[0] - 1): #The second index is the range of white dice(Array length of newDice greater than or equal to 0-2 or less)Loop processing with
      if i != j: #If ind1=i, ind2=If j is not equal
        for k in range(4): #Key number of four arithmetic operations arithOpeNo= 0,1,2,Iterative variable k that scans 3
          if arithOperator(newDice[i],newDice[j],k, fracOption) != np.nan: #The operation result is np.I want to update the array only if it doesn't become nan
            if newDice.shape[0] == 3 \
              and renewDice(newDice,i,j,k,fracOption)[0] == renewDice(newDice,i,j,k,fracOption)[1]:
              #If the length of the array is 3, that is, it is composed of the remaining 2 calculation elements of the white dice and 1 sum of the black dice.
              #When the result of the four arithmetic operations of the calculation element matches the sum of the black dice
                print(renewDNames(newDNames,i,j,k,fracOption)[0], ' = ', renewDNames(newDNames,i,j,k,fracOption)[1],' ______!!!!! This pattern is OK !!!!!______')
                #Calculation record of calculation elements(Character array value)Is output to show that it matches the value of the black dice!(This is the list of solutions)
                break
            elif newDice.shape[0] == 3 \
              and renewDice(newDice,i,j,k,fracOption)[0] != renewDice(newDice,i,j,k,fracOption)[1]:
              #If the length of the array is 3, that is, it is composed of the remaining 2 calculation elements of the white dice and 1 sum of the black dice.
              #When the result of the four arithmetic operations of the calculation elements does not match the sum of the black dice
                #print(renewDNames(calcDNames,i,j,k,fracOption),' ___This pattern is NG xxx___')
                #Calculation record of calculation elements(Character array value)Outputs and indicates that it did not match the value of the black dice!(It's hard to see, so I usually comment out)
                break
            else: #If the array length is still greater than 3
              jmcReduce(renewDice(newDice,i,j,k,fracOption),renewDNames(newDNames,i,j,k,fracOption),fracOption)
              #Recursively update to a shorter array again and start over
          else: #The operation result is np.Update k without doing anything if it becomes nan
            continue
      else: #If ind1=i, ind2=Update j without doing anything if j is equal
        continue

### fanction trial START ###
print('\n______START______')
jmcReduce(calcDice, calcDNames, fracOption=False)
### fanction trial END ###
______START______
(((2 + (4 * 4)) * 3) + 1)  =  55  ______!!!!! This pattern is OK !!!!!______
(1 + ((2 + (4 * 4)) * 3))  =  55  ______!!!!! This pattern is OK !!!!!______
((3 * (2 + (4 * 4))) + 1)  =  55  ______!!!!! This pattern is OK !!!!!______
(1 + (3 * (2 + (4 * 4))))  =  55  ______!!!!! This pattern is OK !!!!!______
((3 * ((4 * 4) + 2)) + 1)  =  55  ______!!!!! This pattern is OK !!!!!______
(1 + (3 * ((4 * 4) + 2)))  =  55  ______!!!!! This pattern is OK !!!!!______
(1 + (((4 * 4) + 2) * 3))  =  55  ______!!!!! This pattern is OK !!!!!______
((((4 * 4) + 2) * 3) + 1)  =  55  ______!!!!! This pattern is OK !!!!!______
(((2 + (4 * 4)) * 3) + 1)  =  55  ______!!!!! This pattern is OK !!!!!______
(1 + ((2 + (4 * 4)) * 3))  =  55  ______!!!!! This pattern is OK !!!!!______
((3 * (2 + (4 * 4))) + 1)  =  55  ______!!!!! This pattern is OK !!!!!______
(1 + (3 * (2 + (4 * 4))))  =  55  ______!!!!! This pattern is OK !!!!!______
((3 * ((4 * 4) + 2)) + 1)  =  55  ______!!!!! This pattern is OK !!!!!______
(1 + (3 * ((4 * 4) + 2)))  =  55  ______!!!!! This pattern is OK !!!!!______
(1 + (((4 * 4) + 2) * 3))  =  55  ______!!!!! This pattern is OK !!!!!______
((((4 * 4) + 2) * 3) + 1)  =  55  ______!!!!! This pattern is OK !!!!!______
_______END_______

Consideration in the first race

result

■ This time, the solution was searched while gradually updating the array shortly by the jmcReduce () function ** that was recursively defined.

■ As a result, ** all the output solutions are correct **, and I think we have presented at least some Jamaican solutions that should hold.

problem

_______ [1] Multiple solutions overlap as the meaning of the formula _______

■ Especially in the case of solutions using addition and multiplication, multiple solution groups with different order of arithmetic operations were displayed even though they have the same mathematical meaning. Example) (((2 + (4 * 4)) * 3) + 1) = 55 The solution ((((4 * 4) + 2) * 3) + 1) = 55 has the same mathematical meaning.

■ To improve this, you have to change the treatment of addition (sum, add () function) and multiplication (product, multiply ()).

_______ [2] I have presented multiple identical solutions _______

■ In this calculation example, you can see that the two groups have presented exactly the same solution. Example) ((((4 * 4) + 2) * 3) + 1) = 55 The solution ((((4 * 4) + 2) * 3) + 1) = 55 has exactly the same appearance and mathematical meaning.

■ Probably, if the same number exists in the ** white dice, the calculation result may be duplicated many times while recurring ** </ strong> I think font>.

_______ [3] Are you able to search for all the solutions? _______

■ Jamaican solutions generally exist in quite a few types in a set of dice.

■ Some solutions using subtraction and division may be included in it.

■ While trying this jmc Reduce with several types of dice roll results, I was skeptical that "I think I couldn't find a solution using subtraction or division." (Especially division)

■ Therefore, I tried jmcReduce using the Jamaican dice roll result and solution example in the following reference article. Example) White dice 2 4 2 4 5 Black dice 20 3 On the other hand, the following four solutions can be considered, for example. (It is a pattern that uses addition, subtraction, multiplication and division evenly.)     (2 × 6) + 2 + 4 + 5 = 23     ((6 + 4) × 2) + 5 - 2 = 23     4 × (6 + (2 ÷ 2)) - 5 = 23     5 × (6 ÷ 2) + (4 × 2) = 23

** _____ [Reference article] _____ ** [^ ref006] Check | Preceding implementation site for solution search in Jamaica ➡︎ A site that does only one solution search for Jamaica. (With option to use fractions)

List of calculated elements:  [ 2.  6.  2.  4.  5. 23.]
List of characterized arrays:  ['2' '6' '2' '4' '5' '23']

______START______
((((2 * 6) + 2) + 4) + 5)  =  23  ______!!!!! This pattern is OK !!!!!______
(5 + (((2 * 6) + 2) + 4))  =  23  ______!!!!! This pattern is OK !!!!!______
((((2 * 6) + 2) + 5) + 4)  =  23  ______!!!!! This pattern is OK !!!!!______
(4 + (((2 * 6) + 2) + 5))  =  23  ______!!!!! This pattern is OK !!!!!______
((4 + ((2 * 6) + 2)) + 5)  =  23  ______!!!!! This pattern is OK !!!!!______

===== Omitted Omitted Omitted =====

(5 + (2 + ((2 * 6) + 4)))  =  23  ______!!!!! This pattern is OK !!!!!______
(((2 * 6) + 4) + (2 + 5))  =  23  ______!!!!! This pattern is OK !!!!!______
((2 + 5) + ((2 * 6) + 4))  =  23  ______!!!!! This pattern is OK !!!!!______
(2 + (5 + ((2 * 6) + 4)))  =  23  ______!!!!! This pattern is OK !!!!!______
((5 + ((2 * 6) + 4)) + 2)  =  23  ______!!!!! This pattern is OK !!!!!______
(((2 * 6) + 4) + (5 + 2))  =  23  ______!!!!! This pattern is OK !!!!!______
((5 + 2) + ((2 * 6) + 4))  =  23  ______!!!!! This pattern is OK !!!!!______
((((2 * 6) + 5) + 2) + 4)  =  23  ______!!!!! This pattern is OK !!!!!______
(4 + (((2 * 6) + 5) + 2))  =  23  ______!!!!! This pattern is OK !!!!!______
((((2 * 6) + 5) + 4) + 2)  =  23  ______!!!!! This pattern is OK !!!!!______
(2 + (((2 * 6) + 5) + 4))  =  23  ______!!!!! This pattern is OK !!!!!______
((2 + ((2 * 6) + 5)) + 4)  =  23  ______!!!!! This pattern is OK !!!!!______
(4 + (2 + ((2 * 6) + 5)))  =  23  ______!!!!! This pattern is OK !!!!!______

===== Omitted Omitted Omitted =====

(((5 + 4) + (2 * 6)) + 2)  =  23  ______!!!!! This pattern is OK !!!!!______
((2 * 6) + ((5 + 4) + 2))  =  23  ______!!!!! This pattern is OK !!!!!______
(((5 + 4) + 2) + (2 * 6))  =  23  ______!!!!! This pattern is OK !!!!!______
((((2 + 2) * 6) - 5) + 4)  =  23  ______!!!!! This pattern is OK !!!!!______
(4 + (((2 + 2) * 6) - 5))  =  23  ______!!!!! This pattern is OK !!!!!______
(((6 * (2 + 2)) - 5) + 4)  =  23  ______!!!!! This pattern is OK !!!!!______
(4 + ((6 * (2 + 2)) - 5))  =  23  ______!!!!! This pattern is OK !!!!!______
((2 + 2) + ((6 * 4) - 5))  =  23  ______!!!!! This pattern is OK !!!!!______
(((6 * 4) - 5) + (2 + 2))  =  23  ______!!!!! This pattern is OK !!!!!______
((2 + 2) + ((4 * 6) - 5))  =  23  ______!!!!! This pattern is OK !!!!!______
(((4 * 6) - 5) + (2 + 2))  =  23  ______!!!!! This pattern is OK !!!!!______
((((2 * 2) * 6) - 5) + 4)  =  23  ______!!!!! This pattern is OK !!!!!______
(4 + (((2 * 2) * 6) - 5))  =  23  ______!!!!! This pattern is OK !!!!!______
(((6 * (2 * 2)) - 5) + 4)  =  23  ______!!!!! This pattern is OK !!!!!______
(4 + ((6 * (2 * 2)) - 5))  =  23  ______!!!!! This pattern is OK !!!!!______
((2 * 2) + ((6 * 4) - 5))  =  23  ______!!!!! This pattern is OK !!!!!______
(((6 * 4) - 5) + (2 * 2))  =  23  ______!!!!! This pattern is OK !!!!!______
((2 * 2) + ((4 * 6) - 5))  =  23  ______!!!!! This pattern is OK !!!!!______
(((4 * 6) - 5) + (2 * 2))  =  23  ______!!!!! This pattern is OK !!!!!______
((((2 + 4) * 2) + 6) + 5)  =  23  ______!!!!! This pattern is OK !!!!!______

===== Omitted Omitted Omitted =====

((6 * 2) + (4 + (2 + 5)))  =  23  ______!!!!! This pattern is OK !!!!!______
((4 + (2 + 5)) + (6 * 2))  =  23  ______!!!!! This pattern is OK !!!!!______
((2 * 6) + (4 + (2 + 5)))  =  23  ______!!!!! This pattern is OK !!!!!______
((4 + (2 + 5)) + (2 * 6))  =  23  ______!!!!! This pattern is OK !!!!!______
((((6 - 2) * 4) + 2) + 5)  =  23  ______!!!!! This pattern is OK !!!!!______
(5 + (((6 - 2) * 4) + 2))  =  23  ______!!!!! This pattern is OK !!!!!______
((((6 - 2) * 4) + 5) + 2)  =  23  ______!!!!! This pattern is OK !!!!!______
(2 + (((6 - 2) * 4) + 5))  =  23  ______!!!!! This pattern is OK !!!!!______
((2 + ((6 - 2) * 4)) + 5)  =  23  ______!!!!! This pattern is OK !!!!!______
(5 + (2 + ((6 - 2) * 4)))  =  23  ______!!!!! This pattern is OK !!!!!______
(((6 - 2) * 4) + (2 + 5))  =  23  ______!!!!! This pattern is OK !!!!!______
((2 + 5) + ((6 - 2) * 4))  =  23  ______!!!!! This pattern is OK !!!!!______
(2 + (5 + ((6 - 2) * 4)))  =  23  ______!!!!! This pattern is OK !!!!!______
((5 + ((6 - 2) * 4)) + 2)  =  23  ______!!!!! This pattern is OK !!!!!______
(((6 - 2) * 4) + (5 + 2))  =  23  ______!!!!! This pattern is OK !!!!!______
((5 + 2) + ((6 - 2) * 4))  =  23  ______!!!!! This pattern is OK !!!!!______
(((6 - 2) * 4) + (2 + 5))  =  23  ______!!!!! This pattern is OK !!!!!______
((2 + 5) + ((6 - 2) * 4))  =  23  ______!!!!! This pattern is OK !!!!!______
((2 + 5) + (4 * (6 - 2)))  =  23  ______!!!!! This pattern is OK !!!!!______
((4 * (6 - 2)) + (2 + 5))  =  23  ______!!!!! This pattern is OK !!!!!______
((2 + (4 * (6 - 2))) + 5)  =  23  ______!!!!! This pattern is OK !!!!!______

===== Omitted Omitted Omitted =====

(((5 + 4) + 2) + (2 * 6))  =  23  ______!!!!! This pattern is OK !!!!!______
((6 * 2) + ((5 + 4) + 2))  =  23  ______!!!!! This pattern is OK !!!!!______
(((5 + 4) + 2) + (6 * 2))  =  23  ______!!!!! This pattern is OK !!!!!______

_______END_______

inspection result

■ Due to problems [1] and [2], the output result is very long, so the middle part was omitted.

■ As a result, the solution using ** subtraction was output, but the solution using ** </ font> ** division There was no output. ** </ font>

■ Possible causes are a malfunction of fracOption or a mistake in the definition of arisOperator, but when the result of the four arithmetic operations was output in Task.004, 6/6 was also output as 1, so The exact cause has not been investigated.

Challenge until next time

_______ [1] In the case of addition / multiplication, pre-merge those with the same result _______

■ Especially in the case of solutions using addition and multiplication, multiple solution groups with different order of arithmetic operations were displayed even though they have the same mathematical meaning.

■ When defining the add () function, multiply () function, arishOprerator () function, or when looping the jmcReduce () function, is it possible to devise ways to eliminate in advance the set of i and j with the same addition and multiplication results? ??

■ It may be difficult to solve this by recursive function definition.

*** After outputting the number of possible combinations of two and four arithmetic operations in advance and the result, the set of arrays that must be searched is statically set. I thought that it would be effective to prepare a stored array and solve it *** </ font>.

_______ [2] Improving division errors ________

■ I don't know the exact cause, but I couldn't output the solution using ** division. ** </ font>.

■ I would like to start by investigating the exact cause while reviewing the definitions related to the four arithmetic operations and the algorithm around the solution.

Next time preview

This time, I tried to solve Jamaica by implementing the function definition of four arithmetic operations and the recursive scanning function to the calculation element array.

Next time, after improving the problem that occurred this time, *** Define a function to search for a solution by statically complementing, generating, and processing solution candidates without performing a recursive search </ font> *** I also want to try to do it. ..

The title is ** vol.04 "Aiming for a solution in Jamaica ~ Round 2 ~" **.

REF. Basic information about Jamaica

Q1. What is math teaching tool "Jamaica" </ font>?

A1. "Jamaica" is sold as an arithmetic teaching tool with the theme of "number play" using dice </ font>.

■ *** This is a general product that is also sold on mail-order sites such as Amazon and Yahoo! Shopping. *** *** ➡︎ Click here for the Amazon sales page (https://www.amazon.co.jp/dp/4902756161/ref=cm_sw_r_tw_dp_U_x_XrEPEbCJ5VRST). ➡︎ Please refer to the figure below for the image (quoted from the above page) qiita1_jamaica_red.jpg

■ *** There are two types of dice, white and black, and 5 and 2 dice are included, respectively. *** *** ➡︎ A white dice x 5 and a black dice x 1 are attached to the ring part, and a black dice x 1 is attached to the center part.

color/ Color quantity/ amount Numbers listed/ Numbers
White/ white 5 pieces/ 5 dice 1, 2, 3, 4, 5, 6
black/ black 1 piece/ 1 dice 1, 2, 3, 4, 5, 6
black/ black 1 piece/ 1 dice 10, 20, 30, 40, 50, 60

Q2. What is Jamaica's how to play </ font>?

A2. Perform four arithmetic operations </ font> using each of the white dice x 5 numbers once to make the sum of the black dice x 2 numbers.

For example, if the white dice roll is (1,2,3,5,5) and the black dice roll is (10,6), You can add all the rolls of the white dice to create the equation 1 + 2 + 3 + 5 + 5 = 10 + 6.

qiita002_jamaica_sample_sizedown.png

■ *** White dice x 5 numbers can only be used once each. *** *** ➡︎ For example, if the white dice roll is (2,4,5,3,3), you can use it for calculation. 2 is 1 time, 3 is 2 times, 4 is 1 time, 5 is 1 time. Do not use any more. (You can't use 2 twice or 3 three times.) ➡︎ Of course, ** "Do not use the specified number of times" is also a law **. (In the above example, you can't use 3 only once.)

■ *** Four arithmetic operations are addition, subtraction, multiplication and division </ font>. *** *** ➡︎ ** "Addition" ** </ font> is "addition", that is, ** "addition" **. For example, calculate the sum (result of addition) such as 2 + 3 = 5. </ font> ➡︎ ** "Decrease" ** </ font> is "subtraction", that is, ** "subtraction" **. For example, calculate the difference (subtraction result) such as 5-2 = 3. </ font> ➡︎ ** "Multiplication" ** </ font> is "multiplication", that is, ** "multiplication" **. For example, calculate the product (the result of multiplication) such as 3 × 4 = 12. </ font> ➡︎ ** "division" ** </ font> is "division", that is, ** "division" **. For example, calculate the quotient (division result *) such as 4/2 = 2. </ font>

  • There are rules that allow only divisible operations, that is, rules that do not allow irreducible fractions, and rules that allow irreducible fractions as well. [1] ** In the case of a rule that does not allow irreducible fractions ** When the white dice roll (2,4,6,3,5), you can calculate 6/3 = 2, but The result 6/4 = 3/2 is not available. [2] ** In the case of a rule that allows irreducible fractions ** When the white dice roll (2,4,6,3,5), not only can the calculation 6/3 = 2 be performed, but also The result 6/4 = 3/2 is also available.
  • ** Some dice rolls cannot be solved without using irreducible fractions. ** </ font> Example) When the white dice roll (2,2,4,4,6) and the black dice roll (30,3) The equation {(2- 2/4) + 4} x 6 = 30 + 3 holds. </ font>

■ *** The four arithmetic operations can be performed in any order. *** *** ➡︎ You can add two numbers first and then multiply by another number. In other words, you can use parentheses any number of times, anytime, anywhere. Example) When the white dice roll (3,6,4,4,1) and the black dice roll (20,6) The equation {(3 + 6) + (4 x 4)} + 1 = 20 + 6 holds. </ font>

Reference article summary

[^ ref001]: Recollection | Let's reproduce the math teaching tool "Jamaica" ❗️'s previous article (vol.01) [^ ref002]: Recollection | Let's reproduce the math teaching tool "Jamaica" ❗️'s previous article (vol.02) [^ ref003]: Task004 | [What is NAN? ] Meaning and usage [^ ref004]: Task005 | How to write conditional branch by if statement in Python [^ ref005]: Task005 | Simplest example for understanding recursive functions [^ ref006]: Check | Preceding implementation site for solution search in Jamaica