[CodeCombat Computational Thinking] Mathematical methods + programming methods to solve math problems - AI4kids

Let’s first look at a calculation problem of International Mathematical Olympiad difficulty for elementary school students. There are two types of three-digit numbers. Type A satisfies that the sum of the numbers on each digit is 19, and Type B satisfies that the sum of the numbers on each digit is 8. Are there more numbers in Type A or Type B? How many more? When it comes to general mathematical solutions, most students first think of the enumeration method, which involves analyzing the list of all numbers that meet condition A (100A + 10B + C = 19) and condition B (100A + 10B + C = 8) (as shown below). This is a relatively time-consuming and laborious method, and may result in an incorrect answer due to carelessness, such as missing a few numbers.

20230707_content_045_CodeCombat-Computational-Thinking2_600x600

Improved mathematical solution: If we observe carefully, the sum of the digits of A's three-digit number is 19, and the sum of the digits of B's ​​three-digit number is 8. The sum of the two is 19+8 = 27, which is exactly 9 + 9 + 9. This means that every three-digit number whose sum is 8 (ABC) has a corresponding number whose sum is 19 (9-A 9-B 9-C).

In addition, we compare the numbers whose sum of three digits is 19 with the numbers whose sum of three digits is 8 and find that the three-digit numbers whose sum of three digits is 19 have one more number whose first digit is 9, which is exactly 9 (when A=0, the three-digit number degenerates into a two-digit number and does not meet the condition). Therefore, we can conclude that there are more Class A numbers, and Class A numbers are 9 more than Class B numbers.

After understanding the problem-solving method of general program , we use Python program to solve this problem. Write the logic of the question conditions clearly. The program uses the main knowledge points of CodeCombat Computer Science 4, such as For loops, traversal, lists, arithmetic operations, etc. After the 16-line code (as shown below) is written, running it immediately shows that there are 45 numbers that meet the A condition and 36 numbers that meet the B condition.

20230707_content_045_CodeCombat-Computational-Thinking3_600x600

Improved algorithm for solving problems

We can further optimize the logic of the question and simplify the 16-line code into a 1-line code. Enter the program and immediately get the correct answer, which is 9.

print (sum ([1 if s==19 else (-1 if s==8 else0) for s in [x//100 + x//10%10 + x%10 for x in range (100,1000)]]))

The program's solution to this math problem is more general because it can handle any three-digit sum problem. The ingenious mathematical solution discovered the special conditions of this problem and provided a simpler solution to the problem. Mathematical methods are the foundation. The strength of computers is their powerful computing ability. Mathematical thinking combined with the powerful computing ability of computers can often achieve twice the result with half the effort. This is why we encourage students to learn programming, which adds an extra dimension to thinking about and solving problems, and pursues the optimization and improvement of solutions.

Want to learn more? Click to try the free programming course!

More Related Articles

Back to blog