user
An **Undulating number** in some base is a number which has the digit form ABABAB... where A and B are digits in that base.

For the purposes of this task, we shall only consider a number to be undulating if it has at least 3 digits in a given base and A != B.

Examples

The numbers: 101 and 9898 are undulating numbers in base 10.

The numbers: 50 and 2350 are undulating numbers in base 7 because their base 7 representations are: 101 and 6565 respectively.

Task

For base 10, find and show on this page:

1. All three digit undulating numbers.

2. All four digit undulating numbers.

3. All three digit undulating numbers which are primes.

4. The 600th undulating number.

5. How many undulating numbers are less than 2^53 and the largest such number.

Bonus

Do the same for base 7 expressing the results in base 10 apart from 4. and 5. which should be expressed in base 7 also.

Note that undulating numbers with a given number of digits in base 7 may, of course, have less digits when expressed in base 10 and are unlikely to be still undulating. However, 121 (or 232 in base 7) is an exception to this.

References

* Wikipedia article : Undulating number
* OEIS sequence A046075: Non-trivial undulants
assistant
**Translation of**: Wren

```
# 20230602 Raku programming solution

sub undulating ($base, \n) {
   my \limit = 2**(my \mpow = 53) - 1;
   my (\bsquare,@u3,@u4) = $base*$base; 
   for 1..^$base X 0..^$base -> (\a,\b) {
      next if b == a;
      @u3.push(a * bsquare + b * $base + a);
      @u4.push((my \v = a * $base + b) * bsquare + v)
   }
   say "\nAll 3 digit undulating numbers in base $base:";
   .fmt('%3d').say for @u3.rotor: 9;
   say "\nAll 4 digit undulating numbers in base $base:";
   .fmt('%4d').say for @u4.rotor: 9; 
   say "\nAll 3 digit undulating numbers which are primes in base $base:";
   my @primes = @u3.grep: *.is-prime; 
    .fmt('%3d').say for @primes.rotor: 10, :partial;
   my \unc = (my @un = @u3.append: @u4).elems;
   my ($j, $done) = 0, False;
   loop {
      for 0..^unc {
         my  \u = @un[$j * unc + $_] * bsquare + @un[$j * unc + $_] % bsquare;
	 u > limit ?? ( $done = True and last ) !! ( @un.push: u );
      }
      $done ?? ( last ) !! $j++ 
   }
   say "\nThe {n} undulating number in $base $base is: @un[n-1]";
   say "or expressed in base $base : {@un[n-1].base($base)}" unless $base == 10;
   say "\nTotal number of undulating numbers in base $base < 2**{mpow} = {+@un}";
   say "of which the largest is: ", @un[*-1];
   say "or expressed in base $base : {@un[*-1].base($base)}" unless $base == 10;
}

undulating $_, 600 for 10, 7;

```

You may Try it online!