← 返回资讯屏轩教育 · 行业资讯

Scratch Programming: Solving Mathematical Problems with Visual Coding | Scratch编程:用图形化编程解决数学问题

2026-08-31 01:43 · 本文部分内容由AI辅助创作

📚 Scratch Programming: Solving Mathematical Problems with Visual Coding | Scratch编程:用图形化编程解决数学问题

Scratch is a visual programming language developed by MIT that allows students to create interactive stories, games, and animations by snapping together colorful blocks. Beyond its playful interface, Scratch is a powerful tool for exploring mathematical concepts through hands-on, computational thinking. In this article, we will demonstrate how to use Scratch to solve a variety of mathematical problems, from basic arithmetic to geometry and number theory.

Scratch 是由麻省理工学院开发的一款可视化编程语言,学生通过拖拽彩色积木块来创建交互式故事、游戏和动画。除了有趣的界面之外,Scratch 更是通过动手计算思维探索数学概念的强大工具。在本文中,我们将展示如何利用 Scratch 解决从基础算术到几何、再到数论的多种数学问题。


1. Getting Started with Scratch | Scratch入门基础

Before diving into mathematical problem-solving, it is essential to understand the core components of the Scratch interface. The main areas include the Stage (where projects run), the Sprite List (characters or objects), the Block Palette (coding commands), and the Scripts Area (where blocks are assembled). These elements work together to create a visual representation of algorithms and mathematical logic.

在深入数学问题求解之前,理解 Scratch 界面的核心组成至关重要。主要区域包括舞台(项目运行的地方)、角色列表(角色或对象)、积木区(编程命令)以及脚本区(拼接积木的地方)。这些元素协同工作,构建出算法和数学逻辑的可视化表示。

For mathematical computing, the following block categories are particularly relevant: "Operators" (arithmetic, comparison, and logical operations), "Variables" (storing numbers and results), "Control" (loops and conditionals), and "Sensing" (user input and timers). Familiarity with these blocks equips students with the necessary tools for solving intriguing mathematical challenges.

对于数学计算而言,以下积木类别尤为重要:"运算"(算术、比较和逻辑运算)、"变量"(存储数字和结果)、"控制"(循环和条件判断)以及"侦测"(用户输入和计时器)。熟悉这些积木能为学生提供解决有趣数学挑战的必要工具。


2. The Problem-Solving Framework | 问题解决框架

To effectively solve mathematical problems in Scratch, we follow a structured four-step framework: (1) understand the problem and identify input/output, (2) design an algorithm using pseudocode or flowcharts, (3) implement the algorithm using Scratch blocks, and (4) test, debug, and refine the solution. This approach mirrors the computational thinking skills emphasized in modern STEM education.

为了在 Scratch 中高效解决数学问题,我们遵循结构化的四步框架:(1) 理解问题并确定输入/输出;(2) 使用伪代码或流程图设计算法;(3) 用 Scratch 积木实现算法;(4) 测试、调试并优化解决方案。这种方法反映了现代 STEM 教育中强调的计算思维技能。

Let us consider a concrete example: computing the sum of the first n positive integers. The mathematical formula is S = n × (n + 1) ÷ 2, but we can also compute it iteratively with a loop. In Scratch, we would create a variable "sum" initialized to 0, a variable "i" initialized to 1, and a loop that repeatedly adds i to sum and increments i until i exceeds n. This illustrates the connection between algebraic formulas and algorithmic thinking.

让我们考虑一个具体例子:计算前 n 个正整数的和。数学公式为 S = n × (n + 1) ÷ 2,但我们也可以用循环迭代计算。在 Scratch 中,我们创建变量"总和"初始化为 0,变量"i"初始化为 1,然后循环将 i 加到总和中,并递增 i 直到 i 超过 n。这展示了代数公式与算法思维之间的紧密联系。


3. Prime Number Detection | 素数判定算法

A classic number theory problem is determining whether a given number is prime. A natural number greater than 1 is prime if it has exactly two distinct positive divisors: 1 and itself. In Scratch, we can implement a trial-division algorithm: for each integer from 2 to the square root of n, check whether n is divisible by that integer. If no divisor is found, n is prime.

一个经典的数论问题是判断给定数字是否为素数。大于 1 的自然数如果恰好只有两个不同的正因数(1 和它本身),则它是素数。在 Scratch 中,我们可以实现试除法算法:对于从 2 到 n 的平方根的每个整数,检查 n 是否能被该整数整除。如果没有找到因数,则 n 是素数。

The Scratch implementation uses the modulo operator from the "Operators" category. The code might include a "repeat until" loop that checks divisibility and sets a "isPrime" flag to "false" if a factor is found. Since checking up to the square root reduces iterations significantly, this is an efficient algorithm for numbers up to several million, demonstrating the power of algorithmic optimization.

Scratch 实现使用了"运算"类别中的取余运算。代码可能包含一个"重复执行直到"循环,检查整除性,并在找到因数时将"是否为素数"标志设为"假"。由于只检查到平方根,迭代次数显著减少,这使得该算法对几百万以内的数字都很高效,展示了算法优化的力量。

if n mod i = 0 then set isPrime to false | 如果 n 除以 i 的余数 = 0,则将 isPrime 设为假


4. Greatest Common Divisor and Euclidean Algorithm | 最大公约数与欧几里得算法

The greatest common divisor (GCD) of two integers is the largest positive integer that divides both numbers without a remainder. The Euclidean algorithm provides an elegant method: repeatedly replace the larger number by the difference or the remainder of the two numbers until they become equal. Alternatively, we can use the modulo-based version: while b ≠ 0, set (a, b) = (b, a mod b); the final value of a is the GCD.

两个整数的最大公约数(GCD)是能同时整除这两个数且不留余数的最大正整数。欧几里得算法提供了一种优雅的方法:反复用两个数的差或余数替换较大的数,直到它们相等。我们也可以使用基于取模的版本:当 b ≠ 0 时,令 (a, b) = (b, a mod b);最终的 a 值即为最大公约数。

In Scratch, this algorithm can be implemented with variables "a" and "b", a "repeat until" block, and the modulo operator. This problem is excellent for teaching recursion and iteration. Students can also extend the project to compute the least common multiple (LCM) using the formula LCM(a, b) = |a × b| ÷ GCD(a, b).

在 Scratch 中,可以使用变量"a"和"b"、"重复执行直到"积木以及取余运算来实现该算法。这个问题非常适合教学递归和迭代。学生还可以扩展项目,利用公式 LCM(a, b) = |a × b| ÷ GCD(a, b) 来计算最小公倍数(LCM)。


5. Quadratic Equation Solver | 一元二次方程求解器

A quadratic equation in the form ax² + bx + c = 0 can be solved using the quadratic formula: x = (−b ± √b² − 4ac) ÷ (2a). The discriminant Δ = b² − 4ac determines the nature of the roots: if Δ > 0, there are two distinct real roots; if Δ = 0, there is one repeated real root; if Δ < 0, there are no real roots (two complex roots).

形式为 ax² + bx + c = 0 的一元二次方程可以使用求根公式求解:x = (−b ± √b² − 4ac) ÷ (2a)。判别式 Δ = b² − 4ac 决定根的性质:若 Δ > 0,有两个不同的实根;若 Δ = 0,有一个重根;若 Δ < 0,则没有实根(有两个复数根)。

Building a Scratch solver is straightforward. First, ask the user for inputs a, b, and c using the "ask" block. Next, compute the discriminant using the "sqrt" operator. Then, use conditional statements to display the appropriate solution(s). A challenge here is handling the square root of negative numbers; we can restrict to real solutions or introduce an imaginary unit representation.

构建 Scratch 求解器非常直接。首先,使用"询问"积木向用户请求输入 a、b 和 c。然后,使用"平方根"运算计算判别式。接下来,使用条件语句显示相应的解。这里的难点在于处理负数的平方根;我们可以限制为实数解,或者引入虚数单位表示。


6. Exploring Sequences with Loops | 用循环探索数列

Sequences and series are foundational concepts in mathematics. In Scratch, we can generate Fibonacci numbers, arithmetic progressions, and geometric progressions using loops. For instance, the Fibonacci sequence starts with 0 and 1, and each subsequent term is the sum of the two preceding terms: 0, 1, 1, 2, 3, 5, 8, 13, …

数列与级数是数学中的基础概念。在 Scratch 中,我们可以使用循环生成斐波那契数列、等差数列和等比数列。例如,斐波那契数列以 0 和 1 开头,后续每一项都是前两项之和:0、1、1、2、3、5、8、13、……

To generate the first n Fibonacci numbers, we maintain three variables: "prev1", "prev2", and "current". Each iteration computes the next term and updates the variables accordingly. This project helps students understand the relationship between recurrence relations and iterative processes, and it also introduces the golden ratio φ = (1 + √5) ÷ 2 ≈ 1.618.

为了生成前 n 个斐波那契数,我们维护三个变量:"前一项"、"前两项"和"当前项"。每次迭代计算下一项并相应地更新变量。这个项目帮助学生理解递推关系与迭代过程之间的联系,并引入黄金比例 φ = (1 + √5) ÷ 2 ≈ 1.618。


广告 / Sponsored

7. Numerical Integration and Area Estimation | 数值积分与面积估算

Numerical integration is a powerful technique for approximating the area under a curve. The Monte Carlo method is particularly well-suited to Scratch because it relies on random sampling. To estimate the area of a shape, we generate many random points within a bounding box and count how many fall inside the shape. The area is then approximated by the ratio of inside points to total points multiplied by the bounding box area.

数值积分是逼近曲线下面积的有力技术。蒙特卡洛方法特别适合 Scratch,因为它依赖于随机采样。要估算形状的面积,我们在边界框内生成大量随机点,并计算落在形状内的点数。面积则通过内部点数与总点数的比值乘以边界框面积来近似。

For example, to estimate π, consider a square of side length 2 centered at the origin, with an inscribed circle of radius 1. The ratio of the circle's area to the square's area is π ÷ 4. By generating random points in the square and checking whether x² + y² ≤ 1, we can estimate π with increasing accuracy as the number of trials grows. This project beautifully illustrates the law of large numbers.

例如,要估算 π,考虑一个以原点为中心、边长为 2 的正方形,其内切圆半径为 1。圆面积与正方形面积之比为 π ÷ 4。通过在正方形中生成随机点,并检查 x² + y² ≤ 1 是否成立,我们可以估算 π,并且随着试验次数的增加,精度不断提高。这个项目完美地诠释了大数定律。

π ≈ 4 × (points inside circle) ÷ (total points) | π ≈ 4 × (圆内点数) ÷ (总点数)


8. Probability and Dice Simulation | 概率与骰子模拟

Probability theory often feels abstract, but Scratch makes it tangible through simulation. Consider the experiment of rolling two fair six-sided dice and recording the sum. The possible sums range from 2 to 12, but they are not equally likely. The probability distribution is triangular: 7 is the most probable sum, with probability 6 ÷ 36 = 1 ÷ 6 ≈ 16.7%.

概率论常常显得抽象,但 Scratch 通过模拟使其变得直观。考虑掷两个公平的六面骰子并记录点数之和的实验。可能的和从 2 到 12,但它们并非等概率。概率分布呈三角形:7 是最可能的和,概率为 6 ÷ 36 = 1 ÷ 6 ≈ 16.7%。

In Scratch, we can use the "pick random" block to simulate each die, then tally the results across thousands of trials. A histogram can be displayed on the Stage using lists and pen commands. This hands-on approach deepens students' understanding of expected value, variance, and experimental vs. theoretical probability. It also provides a strong foundation for advanced statistics.

在 Scratch 中,我们可以使用"在…和…之间取随机数"积木来模拟每个骰子,然后在数千次试验中统计结果。使用列表和画笔命令可以在舞台上显示直方图。这种动手实践加深了学生对期望值、方差以及实验概率与理论概率之间关系的理解,也为高级统计学奠定了坚实基础。


9. Dynamic Geometry: Exploring the Pythagorean Theorem | 动态几何:探索勾股定理

Scratch is not only for arithmetic and algebra; it is also a dynamic geometry environment. We can draw right triangles on the Stage using the pen extension and verify the Pythagorean theorem: a² + b² = c². By creating sliders for the legs a and b using variables, students can observe how the hypotenuse c = √a² + b² changes in real time.

Scratch 不仅适用于算术和代数,它还是一个动态几何环境。我们可以使用画笔扩展在舞台上绘制直角三角形,并验证勾股定理:a² + b² = c²。通过使用变量创建直角边 a 和 b 的滑块,学生可以实时观察斜边 c = √a² + b² 的变化。

To draw the triangle, we use the "move" and "turn" blocks along with the Pythagorean theorem to calculate the side lengths. We can also extend this project to compute trigonometric ratios (sine, cosine, tangent) and verify identities. This integrates programming, geometry, and trigonometry in a single cohesive project that encourages exploratory learning.

要绘制三角形,我们使用"移动"和"旋转"积木,并结合勾股定理计算各边长度。我们还可以扩展此项目以计算三角比(正弦、余弦、正切)并验证恒等式。这将编程、几何和三角学整合在一个连贯的项目中,鼓励探索式学习。


10. Debugging and Optimization Strategies | 调试与优化策略

As with any programming endeavor, debugging is an integral part of the process. Common errors in Scratch mathematical projects include: forgetting to initialize variables, incorrect loop conditions (off-by-one errors), and misusing the modulo operator with negative numbers. A systematic debugging approach involves tracing variable values, using "say" or "show variable" blocks to display intermediate results, and testing with multiple input cases.

与任何编程工作一样,调试是过程中不可或缺的一部分。Scratch 数学项目中的常见错误包括:忘记初始化变量、循环条件不正确(差一错误)以及负数的取余运算使用错误。系统化调试方法包括追踪变量值、使用"说"或"显示变量"积木来展示中间结果,以及使用多组输入进行测试。

Optimization is equally important. For computationally intensive tasks like Monte Carlo simulations, we can reduce the number of visual updates or use the "turbo mode" option to increase execution speed. Additionally, rewriting inefficient algorithms (e.g., replacing naive primality testing with trial division up to √n) can drastically improve performance. These skills are transferable to text-based programming languages such as Python and Java.

优化同样重要。对于蒙特卡洛模拟等计算密集型任务,我们可以减少视觉更新次数,或使用"加速模式"选项来提高执行速度。此外,重写低效算法(例如,将朴素素性测试替换为试除到 √n)可以显著提升性能。这些技能可迁移到 Python 和 Java 等文本编程语言中。


11. Beyond Scratch: Connecting to Text-Based Programming | 超越Scratch:连接文本编程

Scratch serves as a stepping stone to professional programming languages. The concepts learned—variables, loops, conditionals, functions, and algorithms—are universal. For instance, the Euclidean algorithm implemented in Scratch can be written in Python in just a few lines using recursion or a while loop. Understanding the visual representation first makes the transition to syntax-based languages much smoother.

Scratch 是通往专业编程语言的垫脚石。所学的概念——变量、循环、条件、函数和算法——是通用的。例如,在 Scratch 中实现的欧几里得算法可以在 Python 中用递归或 while 循环仅用几行代码编写。先理解可视化表示,再过渡到基于语法的语言会顺畅得多。

Moreover, the mathematical thinking cultivated through Scratch problem-solving—breaking complex problems into smaller steps, recognizing patterns, and designing algorithms—is exactly what is assessed in exams like IGCSE Computer Science, A-Level Mathematics, and the International Baccalaureate (IB) programs. We encourage students to maintain a portfolio of their Scratch math projects as evidence of their computational thinking and problem-solving abilities.

此外,通过 Scratch 解决问题所培养的数学思维——将复杂问题分解为小步骤、识别模式、设计算法——正是 IGCSE 计算机科学、A-Level 数学和国际文凭(IB)课程等考试所考察的能力。我们鼓励学生保留他们的 Scratch 数学项目作品集,作为计算思维和解决问题能力的证明。


12. Conclusion: Empowering Mathematical Creativity | 结语:赋能数学创造力

Scratch transforms mathematics from a passive subject of memorized formulas into an active, creative discipline of discovery and experimentation. By building projects that solve problems from number theory, algebra, geometry, probability, and calculus, students not only master mathematical concepts but also develop essential 21st-century skills: computational thinking, logical reasoning, debugging, and iterative design.

Scratch 将数学从被动记忆公式的学科转变为一个主动的、创造性的探索与实验学科。通过构建解决数论、代数、几何、概率和微积分问题的项目,学生不仅掌握了数学概念,还培养了 21 世纪必备技能:计算思维、逻辑推理、调试和迭代设计。

We encourage all students to open the Scratch editor today, choose a mathematical problem that interests them, and begin building. Remember that every expert was once a beginner—and the most profound learning happens when we are free to experiment, make mistakes, and try again. Whether you are preparing for an examination or simply exploring the beauty of mathematics, Scratch is your faithful companion on this exciting journey.

我们鼓励所有学生今天打开 Scratch 编辑器,选择一个感兴趣的数学问题,并开始构建。请记住,每位专家都曾是初学者——而最深刻的学习发生在我们可以自由实验、犯错并再次尝试的时候。无论您是在备考,还是仅仅在探索数学之美,Scratch 都是您在这段激动人心旅程中的忠实伙伴。

Published by TutorHao | Computer Science Revision Series | aleveler.com

广告 / Sponsored
Linux高级程序设计(高等院校计算机技术十二五规划教材)/刘加海/季江民/浙江大学出版社
计算机 备考教辅 · 正版现货 · 与本文内容相关
查看详情 / View on Taobao参考价 ¥55.6

更多咨询请联系16621398022(同微信)

备考资讯每日更新,关注不错过
考纲变化 · 科目分析 · 学习方法
扫码关注公众号 tutorhao
在微信里长按二维码识别关注
或截图后用「扫一扫 → 相册」选择本图
公众号:tutorhao