Documentation / LearningLemur

  • Demos
  • Visit our website
  • Contact us
  • MathType

    • Wiris Quizzes

      • Learning Lemur

        • CalcMe

          • MathPlayer

            • Store FAQ

              • VPAT for the electronic documentation

                • MathFlow

                  • BF FAQ

                    • Miscellaneous

                      • Home
                      • Learning Lemur
                      • Basic guide to Learning Lemur
                      • Using advanced logic
                      • Using advanced logic

                      Examples

                      Reading time: 4min

                      Sum of two random irreducible fractions

                      Consider creating an exercise where students must calculate the sum of two randomly generated irreducible fractions.

                      # 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
                      # The solution the students will calculate
                      sol = a/b + c/d                     

                       

                      Solving linear equations with integer solutions

                      Consider creating a linear equation with random coefficients, ensuring that the solution is an integer.

                      # Generate a quadratic equation ax^2 + bx + c = 0
                       with integer/rational solutions by ensuring discriminant is a perfect square.
                      
                      # 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. If not, regenerate value "c". 
                      
                      while square?(b^2-4*a*c) == false
                      do c = random([-10..10]/[0])
                      
                      
                      # At this point, we have a valid quadratic equation with rational solutions
                      solution1 = (-b + sqrt(discriminant)) / (2*a)
                      solution2 = (-b - sqrt(discriminant)) / (2*a)

                      Tip: When you're using the same logic multiple times—like generating random numbers with the same rules—you can define a function to avoid repeating yourself. This makes your code easier to read and maintain.

                      r():= random([-10..10]/[0])
                      a=r()
                      b=r()
                      c=r()
                       

                       

                      Greatest common divisor of two random integers

                      Consider creating an exercise where students must calculate the GCD of two random integers.

                      # 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
                      
                      # Students should find the gcd of "x" and "y"
                      solution = target_gcd

                       

                      Division with no remainder (exact division)

                      Consider creating an exercise where students divide two integers, ensuring that the result is an exact integer without remainder

                      # Select a random divisor from 2 to 12
                      divisor = random(2,12)
                      
                      # Choose a random integer quotient between 2 and 10
                      quotient = random(2,10)
                      
                      # Compute dividend ensuring exact division
                      dividend = divisor * quotient
                      
                      # Students perform the division: dividend ÷ divisor
                      solution = quotient

                       

                      Generating a right-angled triangle with integer sides (Pythagorean Triple)

                      Create an exercise involving a right-angled triangle where side lengths are integers (a Pythagorean triple).
                       

                      # 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 > n
                      
                      # Ensure m and n are coprime and not both odd (to avoid common factors in a,b,c)
                      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
                      
                      # Variables a and b are the legs, and c is the hypotenuse.
                      # These can be used for problems on Pythagorean theorem, trigonometry, etc.
                      

                       

                      Generating a right-angled triangle with integer sides (Pythagorean Triple)

                      Create an exercise involving a right-angled triangle where side lengths are integers (a Pythagorean triple).
                       

                      # 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 > n
                      
                      # Ensure m and n are coprime and not both odd (to avoid common factors in a,b,c)
                      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
                      
                      # Variables a and b are the legs, and c is the hypotenuse.
                      # These can be used for problems on Pythagorean theorem, trigonometry, etc.
                      

                      This algorithm uses Euclid's formula for generating Pythagorean triples: a=m2−n2, b=2mn, c=m2+n2.

                      1. It generates two integers m_val and n_val with m_val > n_val.
                      2. A while loop ensures that m_val and n_val are coprime (gcd(m_val, n_val) == 1) and that they are not both odd (this condition, along with coprimality, ensures a primitive Pythagorean triple, which can then be scaled). If the conditions aren't met, new m_val and n_val are generated.
                      3. The sides a, b, and c are calculated.
                      4. Optionally, these sides are scaled by a random integer k to produce non-primitive triples as well.
                       

                       

                      Generating a 3x3 system of linear equations with a unique integer solution

                      Create a system of three linear equations in three variables that is guaranteed to have a unique solution, and all solution components are integers.
                       

                      # Define a function to generate random integers, excluding zero if needed
                      r() := random([-5..5]/[0]) 
                      
                      # Generate a coefficient matrix A with a non-zero determinant
                      repeat
                          A = [ [r(), r(), r()],
                                 [r(), r(), r()],
                                 [r(), r(), r()] ]
                      until determinant(A) != 0
                      
                      # Generate a unique integer solution vector 's'
                      s_x = r() # Ensure solution components are non-zero for more interesting problems
                      s_y = r()
                      s_z = r()
                      solution_vect = [ s_x, s_y, s_z ]
                      
                      # Calculate the right-hand side vector 'b' such that A*solution_vector = b
                      b_vect = A * solution_vect
                      
                      # Extract coefficients for the question
                      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)
                      
                      # The system for students to solve is:
                      # a11*x + a12*y + a13*z = b1
                      # a21*x + a22*y + a23*z = b2
                      # a31*x + a32*y + a33*z = b3
                      # The solution is (s_x, s_y, s_z)
                      

                       

                      Generating Linearly Independent/Dependent 3D Vectors

                      Create an exercise where students need to determine if a set of three 3D vectors is linearly independent or dependent, with a controlled outcome.

                      # Generate two linearly independent 3D vectors u and v
                      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])
                      
                      # Ensure v is not a multiple of u 
                      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]
                      
                      # Generate random non-zero scalars k1 and k2
                      k1 = random([-3..3] / [0])
                      k2 = random([-3..3] / [0])
                      
                      # Create vector w as a linear combination of u and v, ensuring it's dependent
                      w = k1 * u + k2 * v
                      
                      # The set {u, v, w} is now linearly dependent.
                      
                      
                      # To generate a linearly independent set, ensure the third vector is is NOT a linear combination. Let's create a new third vector t
                      t1 = random([-5..5] / [0])
                      t2 = random([-5..5] / [0])
                      t3 = random([-5..5] / [0])
                      
                      #Ensure t is not linear dependent to u and v
                      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]
                      
                      # The set {t1, t2, t3} is now linearly independent.
                      

                       

                       

                       

                      Was this article helpful?

                      Yes
                      No
                      Give feedback about this article

                      Related Articles

                      • Introduction

                      Examples

                      Sum of two random irreducible fractions Solving linear equations with integer solutions Greatest common divisor of two random integers Division with no remainder (exact division) Generating a right-angled triangle with integer sides (Pythagorean Triple) Generating a right-angled triangle with integer sides (Pythagorean Triple) Generating a 3x3 system of linear equations with a unique integer solution Generating Linearly Independent/Dependent 3D Vectors

                      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