How to Make a Program to Factor Quadratics in Scratch

by Joshuar9202 in Circuits > Software

490 Views, 1 Favorites, 0 Comments

How to Make a Program to Factor Quadratics in Scratch

Screenshot_20230220_122159.png

Factoring quadratics, especially with big numbers can be very problematic as it can take a long time. Most people would do it by either taking 2 numbers and seeing if they work, or by using the grouping method where they split the middle term up. However, both of those methods would take quite a while to do as you have to make an educated guess. So I thought "well a computer can take more pairs of numbers in a given amount of time compared to a human an check them faster, so why not make a program to do this for me?"

disclaimer: the 'a' value of the quadratic has to be equal to one or the program will not work. Also the equation has in standard form

Supplies

scratch

Creating the Variables

Screenshot_20230220_111745.png

There are some variables we have to make first.

The maxVal and minVal variables just stores the range of numbers that the program will look through.

The x and y variables are the potential numbers that the program will use to test if it works.

The b_coefficient variable is the b term in the quadratic equation.

The c_coefficient variable is the c term in the quadratic equation.

The works variable will always be at 0 until the program works and when it does it will act in a similar fashion to a loop interrupt

The loopNumber variable is how many times the loop goes around which is what the x value will start at (the first test number)

The Loop to Test Out the Combinations

Screenshot_20230220_112920.png

The program will keep testing different numbers until it works, which is util works = 1. To do this, it will all be in a repeat until block with works = 1 as the condition. Then we set x to be the loopNumber, so everytime the second number 'y' cycles through the maximum value that you tell it to, it will add one. This way it can check all the possible combinations of numbers between the maximum and minimum values that you set. Since I set my max to 300 and minimum to -300, an example of what it does is when the program starts x will be -300 and y will be -300 and the next time the loop goes around, x will still be -300 and y will be -299 and it will cycle until y = 300 in which case y is reset back to the minimum value and the loopNumber will add one so x will be -299. Each time the loop goes around, the program will check if the numbers work and if they do, the loop will end, but if they don't, it will continue until it finds a combination that works.

Outputting the Factors

Screenshot_20230220_113019.png
Screenshot_20230220_113036.png

I made two sprites that I drew, one is factor 1 and the other is factor 2. They both have the same code in them, but factor 1 says the x value and factor 2 says the y value. Because of this, when you want to change the values of the b and c terms, you have to change it in both the sprites. Keep in mind that to fully be in factored form of the quadratic, when you write it down make sure to put (x + factor one in the program) times (x + factor 2 in the program). Even though the program works only when the 'a' term is 1, I still found it to be pretty useful as most of the time when you take out the GCF of the quadratic, the 'a' value turns out to be one.

Testing It Out

Screenshot_20230220_114019.png

Really this program can simplify any quadratic with the 'a' value equal to one, just remember to change the maximum and minimum values if the b and c terms of the equation are out of the max and minimum values' range.