Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: porkchop@invalid.foo (Mike Sanders) Newsgroups: comp.lang.awk Subject: 100 Random Single Variable Linear Equations Date: Fri, 6 Dec 2024 03:46:32 -0000 (UTC) Organization: A noiseless patient Spider Lines: 87 Sender: Mike Sanders Message-ID: Injection-Date: Fri, 06 Dec 2024 04:46:33 +0100 (CET) Injection-Info: dont-email.me; posting-host="b091e410931247565241b7435975ad23"; logging-data="2229243"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19XiAW/L8XXvwch1mHafDoR" User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (NetBSD/9.3 (amd64)) Cancel-Lock: sha1:1BOtNKwlL24PrYpD2gnxtXf8MME= Bytes: 3325 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