-
MathType
-
WirisQuizzes
-
Learning Lemur
-
CalcMe
-
MathPlayer
-
Store FAQ
-
VPAT for the electronic documentation
-
MathFlow
-
BF FAQ
-
Miscellaneous
-
Wiris Integrations
Advanced Logic Examples in Learning Lemur
Reading time: 4minWhen to use this: Use this guide when you need to control how random values are generated in your questions or enforce specific mathematical properties (e.g., irreducible fractions, integer solutions, exact division, controlled vector dependence).
What you'll achieve: You will be able to implement reusable advanced logic patterns that:
- Guarantee specific mathematical constraints
- Avoid invalid or trivial cases
- Ensure pedagogically controlled outcomes
Before you begin
Requirements
- Basic knowledge of how to create a standard Learning Lemur question
- Familiarity with random variables and question algorithms
Steps
Each section below presents a reusable advanced logic pattern. You can copy the logic directly into your question algorithm and adapt it as needed.
Pattern 1 — Sum of two random irreducible fractions
Goal: Generate two random fractions that are already irreducible and compute their sum.
Algorithm
# Generate the first fraction
# Choose a random numerator "a" from 1 to 9
a = random(1,9)
# Define set "B" of denominators that are coprime with "a"
B = { b with b in 2..9 where gcd(a,b)=1 }
# Choose random denominator "b" from set B
b = random(B)
# Generate the second fraction
# Choose another random numerator "c" from 1 to 9
c = random(1,9)
# Define set "D" of denominators coprime with "c", different from "b"
D = { d with d in 2..9 where gcd(c,d)=1 && d!=b }
# Choose random denominator "d" from set "D"
d = random(D)
# Calculate the sum of the two irreducible fractions
sol = a/b + c/dWhat this ensures
Both fractions are irreducible before students compute the sum.
Pattern 2 — Solving quadratic equations with rational solutions
Goal: Generate a quadratic equation whose discriminant is a perfect square, ensuring rational solutions.
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=b^2-4*a*c 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)What this ensures
The generated equation always has rational solutions.
Pattern 3 — Greatest common divisor of two random integers
Goal: Generate two integers whose GCD is a predefined value.
Algorithm
# Randomly select target GCD between 2 and 10
target_gcd = random(2,10)
# Select two random integers that are coprime (so gcd=1)
repeat
m = random(2,10)
n = random([2,10]/[m])
until gcd(m, n) == 1
# Generate integers ensuring exact GCD is target_gcd
x = m * target_gcd
y = n * target_gcd
solution = target_gcdWhat this ensures
The generated integers always have the exact target GCD.
Pattern 4 — Division with no remainder (exact division)
Goal: Ensure the division of two integers produces an exact integer result.
# Select a random divisor from 2 to 12
div = random(2,12)
# Choose a random integer quotient between 2 and 10
quot = random(2,10)
# Compute dividend ensuring exact division
dividend = div * quot
# Students perform the division
solution = quotWhat this ensures
Students never encounter unintended decimal results.
Pattern 5 — Generating a right-angled triangle (Pythagorean triple)
Goal: Generate integer side lengths of a right-angled triangle.
# 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
a = m_val^2 - n_val^2
b = 2 * m_val * n_val
c = m_val^2 + n_val^2
# Optionally scale
k = random(1,3)
a = k*a
b = k*b
c = k*cWhat this ensures
The generated triangle always has integer sides satisfying the Pythagorean theorem.
Pattern 6 — Generating a 3x3 system with a unique integer solution
Generate a linear system with guaranteed unique integer solution.
r() := random([-5..5]/[0])
repeat
A = [ [r(), r(), r()],
[r(), r(), r()],
[r(), r(), r()] ]
until determinant(A) != 0
sx = r()
sy = r()
sz = r()
solution_vect = [ sx, sy, sz ]
b_vect = A * solution_vect
a11 = A subindex_operator(1,1)
a12 = A subindex_operator(1,2)
a13 = A subindex_operator(1,3)
a21 = A subindex_operator(2,1)
a22 = A subindex_operator(2,2)
a23 = A subindex_operator(2,3)
a31 = A subindex_operator(3,1)
a32 = A subindex_operator(3,2)
a33 = A subindex_operator(3,3)
b1 = b_vect subindex_operator(1)
b2 = b_vect subindex_operator(2)
b3 = b_vect subindex_operator(3)What this ensures
The system always has a unique integer solution known in advance.
Pattern 7 — Generating linearly independent or dependent 3D vectors
Control whether a set of 3D vectors is linearly dependent or independent.
u1 = random([-5..5] / [0])
u2 = random([-5..5] / [0])
u3 = random([-5..5] / [0])
u = [u1, u2, u3]
v1 = random([-5..5] / [0])
v2 = random([-5..5] / [0])
v3 = random([-5..5] / [0])
while vectorial_product(u,[v1,v2,v3])==[0,0,0]
v1 = random([-5..5] / [0])
v2 = random([-5..5] / [0])
v3 = random([-5..5] / [0])
end
v = [v1, v2, v3]
k1 = random([-3..3] / [0])
k2 = random([-3..3] / [0])
w = k1 * u + k2 * v
t1 = random([-5..5] / [0])
t2 = random([-5..5] / [0])
t3 = random([-5..5] / [0])
while determinant([u,v,[t1,t2,t3]]) == 0
t1 = random([-5..5] / [0])
t2 = random([-5..5] / [0])
t3 = random([-5..5] / [0])
end
t = [t1, t2, t3]What this ensures
You can explicitly control whether the generated vectors are dependent or independent.
Verify it worked
- The generated data always satisfies the intended mathematical property
- No unintended edge cases appear during multiple previews
- Students cannot encounter logically invalid instances.
Options and variations
- If you need better performance: Reduce the size of random intervals to avoid long regeneration loops
- If you need reusable logic: Define helper functions (e.g., r():= random([-10..10]/[0])) to simplify code
- If you want to enforce simplified answers instead of simplified generation: Apply constraints in the grading logic instead of in the random generation stage
Common errors
- Infinite loop in generation: Your constraint may be too restrictive → Broaden the allowed interval or adjust the condition
- Unexpected decimal solutions: Check that integer-only constraints are enforced in the generation logic
- Red validation box in answer field when entering lists: The editor expects a single expression → This does not affect grading if the logic is correct