I was inspired by
this Reddit thread to see if I could supplement the traditional regex primality test with a regex Euclidean algorithm. Here's the shortest one that occurred to me; it takes the two numbers (assumed to be positive integers) whose GCD we want to compute in
$a and
$b:
(1 x $a) =~ /(?:$1)*(.*)/ and ($a, $b) = ($b, length $1) while $b
At the end,
$a is the GCD (and
$b is
0). Life's even better if we're allowed to start with
$a and
$b strings of
1s of the desired length, and leave our answer in
$a in the same form:
($a, $b) = ($b, $a =~ /(?:$1)*(.*)/) while $b