user
|  |
| --- |
| This page uses content from **Wikipedia**. The original article was at Lunar arithmetic. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) |

**Lunar arithmetic** is a version of arithmetic in which the addition and multiplication on digits are defined as the max and min operations. Thus, in lunar arithmetic,

:   2
    +
    7
    =
    max
    {
    2
    ,
    7
    }
    =
    7
    {\displaystyle {\displaystyle 2+7=\max\{2,7\}=7}}
     and 

    2
    ×
    7
    =
    min
    {
    2
    ,
    7
    }
    =
    2.
    {\displaystyle {\displaystyle 2\times 7=\min\{2,7\}=2.}}

The lunar arithmetic operations on nonnegative multidigit numbers are performed as in usual arithmetic as illustrated in the following examples. The world of lunar arithmetic is restricted to the set of nonnegative integers.

```
 976 +
 348
 ----
 978 (adding digits column-wise)

```

```
    976 ×
    348
   ----
   876 (multiplying the digits of 976 by 8)
  444  (multiplying the digits of 976 by 4)
 333   (multiplying the digits of 976 by 3)
 ------
 34876 (adding digits column-wise)

```

**A few important properties of lunar arithmetic**

1. The lunar addition and multiplication operations satisfy the Commutative and Associative property laws.
2. The lunar multiplication distributes over the lunar addition.
3. The digit 0 is the additive identity under lunar addition.
4. The digit 9 is the multiplicative identity under lunar multiplication.

Note that lunar arithmetic can be done in any base. This task is restricted to base 10 only.

Task

Implement lunar addition and lunar multiplication routines. Extra kudos for implementing them as operators.

Use those routines (operators) to perform the following:

* Lunar addition and lunar multiplication of the number groups: (976, 348), (23, 321), (232, 35), (123, 32192, 415, 8)
* Using lunar arithmetic, find the first twenty *distinct* even numbers. 

  n
  +
  n
  ≠
  2
  ×
  n
  {\displaystyle {\displaystyle n+n\neq 2\times n}}
   and 

  n
  +
  n
  =
  n
  {\displaystyle {\displaystyle n+n=n}}
  . The even numbers are numbers of the form 

  2
  ×
  n
  {\displaystyle {\displaystyle 2\times n}}
  .
* Using lunar arithmetic, find the first twenty square numbers. 

  n
  ×
  n
  {\displaystyle {\displaystyle n\times n}}
* Using lunar arithmetic, find the first twenty factorial numbers. 

  n
  !
  =
  1
  ×
  2
  ×
  ⋯
  ×
  n
  {\displaystyle {\displaystyle n!=1\times 2\times \cdots \times n}}

Stretch

The lunar squares sequence is not monotonic. Squared values are not strictly increasing as the numbers grow larger.

* Find the first number whose lunar square is smaller than the previous.

See also
:   OEIS A087019 - Lunar squares
:   OEIS A189788 - Base-10 lunar factorials
:   Tropical algebra overloading
assistant
That's not a banana. That's a crescent moon.

```
# The operators
sub infix:<🌙+> ($a, $b = 0) { # addition
    +([Zmax] ('0'x$b.chars~$a).comb,('0'x$a.chars~$b).comb).join;
}

sub infix:<🌙×> ($a, $b = 9) { # multiplication
    [🌙+] $b.flip.comb.kv.map: -> $k,$v {$a.comb.map(* min $v).join ~ '0'x$k}
}

# The task
for (976, 348), (23, 321), (232, 35), (123, 32192, 415, 8) {
    say '     Lunar add: ' ~ (.join: ' 🌙+ ') ~ ' == ' ~ (.join: ' 🌙+ ').EVAL;
    say 'Lunar multiply: ' ~ (.join: ' 🌙× ') ~ ' == ' ~ (.join: ' 🌙× ').EVAL;
    say '';
}

say "First 20 distinct lunar even numbers:\n" ~ (^Inf).map(* 🌙× 2).unique[^20];
say "\nFirst 20 lunar square numbers:\n"      ~ (my @squares = (^Inf).map:{$_ 🌙× $_})[^20];
say "\nFirst 20 lunar factorial numbers:\n"   ~ (1..*).map({[🌙×] 1..$_})[^20];

# Stretch
say "\nFirst number whose lunar square is smaller than the previous: " ~
(1..Inf).first: {@squares[$_ - 1] > @squares[$_]};

```

Output:

```
     Lunar add: 976 🌙+ 348 == 978
Lunar multiply: 976 🌙× 348 == 34876

     Lunar add: 23 🌙+ 321 == 323
Lunar multiply: 23 🌙× 321 == 2321

     Lunar add: 232 🌙+ 35 == 235
Lunar multiply: 232 🌙× 35 == 2332

     Lunar add: 123 🌙+ 32192 🌙+ 415 🌙+ 8 == 32498
Lunar multiply: 123 🌙× 32192 🌙× 415 🌙× 8 == 123233232

First 20 distinct lunar even numbers:
0 1 2 10 11 12 20 21 22 100 101 102 110 111 112 120 121 122 200 201

First 20 lunar square numbers:
0 1 2 3 4 5 6 7 8 9 100 111 112 113 114 115 116 117 118 119

First 20 lunar factorial numbers:
1 1 1 1 1 1 1 1 1 10 110 1110 11110 111110 1111110 11111110 111111110 1111111110 11111111110 111111111100

First number whose lunar square is smaller than the previous: 1020
```