Background
Many esoteric programming languages don't have numbers built in at literals, so you have to calculate them at runtime; and in many of these cases, the number representation can be quite interesting. We've already had a challenge about representing numbers for Underload. This challenge is about representing numbers in Modular SNUSP. (Note that you don't need to learn SNUSP in order to complete this challenge – all the information you need is in the specification – but you might find the background interesting.)
The task
For the purpose of this challenge, a Modular SNUSP number is a string formed out of the characters @
, +
, and =
, except that the last character is a #
, and that the penultimate character must be +
or =
(it cannot be @
). For example, valid numbers include @+#
, ==#
, and @@+@=#
; examples of invalid numbers include +=
, @@#
, and +?+#
.
The value of a Modular SNUSP number is calculated recursively as follows:
#
has a value of 0 (this is the base case).- If the number has the form
=x
, for any stringx
, its value is equal to the value ofx
. - If the number has the form
+x
, for any stringx
, its value is equal to the value ofx
, plus 1. - If the number has the form
@cx
, for any single characterc
and any stringx
, its value is equal to the value ofx
, plus the value ofcx
.
For this challenge, you must write a program that takes a nonnegative integer as input, and outputs a string that's the shortest possible Modular SNUSP number that has a value equal to the input.
Clarifications
- It's entirely possible that there will be more than one string with the same value, and in particular, for some integers there will be a tie for the shortest Modular SNUSP number with that value. In such a case, you may output any of the numbers involved wit the tie.
- There's no restriction on the algorithm you use to find the number; for example, brute-forcing strings and evaluating them is a legal tactic, but so is doing something cleverer to reduce the search space.
- As usual on PPCG, your submission may be either a full program or a function (pick whichever is more concise in your language).
- This isn't a problem about handling input and output formats, so you can use any reasonable means to input a nonnegative integer and output a string. There's a full guide on meta, but the most commonly used legal methods include function arguments/returns, command line arguments, and standard input/standard output.
Test cases
Here are the shortest representations of the first few numbers:
- 0:
#
- 1:
+#
- 2:
++#
- 3:
+++#
or@++#
- 4:
++++#
or+@++#
or@=++#
- 5:
@+++#
or@@++#
- 6:
+@+++#
or+@@++#
or@=+++#
or@=@++#
or@@=++#
- 7:
@++++#
or@+@++#
- 8:
@@+++#
or@@@++#
- 9:
+@@+++#
or+@@@++#
or@+++++#
or@++@++#
or@+@=++#
or@@=+++#
or@@=@++#
- 10:
@=@+++#
or@=@@++#
or@@@=++#
(this is a fairly important test case to check, as all the possible answers include=
) - 11:
@+@+++#
or@+@@++#
or@@++++#
or@@+@++#
- 12:
+@+@+++#
or+@+@@++#
or+@@++++#
or+@@+@++#
or@=+@+++#
or@=+@@++#
or@=@=+++#
or@=@=@++#
or@=@@=++#
or@@=++++#
or@@=+@++#
or@@=@=++#
- 13:
@@@+++#
or@@@@++#
- 14:
+@@@+++#
or+@@@@++#
or@=@++++#
or@=@+@++#
or@@+++++#
or@@++@++#
or@@+@=++#
- 15:
@+@++++#
or@+@+@++#
or@@=@+++#
or@@=@@++#
or@@@=+++#
or@@@=@++#
As a larger test case, the output from input 40 should be @@@=@@+++#
, @@@=@@@++#
, @@@@=@+++#
, or @@@@=@@++#
.
Victory condition
As a code-golf challenge, the winner is the shortest entry, measured in bytes.
=
will optimally only occur as@=
, right? \$\endgroup\$