Documentation / LearningLemur

  • Demos
  • Visit our website
  • Contact us
  • MathType

    • WirisQuizzes

      • LearningLemur

        • CalcMe

          • MathPlayer

            • Store FAQ

              • VPAT for the electronic documentation

                • 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

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

                        What you'll achieve: 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?

                        Yes
                        No
                        Give feedback about this article

                        Related Articles

                        • Polygon
                        • Equilateral
                        • Icosahedron

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

                        Before you begin Requirements Steps Generate valid parameters Enforce coprimality and parity conditions Apply Euclid's formula Optionally scale the triple Verify it worked Full algorithm (copy-paste version) Options and variations Common errors Generated sides are not integers Repeated regeneration in the while loop Unexpected degenerate triangles Related

                        Making people’s STEM work more meaningful

                        MathType

                        • MathType for Office Tools
                        • MathType for Mac
                        • MathType for Microsoft 365
                        • MathType for Google Workspace
                        • MathType for LMS
                        • MathType for XML Editors
                        • Arabic notation
                        • Our products accessibility
                        • MathType is online

                        WirisQuizzes

                        Learning Lemur

                        Solutions for Education

                        • Blackboard Learn
                        • Brightspace by D2L
                        • Canvas
                        • Google Classroom
                        • Moodle
                        • Schoology

                        Solutions for Publishing Houses

                        Solutions for Technical Writers

                        Solutions for Chemistry

                        Integrations

                        • HTML Editors
                        • MathType in WordPress

                        Pricing

                        Company

                        Careers

                        Blog

                        Contact Us

                        Buy Now

                        Plugin Downloads

                        © Wiris 2025

                        • Cookie Settings
                        • Cookie Policy
                        • Terms of Use
                        • Privacy Policy / GDPR
                        • Student Data Privacy
                        • Compliance
                        • Powered by Helpjuice
                        Expand