Groups | Search | Server Info | Login | Register


Groups > comp.lang.awk > #9880

Re: 100 Random Single Variable Linear Equations

From porkchop@invalid.foo (Mike Sanders)
Newsgroups comp.lang.awk
Subject Re: 100 Random Single Variable Linear Equations
Date 2024-12-06 13:38 +0000
Organization A noiseless patient Spider
Message-ID <viuunp$2c1ev$1@dont-email.me> (permalink)
References <vits2o$240vr$1@dont-email.me> <vitvta$24sm3$1@dont-email.me> <viurhe$2bces$1@dont-email.me>

Show all headers | View raw


Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:

> Hi Mike,

Hey Janis =)
 
> is my guess correct that you want to create linear equation samples
> to be printed (on paper) and solved? Or is it meant as a programming
> course sample? - My suggestions depend on being one or the other...

Just a quick project, nothing serious. Really just thinking aloud
and hoping some of you might offer up your thoughts. Really great
suggestions, I think I'll add some of your ideas to it in the next
few days.
 
> I've learned linear equations to contain an additive term, as in
>   a x + b = c    (i.e.  a*x + b == c  written as Awk expression),
> so I'd have expected the operator to be '+' or '-' (not '*' or '/').
> (Otherwise, with  a*x * b , you could just calculate a*b first and
> a/b respectively, in case of a division a*x / b, before then doing
> the single final lhs/rhs operation. The "both sides" procedures
> you describe in your introductory comment would be unnecessarily
> complicate if you really meant * and / .)

Ahh but I do like the 'spice' of '*' & '/' (see latest iteration below).

But I do wonder about: 5x vs. 5 * x or even (5 * x)... I've read so many
opinions on this matter. If there an offical standard? I dont know.

One older book I have (from 1917!) has 1-2 paragraphs saying 5x without
an intervening * is very bad form & yet, everybody seems to use it, at
least here the USA.

> I wonder about the many temporary variables and technical comments;
> most don't contribute to legibility or clearness and are unnecessary.
> There could be used better naming for the remaining fewer variables.
> It could gain from more structuring, like using a 'random' function
> for integers to make the random expressions simpler.
> Control structure could be simplified, made clearer; do { } while .
> Re-iterating over the stored equations is unnecessary, you can just
> print them.

I know, more clean ups are needed. But the embedded documentation
ought to be included IMO, though, it is terribly messy...

> (I've added code reflecting these suggestions at the end of my post
> in case you'd like to pick an idea or two. I've also changed a few
> more details, just in case you wonder about any differences to the
> original code.)

Yes, certainly, let me study & consider your code & see if I can weave
it into the project. Sounds interesting.
 
> 
>     function rnd (n)  # n -> 1..n
>     {
>         return  int(rand() * n) + 1
>     }
> 
>     BEGIN {
>         srand()
> 
>         while (++serial_number <= 100) {
>             do {
>                 opc = rand() < 0.5 ? "+" : "-"  # choose random operator
>                 equ = sprintf("%d x %c %d = %d", rnd(20), opc, rnd(20),
> rnd(50))
>             } while (equ in equations_store)    # avoid duplicates
> 
>             equations_store [equ]  # memorize generated equation
> 
>             printf("%3d.\t%s\n", serial_number, equ)
>         }
>     }
> 

Yeah I like your thinking, nice & clear. Solid stuff. I'll put some of
this to work during Christmas.

Here's my latest (before I saw your reply). Checkout 'z', sort of like
a gear in a machine yeah? 'n' too. But my imagination is running wild...

BEGIN {
    srand() # seed random number generator

    # keep generating until we have exactly 100 unique equations
    while (u < 100) {
        a = int(rand() * 20) + 1 # random value 1 to 20
        b = int(rand() * 20) + 1 # random value 1 to 20
        c = int(rand() * 50) + 1 # random value 1 to 50
        z = substr("*-/+", (++q % 4) + 1, 1)      # cycle operators
        e = sprintf("%dx %s %d = %d", a, z, b, c) # formatted equation

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

    # print equations
    for (j in equ) printf("%03d. %s\n\n\n\n\n\n\n", ++n, j)
}

# eof

-- 
:wq
Mike Sanders

Back to comp.lang.awk | Previous | NextPrevious in thread | Next 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