Re: 100 Random Single Variable Linear Equations

Liste des GroupesRevenir à cl awk 
Sujet : Re: 100 Random Single Variable Linear Equations
De : porkchop (at) *nospam* invalid.foo (Mike Sanders)
Groupes : comp.lang.awk
Date : 08. Dec 2024, 12:02:55
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vj3ucv$3oa44$1@dont-email.me>
References : 1
User-Agent : tin/2.6.2-20221225 ("Pittyvaich") (NetBSD/9.3 (amd64))
final iteration: family/work/time-constraints/etc

earnest thanks to all (bonus points: Janis Papanagnou)

# algebra.awk - Michael Sanders 2024
#
# invocation: awk -f algebra.awk -v SEED=$RANDOM > solve.txt
#
# generates unique algebraic equations in 3 forms...
#
# ax + b = c
# ax + bx = c
# ax + b + x = c
#
# where...
#
# x is the variable (solve for x)
# a, b, & c are constants (numbers)
#
# operators: + addition, - subtraction, * multiplication, / division
#
# notes...
#
# - not all equations will be pedantically/academically proper
# - SEED is optional & can be reused
# - post improvements in comp.lang.awk
#
# further reading: https://en.wikipedia.org/wiki/Algebra

BEGIN {

    # seed random number generator
    if (SEED+0 != SEED) SEED = 1; srand(SEED)

    g = 100       # number of unique equations to generate
    p = length(g) # uniform padding for serial number

    do {
        a = rnd(1, 20)                  # random value for x coefficient
        b = rnd(1, 99)                  # random value for b constant
        c = rnd(1, 500)                 # random value for c constant
        n = (rnd(1, 2) == 1) ? "-" : "" # random negative for x coefficient
        f = rnd(1, 3)                   # random equation form
        op1 = rop()                     # 1st random operator

        if (f == 1) {
            # simple equation: ax op1 b = c
            e = sprintf("%s%dx %s %d = %d", n, a, op1, b, c)
        } else if (f == 2) {
            # medium complexity: ax op1 b2x = c
            b2 = rnd(1, 20) # new/different coefficient for 2nd x
            e  = sprintf("%s%dx %s %dx = %d", n, a, op1, b2, c)
        } else if (f == 3) {
            # more complex: ax op1 b op2 x = c
            op2 = rop()                    # 2nd random operator
            while (op2 == op1) op2 = rop() # hacky...
            e = sprintf("%s%dx %s %d %s x = %d", n, a, op1, b, op2, c)
        }

        # store equation in array if it doesn't already exist and is valid
        if (!(e in equ)) {
            equ[e] = 1 # mark element as reserved
            u++        # increment u for each unique equation
        }

    } while (u < g)

    printf("SEED: %d\n\n", SEED) # print seed & equations in blocks of 10
    for (j in equ) printf("%0" p "d. %s%s", ++i, j, (i % 10 == 0) ? "\n\n" : "\n")

}

function rop() { return substr("+-*/", rnd(1, 4), 1) }

function rnd(min, max) { return int(rand() * (max - min + 1)) + min }

# 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