33
6
Given positive integer n > 2
. We convert it to an array as follows:
- If it is equal to
2
return an empty array - Otherwise create array of all
n
's prime factors sorted ascending, then each element replace with its index in prime numbers sequence and finally convert each element to array
For example, lets convert number 46
to array. Firstly, convert it to array of its prime factors:
[2, 23]
Number 23
is 9
th prime, so replace 2
with empty array and 23
with [9]
. Array now becomes:
[[], [9]]
Prime factors of 9
are 3
and 3
, so:
[[], [3, 3]]
Do the same for both 3
:
[[], [[2], [2]]]
And finally:
[[], [[[]], [[]]]]
Now, to encode it, we simply replace each open bracket with 1
and each closing bracket with 0
, then remove all ending zeros and drop one 1
from the end. This is our binary number. Using the above example:
[ ] [ [ [ ] ] [ [ ] ] ]
| | | | | | | | | | | |
| | | | | | | | | | | |
V V V V V V V V V V V V
1 0 1 1 1 0 0 1 1 0 0 0
Now simply drop the last three zeros and the last 1
. The number becomes 10111001
which is 185
in decimal. That is the expected output. Notice that in array to binary conversion brackets of the main array are not included.
Input
Positive integer n
greater than 2
.
Output
Encoded integer n
.
Rules and IO format
- Standard rules apply.
- Input can be string or number (but in case of string it must be in base 10).
- Output can be string or number (but in case of string it must be in base 10).
- This is code-golf, the shortest answer in bytes wins!
Test cases
More test cases upon request.
3 ---> 1
4 ---> 2
5 ---> 3
6 ---> 5
7 ---> 6
8 ---> 10
9 ---> 25
10 ---> 11
10000 ---> 179189987
10001 ---> 944359
10002 ---> 183722
10003 ---> 216499
10004 ---> 2863321
10005 ---> 27030299
10006 ---> 93754
10007 ---> 223005
10008 ---> 1402478
You should remove the test case with
2
since the submissions are not required to handle it. – Mr. Xcoder – 2017-08-14T18:27:26.487Or just say
n >= 3
if2
is not in the input domain – Stephen – 2017-08-14T18:29:01.407Ok, fixed. Initially, I really didn't see why is 2 a problem, but in the comments at sandbox post people was complaining about it, so I removed it now. – None – 2017-08-14T18:30:03.497
4Rip languages that do not have prime built-ins. – Mr. Xcoder – 2017-08-14T18:31:19.760
@Mr.Xcoder. The lack of prime built-ins should not be a hindrance of posting an answer in some language. It is always interesting to see how different langauges deal with some problem. – None – 2017-08-14T18:35:06.730
Now simply drop the last three zeros and the last 1 - Does that apply to every number (maybe I am just dumb and didn't get the point of the challenge)? – Mr. Xcoder – 2017-08-14T19:02:39.763
@Mr.Xcoder. It depends on how many zeros are at the end. If there are 5 zeros, you will drop all 5 (and of course the last 1 at the end). So basically all trailling zeros and one 1. – None – 2017-08-14T19:04:26.483
"Firstly, convert it to array of its prime factors: [2, 23]" - Are other orders like [23, 2] also permitted? – Paul – 2017-08-14T19:21:38.487
3@Paul. "[...] create array of all n's prime factors sorted ascending" – None – 2017-08-14T19:23:30.603
Out of curiosity, where did you come up with this question? Is there any motive behind it? – Quelklef – 2017-08-15T04:13:41.507
1
@Quelklef. I was working on implementing ATP (just for fun, nothing serious) and I tried to somehow represent every number using nested arrays. So, this encoding is the first idea I came up with.
– None – 2017-08-15T05:06:45.513How does 3 become 2 in "do same for 3"? – Stan Strum – 2017-09-05T06:29:30.117
@StanStrum. Three is the second prime number. – None – 2017-09-05T06:42:04.687
@ThePirateBay Thanks – Stan Strum – 2017-09-05T14:05:33.243
@ThePirateBay You might want to update the accepted answer. – Laikoni – 2017-12-09T10:38:14.977
@Laikoni. Done. – None – 2017-12-11T10:14:39.917
Probably ought to be "Encode a Natural Number" since this method doesn't allow you to encode integers less than 1. – Post Rock Garf Hunter – 2017-12-23T17:19:06.883
1@WheatWizard. I don't mean the precise mathematical sense of the word integer. I am going to leave it. :-) – None – 2017-12-26T15:12:15.940