# A Small Unary Puzzle / A Small Unnary Code Exercise

Decimal is base-10. That means that there's 10 different symbols you can put together to create a number: 1234567890.
Binary is base-2, with just two different symbols: 10.
What would base-1 be? It's Unary: 1.

Let's take decimal 4.
In binary that's 100.
Unary is even easier to learn than binary. It's 1111.

## Abusing the Java String Library to do Math

Let's represent a unary integer as a String: `"11111"` (five)
How do we convert a unary integer to a (binary / decimal) int?

(reminder: java has a REPL `/usr/lib/jvm/java-*-openjdk/bin/jshell`)

```"11111".length() == 5;
```

(hover over or highlight the code to reveal spoilers)

`"1111" + "11"` should equal `"111111"` (six).

```"1111" + "11" == "111111";
// string concatination
// it's that simple
```

Subtraction is a little harder.
`"1111" - "111" == "1"`

```"1111".replaceFirst("111", "") == "1";
```

Let's implement multiplication and division at the same time.
If you aren't using the spoilers at this point,
`"1111" * "11" == "11111111"` (eight) and `"11111111" / "11" == "1111"`

```// multiply
"1111".replace("1", "11") == "11111111";
// divide
"11111111".replace("11", "1") == "1111";
```

If you looked at the above spoiler, there is a bug with division.
What is it?

This implementation has a bug with division.
If the number isn't cleanly divisible with the divisor, the remainer will be added to the result.
It's simple to fix this by writing a for/while loop, but is there a function in the standard library that fixes this?

If you did multiplication and division the same way I did, fractional multiplication is the next step.

```"111".replace("111", "11") == "11"
"111111".replace("111", "11") == "1111"
// multiplication by two thirds

// division is actually multiplication by a fraction
```

## Further Abuse

What would a square function look like?
What would a exp function look like?
There is a way to represent zero. Would the functions you wrote work with this representation?
Is there a way to represent negative numbers while keeping all operations one-liners?