Groups | Search | Server Info | Login | Register


Groups > comp.lang.awk > #9876

100 Random Single Variable Linear Equations

From porkchop@invalid.foo (Mike Sanders)
Newsgroups comp.lang.awk
Subject 100 Random Single Variable Linear Equations
Date 2024-12-06 03:46 +0000
Organization A noiseless patient Spider
Message-ID <vits2o$240vr$1@dont-email.me> (permalink)

Show all headers | View raw


Beware wordwrap...

# algebra.awk: 2024 - Michael Sanders
#
# usage: awk -f algebra.awk > solve.txt
#
# outputs 100 random single variable linear equations in the form: ax+b=c
#
# where...
#
# a, b, & c are constants (numbers)
# x is the variable
#
# example output...
#
# 001. 18x * 20 = 27
# 002. 1x * 16 = 31
# 003. 10x / 8 = 16
#
# solving for x...
#
# 1. solving for x means finding the value of x that makes the equation true.
#
# 2. how to do it:
#   - look at the equation & see if x is combined with numbers or other terms.
#   - use inverse operations to "cancel out" numbers or terms that are with x.
#
#   for example:
#   - if x is multiplied by a number, divide both sides of the equation by that number.
#   - if x is divided by a number, multiply both sides of the equation by that number.
#   - if x is added to a number, subtract that number from both sides.
#   - if x has a number subtracted from it, add that number to both sides.
#
# 3. example, solve for x in the equation: 2x + 5 = 11
#
#   - step 1: subtract 5 from both sides:
#     to remove the +5 from the left side, subtract 5 from both sides:
#     (2x + 5) - 5 = 11 - 5
#
#     simplify:
#     2x = 6
#
#   - step 2: divide both sides by 2:
#     to remove the 2 multiplying x, divide both sides by 2:
#     (2x) / 2 = 6 / 2
#
#    simplify:
#    x = 3
#
# 4. final answer: x = 3
#
# 5. why it works:
#    you perform the same operation on both sides of the equation,
#    keeping it balanced, until x is by itself on one side.
#
# further reading: https://en.wikipedia.org/wiki/Algebra

BEGIN {
    srand() # seed random number generator

    for (q = 1; q <= 100; q++) {
        # generate random coefficients & constant
        a = int(rand() * 20) + 1 # random value for 'a' (1 to 20)
        b = int(rand() * 20) + 1 # random value for 'b' (1 to 20)
        c = int(rand() * 50) + 1 # random value for 'c' (1 to 50)

        opc = (rand() < 0.5 ? "*" : "/")      # random operator
        lhs = sprintf("%dx %s %d", a, opc, b) # left-hand side
        rhs = c                               # right-hand side

        # format equation number with zero-padding
        equation = sprintf("%03d", q)

        # blank lines after equations
        bla = sprintf("\n\n\n\n\n\n\n\n\n")

        # print formated equation
        printf("%s. %s = %d%s", equation, lhs, rhs, bla)

    }
}

# eof

-- 
:wq
Mike Sanders

Back to comp.lang.awk | Previous | NextNext in thread | Find similar


Thread

100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-06 03:46 +0000
  Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-06 04:51 +0000
    Re: 100 Random Single Variable Linear Equations Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-12-06 13:43 +0100
      Re: 100 Random Single Variable Linear Equations Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-12-06 13:59 +0100
        Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-07 02:39 +0000
        Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-07 20:42 +0000
      Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-06 13:38 +0000
        Re: 100 Random Single Variable Linear Equations Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-12-06 21:01 +0100
          Re: 100 Random Single Variable Linear Equations Christian Weisgerber <naddy@mips.inka.de> - 2024-12-06 23:05 +0000
            Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-07 01:36 +0000
        Re: 100 Random Single Variable Linear Equations Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-12-06 12:53 -0800
          Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-06 22:05 +0000
            Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-07 02:06 +0000
        Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-06 22:04 +0000
          Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-07 01:49 +0000
            Re: 100 Random Single Variable Linear Equations Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-12-07 03:21 +0100
              Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-08 06:40 +0000
                Re: 100 Random Single Variable Linear Equations Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-12-08 15:15 +0100
                Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-09 16:43 +0000
                Re: 100 Random Single Variable Linear Equations Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-12-09 21:10 +0100
  Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-08 06:06 +0000
    Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-08 07:16 +0000
  Re: 100 Random Single Variable Linear Equations porkchop@invalid.foo (Mike Sanders) - 2024-12-08 11:02 +0000

csiph-web