竞赛简介：全国大学生数学竞赛（The Chinese Mathematics Competitions, CMC）是一项全国性高水平学科竞赛，从2009年起每年举办一次，主要参赛对象为大学本科二年级及二年级以上的在校大学生。CMC分为数学类和非数学类。其中数学类竞赛内容为大学本科数学专业基础课的教学内容（数学分析占50%，高等代数占35%，解析几何占15%）；非数学类竞赛内容为理工科专业高等数学课程的教学内容，从第五届比赛开始，非数学类决赛增加15%－20%的线性代数的内容。CMC分为预赛和决赛进行，其中分区预赛由各省数学会负责组织，使用全国统一试题在同一时间内进行考试，决赛由全国大学生数学竞赛工作小组和承办单位负责组织实施。
高中数学竞赛和信息学竞赛How I wrote a computer program to solve a competitive math problem. 我如何编写计算机程序来解决竞争性数学问题。 故事 (The Story) For the past 3 years, I’ve always been using ...
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:
def notValid(current, choices): choices = choices.copy() for c in current: if c in choices: choices.remove(c) return len(choices) > 0def generate(max, current, choices): if len(current) == max: return [current] total =  for c in choices: new = current + c if len(new) >= 5 and notValid(new[-5:], choices): continue total += generate(max, current + c, choices) return total 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.
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.
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:
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: