Sujet : 100 Random Single Variable Linear Equations
De : porkchop (at) *nospam* invalid.foo (Mike Sanders)
Groupes : comp.lang.awkDate : 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/AlgebraBEGIN {
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
-- :wqMike Sanders