Wiris

Documentation / LearningLemur

  • Demos
  • Visit our website
  • Contact us
  • MathType

    • WirisQuizzes

      • LearningLemur

        • CalcMe

          • MathPlayer

            • Store FAQ

              • MathFlow

                • BF FAQ

                  • Miscellaneous

                    • Wiris Integrations

                      • Home
                      • LearningLemur
                      • Using LearningLemur
                      • Advanced Logic in LearningLemur
                      • Advanced Logic Examples in LearningLemur
                      • Advanced Logic Examples in LearningLemur

                      How to generate a right-angled triangle with integer sides (Pythagorean triple)

                      Reading time: 2min

                      Use this method when you want to create geometry or trigonometry exercises involving right-angled triangles with integer side lengths.

                      Following this guide, you will generate a right-angled triangle whose sides form a (possibly scaled) Pythagorean triple, ensuring all side lengths are integers.

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

                      Your browser does not support HTML5 video.

                      Before you begin

                      Requirements

                      • Basic knowledge of how to create a LearningLemur question
                      • Familiarity with adding an algorithm to generate random variables

                      Steps

                      Generate valid parameters m>nm > n

                      n_val = random(1,5)
                      m_val = random(n_val + 1, n_val + 5)

                      This guarantees that m>nm > n, a necessary condition for Euclid's formula.

                      Enforce coprimality and parity conditions.

                      while gcd(m_val, n_val) != 1 || (odd?(m_val)=true && odd?(n_val)=true) 
                          n_val = random(1,5)
                          m_val = random(n_val + 1, n_val + 5)
                      end

                      This ensures:

                      • mm and nn are coprime
                      • They are not both odd.

                      Together, these conditions generate a primitive Pythagorean triple.

                      Apply Euclid's formula.

                      a = m_val^2 - n_val^2
                      b = 2 * m_val * n_val
                      c = m_val^2 + n_val^2

                      This produces a right triangle with integer sides.

                      Optionally scale the triple.

                      k = random(1,3)
                      a = k*a
                      b = k*b
                      c = k*c

                      Scaling allows you to generate non-primitive triples as well.

                      Verify it worked

                      • Preview the question multiple times.
                      • Confirm that a2+b2=c2a^2 + b^2 = c^2.
                      • Ensure all sides are integers.
                      • Verify that no degenerate triangle appears.

                      Full algorithm (copy-paste version)

                      Use the complete version below if you want to copy the logic directly into your question algorithm:

                      # Generate two random integers m and n for Euclid's formula (m > n)
                      n_val = random(1,5)
                      m_val = random(n_val + 1, n_val + 5)
                      
                      # Ensure m and n are coprime and not both odd
                      while gcd(m_val, n_val) != 1 || (odd?(m_val)=true && odd?(n_val)=true) 
                          n_val = random(1,5)
                          m_val = random(n_val + 1, n_val + 5)
                      end
                      
                      # Use Euclid's formula to generate a Pythagorean triple
                      a = m_val^2 - n_val^2
                      b = 2 * m_val * n_val
                      c = m_val^2 + n_val^2
                      
                      # Optionally, scale the triple by a random factor k
                      k = random(1,3)
                      a = k*a
                      b = k*b
                      c = k*c

                      Options and variations

                      • If you want only primitive triples, you can remove the scaling section (k).
                      • If you want larger triangles, you can increase the random range for m_val and n_val
                      • If you want to avoid very large numbers, you can reduce the upper bound of the interval.

                      Common errors

                      Generated sides are not integers.
                      Ensure Euclid's formula is written exactly as shown

                      Repeated regeneration in the while loop.
                      Broaden the interval for m_val and n_val

                      Unexpected degenerate triangles.
                      Confirm that the condition m_val > n_val is enforced

                      Related

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

                      Was this article helpful?

                      Give feedback about this article

                      Related Articles

                      • Polygon
                      • Equilateral
                      • Icosahedron
                      • How to generate a quadratic equation with rational solutions

                      How to generate a right-angled triangle with integer sides (Pythagorean triple)

                      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

                      Learning Lemur

                      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