100 Random Single Variable Linear Equations

Liste des GroupesRevenir à cl awk 
Sujet : 100 Random Single Variable Linear Equations
De : porkchop (at) *nospam* invalid.foo (Mike Sanders)
Groupes : comp.lang.awk
Date : 06. Dec 2024, 04:46:32
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vits2o$240vr$1@dont-email.me>
User-Agent : tin/2.6.2-20221225 ("Pittyvaich") (NetBSD/9.3 (amd64))
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

Date Sujet#  Auteur
6 Dec 24 * 100 Random Single Variable Linear Equations23Mike Sanders
6 Dec 24 +* Re: 100 Random Single Variable Linear Equations19Mike Sanders
6 Dec 24 i`* Re: 100 Random Single Variable Linear Equations18Janis Papanagnou
6 Dec 24 i +* Re: 100 Random Single Variable Linear Equations3Janis Papanagnou
7 Dec 24 i i+- Re: 100 Random Single Variable Linear Equations1Mike Sanders
7 Dec 24 i i`- Re: 100 Random Single Variable Linear Equations1Mike Sanders
6 Dec 24 i `* Re: 100 Random Single Variable Linear Equations14Mike Sanders
6 Dec 24 i  +* Re: 100 Random Single Variable Linear Equations3Janis Papanagnou
7 Dec 24 i  i`* Re: 100 Random Single Variable Linear Equations2Christian Weisgerber
7 Dec 24 i  i `- Re: 100 Random Single Variable Linear Equations1Mike Sanders
6 Dec 24 i  +* Re: 100 Random Single Variable Linear Equations3Keith Thompson
7 Dec 24 i  i`* Re: 100 Random Single Variable Linear Equations2Mike Sanders
7 Dec 24 i  i `- Re: 100 Random Single Variable Linear Equations1Mike Sanders
7 Dec 24 i  `* Re: 100 Random Single Variable Linear Equations7Mike Sanders
7 Dec 24 i   `* Re: 100 Random Single Variable Linear Equations6Mike Sanders
7 Dec 24 i    `* Re: 100 Random Single Variable Linear Equations5Janis Papanagnou
8 Dec 24 i     `* Re: 100 Random Single Variable Linear Equations4Mike Sanders
8 Dec 24 i      `* Re: 100 Random Single Variable Linear Equations3Janis Papanagnou
9 Dec 24 i       `* Re: 100 Random Single Variable Linear Equations2Mike Sanders
9 Dec 24 i        `- Re: 100 Random Single Variable Linear Equations1Janis Papanagnou
8 Dec 24 +* Re: 100 Random Single Variable Linear Equations2Mike Sanders
8 Dec 24 i`- Re: 100 Random Single Variable Linear Equations1Mike Sanders
8 Dec 24 `- Re: 100 Random Single Variable Linear Equations1Mike Sanders

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal