-
MathType
-
WirisQuizzes
-
LearningLemur
-
CalcMe
-
MathPlayer
-
Store FAQ
-
VPAT for the electronic documentation
-
MathFlow
-
BF FAQ
-
Miscellaneous
-
Wiris Integrations
How to generate a quadratic equation with rational solutions
Reading time: 2minWhen to use this: Use this method when you want to generate quadratic equations whose solutions are guaranteed to be rational numbers (instead of irrational or complex roots).
What you'll achieve: You will create a quadratic equation where the discriminant is always a perfect square, ensuring that the solutions are rational.
See it in action: Watch how this logic is implemented inside LearningLemur:
Before you begin
Requirements
- Basic knowledge of how to create a LearningLemur question
- Familiarity with adding an algorithm to generate random variables
Steps
Generate random coefficients
a = random([-10..10]/[0])
b = random([-10..10]/[0])
c = random([-10..10]/[0])This generates non-zero coefficients for the quadratic equation.
Enforce a perfect square discriminant
while square?(b^2-4*a*c) == false
do c = random([-10..10]/[0])
endc is generated until the condition is satisfied.Compute the solutions
sol1 = (-b + sqrt(b^2-4*a*c)) / (2*a)
sol2 = (-b - sqrt(b^2-4*a*c)) / (2*a)These values can be used in grading logic or as expected answers.
Verify it worked
- Preview the question multiple times
- Confirm that the discriminant is always a perfect square
- Check that the computed solutions are rational numbers
- Ensure no irrational square roots appear in previews
Full algorithm (copy-paste version)
Use the complete version below if you want to copy the logic directly into your question algorithm:
# Generate random values for the coefficients
a = random([-10..10]/[0])
b = random([-10..10]/[0])
c = random([-10..10]/[0])
# Ensure the discriminant is a perfect square
while square?(b^2-4*a*c) == false
do c = random([-10..10]/[0])
end
# Compute solutions
sol1 = (-b + sqrt(b^2-4*a*c)) / (2*a)
sol2 = (-b - sqrt(b^2-4*a*c)) / (2*a)Options and variations
- If you want integer solutions only, you can add additional constraints to ensure the solutions evaluate to integers (e.g., restrict coefficient intervals further)
- If you want only one repeated root, you can force the discriminant to equal 0 instead of being a perfect square
- If you want smaller coefficients, you can reduce the interval (e.g.,
[-5..5]/[0]) to simplify calculations
Common errors
Infinite loop in the while statement
The interval may be too restrictive → Broaden the coefficient range
Unexpected irrational solutions
Check that square?(...) is correctly written and applied to the discriminant
Division by zero error
Ensure a is never 0 (already handled by [0] exclusion)