Wolfram Cloud – functions, do iteration, if and lists

Iwas reading about group theory recently, for basic cryptography, and wanted a function for computing the set \mathbb{Z}^*_n   , i.e. the set of integers with greatest common divisor (gcd) with n equal to 1 , formally \mathbb{Z}^*_n = \{a \in \mathbb{Z}_n | gcd(a,n)=1\}  .

And I wanted to do it “quickly”, in a math engine, so what better place than on Wolfram’s Development Platform!

It’s not rocket science how to compute the set, but I thought this’d serve as a nice reference, with examples, to basic operations in the Wolfram Language on the Wolfram Cloud.

I needed to be able to check the gcd between numbers, this is built in as GCD, example

Checking from 1..n-1 is tedious however, where Do comes in handy. Do[ expr, { var, limit } ], loops var from 1 to limit (inclusive), example

Time for Print[expr]! Example

But this is badly formatted, and also it includes the check of gcd(6,6), and it has a hardcoded variable. First, into a function, example

Calculates the answer to life, the universe and everything, plus x, i.e. 2. Our function looks like, (variable factored out, removed gcd(n,n) check with g-1)

Still badly formatted, and actually it doesn’t output the set, just a list of tests where it prints the restult of GCD; enter If. If[ expr, t, f], where t is evaluated if expr is true, and f is an optional expression, evaluated if expr evaluates to false. (If no f is provided, and the If is used in something bigger, say a list, it’ll return null). Start small:

Redefine function with the If-condition

I love it, I can compute the set! Still it isn’t very nicely formatted; I want it in a list. So I need a list, and the AppendTo function,

Putting it all together in a ()-clause, extracting the Print I get the final code:

This could probably be optimized further by adding 1 to the initial set, and skip the first check, like below (Do with a { var, start, end }-constraint),

But it’s not a race!!.. I wonder if this can be done with a map somehow, in a sane way. In my head it doesn’t seem faster(, or less involved!)

Also, I totally know that I’m cheating by not calculating gcd(n,n), but I made the clever observation that it’ll never be 1 unless n is 1 (I’ll leave it up to the reader to prove this.) The function doesn’t work for generating \mathbb{Z}^*_1  either; suppose we’ll have to wonder for eternity what the result of this is.

Edit: lol wait, it actually does work in the optimized version, since it adds 1 to the set initially and just skips the Do-loop. I totally knew this; which is why I made the optimized version, I promise.

Leave a Reply

Your email address will not be published. Required fields are marked *