-
MathType
-
WirisQuizzes
-
LearningLemur
-
CalcMe
-
MathPlayer
-
Store FAQ
-
VPAT for the electronic documentation
-
MathFlow
-
BF FAQ
-
Miscellaneous
-
Wiris Integrations
How to generate a right-angled triangle with integer sides (Pythagorean triple)
Reading time: 2minWhen 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:
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
n_val = random(1,5)
m_val = random(n_val + 1, n_val + 5)This guarantees that , 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)
endThis ensures:
- and 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^2This produces a right triangle with integer sides.
Optionally scale the triple
k = random(1,3)
a = k*a
b = k*b
c = k*cScaling allows you to generate non-primitive triples as well.
Verify it worked
- Preview the question multiple times
- Confirm that .
- 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*cOptions 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_valandn_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