13
I once saw on the xkcd fora a format for expressing numbers in an odd way. In this "factor tree" format:
- The empty string is 1.
- Concatenation represents multiplication.
- A number n enclosed in parentheses (or any paired characters) represents the nth prime number, with 2 being the first prime number.
- Note that this is done recursively: the nth prime is the factor tree for n in parentheses.
- The factors of a number should be ordered from smallest to largest.
For example, here are the factor trees for 2 through 10:
()
(())
()()
((()))
()(())
(()())
()()()
(())(())
()((()))
Your task is to take in a positive integer, and output the factor tree for it.
Test Cases
In addition to the 9 above…
100 => ()()((()))((()))
101 => (()(()(())))
1001 => (()())(((())))(()(()))
5381 => (((((((())))))))
32767 => (()())((((()))))(()()(())(()))
32768 => ()()()()()()()()()()()()()()()
Rules
- Characters other than
()[]{}<>are allowed in the output, but ignored. - You should be able to handle any input from 2 to 215 inclusive.
- The winner is the shortest answer in bytes.
Similar to Brain-Flak:
()nilad is 2 instead of 1,(...)monad is next prime instead of push, concatenation is multiply instead of add. – user202729 – 2017-12-08T14:39:21.2071@user202729 you could probably get some irony points by writing a Brain-Flak answer—good luck though. – Nissa – 2017-12-08T16:01:39.250
Definitely worth considering. – user202729 – 2017-12-08T16:05:26.043
1
I would argue that this is a duplicate of Encode an integer. Here is my solution to that, with the front part removed and
– H.PWiz – 2017-12-08T17:10:58.633[1,0]replaced by"()". Also the Jelly solution there is remarkably similar to the one hereWhy didn't that come up in the sandbox? – Nissa – 2017-12-08T18:06:32.737
@StephenLeppik Personally, I saw it in the sandbox, but only noticed it was a duplicate after seeing solutions. Sorry :/ – H.PWiz – 2017-12-08T18:07:41.837
@H.PWiz it's okay, at least I got 60 rep and a bunch of flags out of it. Not to mention rolling out my new tag without it having to go through Suggested Edits. – Nissa – 2017-12-08T18:09:12.623
@H.PWiz is there an existing challenge for decoding these? – Nissa – 2017-12-08T18:11:53.550
I've been trying to find a method for determining if an integer is valid and decode-able. That might make a good challenge in and of itself. Not only do the
0s have to be balanced against the1s so that the parentheses are balanced, but the primes they result in have to be sorted correctly. – Sherlock9 – 2017-12-09T11:43:57.083