Wiris

Documentation / Nubric

  • Demos
  • Visit our website
  • Contact us
  • MathType

    • WirisQuizzes

      • Nubric

        • CalcMe

          • MathPlayer

            • Store FAQ

              • MathFlow

                • BF FAQ

                  • Miscellaneous

                    • Wiris Integrations

                      • Home
                      • Nubric
                      • Using Nubric
                      • Advanced Logic in Nubric
                      • Advanced Logic Examples in Nubric
                      • Advanced Logic Examples in Nubric

                      How to generate a quadratic equation with rational solutions

                      Reading time: 2min

                      Use this method when you want to generate quadratic equations whose solutions are guaranteed to be rational numbers (instead of irrational or complex roots).

                      Following this guide, you will create a quadratic equation ax2+bx+c=0ax^2 + bx + c = 0where the discriminant is always a perfect square, ensuring that the solutions are rational.

                      See it in action: Watch how this logic is implemented inside Nubric:

                      Your browser does not support HTML5 video.

                      Before you begin

                      Requirements

                      • Basic knowledge of how to create a Nubric 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])
                      end
                      This loop ensures that the discriminant b2−4acb^2 - 4acis a perfect square. If it is not, a new value for c 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)

                      Related

                      • Understanding Advanced Logic in Nubric
                      • Common Patterns and Best Practices
                      • Glossary of Commands

                      Was this article helpful?

                      Give feedback about this article

                      Related Articles

                      • Solve
                      • Leading coefficient

                      How to generate a quadratic equation with rational solutions

                      Before you begin Steps Verify it worked Full algorithm (copy-paste version) Options and variations Common errors Related

                      Empowering STEM education

                      MathType

                      • Office Tools
                      • LMS
                      • XML
                      • HTML

                      WirisQuizzes

                      Nubric

                      Integrations

                      Solutions

                      • Education
                      • Publishing houses – platforms and interactive
                      • Publishing houses – Print and digital
                      • Technical writers

                      Pricing

                      Downloads

                      Blog

                      • Success stories

                      About us

                      • Careers
                      • Partnership

                      Contact Us

                      Contact Sales

                      European union (European Regional Development Fund) and 1EdTech (TrustEd Apps Certified)
                      • Cookie Policy
                      • Terms of Use
                      • Privacy Policy / GDPR
                      • Student Data Privacy
                      • Compliance
                      • Cookie Settings

                      © Wiris 2026

                      Expand