How I wrote a computer program to solve a competitive math problem.
故事 (The Story)
For the past 3 years, I’ve always been using math in my computer science projects. Whether it was using quadratic equations for an iOS Game or implementing matrices for a video game simulation, math always played an essential role. However, I’ve never been able to use my coding strength in the numerous competitive math contests I’ve taken, from local challenges to national competitions like the AMC 12. This all changed when I found an online, international contest called Purple Comet.
When going through the rules for the contest, I came across the following:
“On the other hand, participants are permitted to use calculators or computers, for example, to write a computer program in order solve or better understand a problem.”
For the first time, I realized that I could write a computer program specifically for a competitive math problem. Now you may be wondering, why would I want to do this? Shouldn’t I be using math on a math contest? Well, take a look at Question 29 from the Spring 2016 HS Meet:
第一次，我意识到我可以编写一个专门针对竞争性数学问题的计算机程序。 现在您可能想知道，我为什么要这样做？ 我不应该在数学竞赛中使用数学吗？ 好吧，请看一下2016年SpringHS Meet上的问题29：
While solving this problem by hand would take me a lot of time, writing a program to find the answer would take only a few minutes. Here is one coding approach in Python 3.6:
尽管手工解决这个问题需要我花费很多时间 ，但是编写一个寻找答案的程序只需要几分钟 。 这是Python 3.6中的一种编码方法：
def notValid(current, choices):
choices = choices.copy()
for c in current:
if c in choices:
return len(choices) > 0def generate(max, current, choices):
if len(current) == max:
total = 
for c in choices:
new = current + c
if len(new) >= 5 and notValid(new[-5:], choices):
total += generate(max, current + c, choices)
t = generate(10, "", ["R", "B", "Y", "W"])
print("The answer is: ", len(t))
First, we define a helper method called notValid. This method will tell us if current, a subsequence of a generated pattern, contains at least one tile of every color every block of 5 adjacent tiles.
Next, we create our main, recursive method called generate. Notice that this problem can be solved by recursion, either with code or even by hand. However, from my experience, coding recursion is a lot quicker and easier than writing recursion.
接下来，我们创建主要的递归方法generate 。 注意，这个问题可以通过递归来解决，可以使用代码，也可以手动解决。 但是，根据我的经验，与编写递归相比，编码递归要快得多且容易得多。
Inside generate, we first check if our current pattern has reached its max length of 10. If it has, return the pattern. Otherwise, iterate through our list of letter choices. After making sure that the end of the new pattern is valid using notValid, call generate again and return its value.
在generate中 ，我们首先检查当前模式是否已达到其最大长度10。如果已达到，则返回该模式。 否则，请遍历我们的字母选择列表。 使用notValid确保新模式的结尾有效后 ，再次调用generate并返回其值。
After calling generate(10, “”, [“R”, “B”, “Y”, “W”]), we will have a list of all of the valid, 10 letters patterns that can be created from the letters R, B, Y, and W. All you have to do is get the length of the list and you should have the answer of 7,296:
调用generate(10，“”，[“ R”，“ B”，“ Y”，“ W”]))之后，我们将获得可以从字母R创建的所有有效的10个字母模式的列表。 ，B，Y和W。您所要做的就是获取列表的长度，答案应该为7,296 ：
Now although this was a practice problem, I still wrote a couple computer programs for the actual contest I took in Spring 2020. I even managed to score some points😁!
我学到的是 (What I Learned)
By participating in Purple Comet, I learned that code and logic give me greater insight on a math problem. I’m able to organize the problem into inputs (parameters), outputs (returned values), and steps (methods), apply logic and a math principle like recursion, counting, and number theory, and quickly solve for an answer by writing code. From now on, for every competitive math problem that I face (unless it’s geometry🙃), I’m going to ask myself this question:
通过参加Purple Comet，我了解到代码和逻辑使我对数学问题有了更深入的了解。 我能够将问题组织为输入(参数)，输出(返回值)和步骤(方法)，应用逻辑和数学原理(例如递归，计数和数论)，并通过编写代码快速解决问题。 从现在开始，对于我面临的每一个竞争性数学问题(除非是几何🙃 )，我都会问自己一个问题：
Can I code a solution?