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.
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.
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
- The Legend of Ozaria, a global blockbuster about teen programming
- The challenges and opportunities that COVID-19 brings to global youth programming education
- Nick Winter, founder of CodeCombat: How to help primary and secondary school programming education develop and deepen
- CodeCombat was selected for the 33rd International Information Olympiad Competition (IOI)
- Recommended children's programming courses! Elementary school students can easily learn online!
- How Singapore uses CodeCombat to launch a national youth Python programming competition
- Teacher's Guide to CodeCombat Multiplayer Arena Levels
- If you want to keep up with technological trends, should you cultivate your children's programming skills from an early age?
- Ozaria 2020 Hour of Coding ranked as the world's most popular event
- What programming languages are suitable for elementary, junior high, and high school students to learn? Recommended Children's Programming Courses
- CodeCombat is used by schools all over the world, especially in the Asia-Pacific region
- A quick guide to self-learning programming at home for elementary and middle school students: resources for children to learn programming, and recommended introductory camp courses
- Singapore uses CodeCombat to build a youth programming learning community and competition activities