Consider the process of:
Taking a non-negative integer N.
e.g.27
.Spliting it into integers
N - floor(N/2)
andfloor(N/2)
(a 'bigger' and 'smaller' half) and writing them in that order.
e.g.27
becomes14 13
.Removing the space to join the integers into a new, much larger integer.
e.g.14 13
becomes1413
.Repeating steps 2 and 3 some desired number of times.
e.g.1413
→707 706
→707706
→353853 353853
→353853353853
→ ...
This challenge is about doing exactly this, but not always in base 10.
Challenge
Write a program that takes in three numbers, B, N, and S:
B is an integer from 2 to 10 that is the base of N (binary to decimal).
N is the non-negative integer to apply the splitting-rejoining process to. To make user input easier, it is given as a string in base B, not an integer.
S is a non-negative integer that is the number of times to repeat the splitting-rejoining process.
The output of the program is the string representation of N in base B after S split-join procedures.
When S is 0
, no splits are done, so the output is always N.
When N is 0
, all splits have the form 0 0
and reduce to 0
again, so the output is always 0
.
Examples
B = 10, N = 27, S = 1
→1413
B = 10, N = 27, S = 2
→707706
B = 9, N = 27, S = 1
→1413
B = 9, N = 27, S = 2
→652651
B = anything, N = anything, S = 0
→N
B = anything, N = 0, S = anything
→0
Table for all B with N = 1
for S = 0
to 7
:
B S=0 S=1 S=2 S=3 S=4 S=5 S=6 S=7
2 1 10 11 101 1110 111111 10000011111 10000100001000001111
3 1 10 21 1110 202201 101101101100 1201201201212012012011 212100212102121002121212100212102121002120
4 1 10 22 1111 223222 111311111311 2232222232322322222322 11131111131311311111311113111113131131111131
5 1 10 32 1413 432431 213441213440 104220331443104220331442 2433241322130211014044424332413221302110140443
6 1 10 33 1514 535535 245550245545 122553122553122553122552 4125434125434125434125441254341254341254341254
7 1 10 43 2221 11111110 40404044040403 2020202202020220202022020201 10101011010101101010110101011010101101010110101011010100
8 1 10 44 2222 11111111 44444454444444 2222222622222222222226222222 11111113111111111111131111111111111311111111111113111111
9 1 10 54 2726 13581357 62851746285173 3142536758708231425367587081 15212633743485606571782880411521263374348560657178288040
10 1 10 55 2827 14141413 70707077070706 3535353853535335353538535353 17676769267676676767692676771767676926767667676769267676
Table for all B with random N for S = 0
to 3
:
B S=0 S=1 S=2 S=3
2 11011 11101101 11101111110110 11101111110111110111111011
3 2210 11021101 20102012010200 1001212100121210012121001211
4 1113 230223 112112112111 2302302302323023023022
5 101 2323 11341134 31430423143042
6 120 4040 20202020 1010101010101010
7 134 5252 24612461 1230456412304564
8 22 1111 445444 222622222622
9 4 22 1111 505505
10 92 4646 23232323 1161616211616161
Details
- Take input via stdin or the command line. Output to stdout.
- Instead of a program, you may write a function that takes B, N, and S and prints the result normally or returns it (as a string).
- B, N, and S may be taken in any order.
- All inputs that produce outputs whose decimal values are below 232 should work.
- N is represented in the usual way. i.e. most significant digit first and no leading zeros except in zero itself which is written
0
. (Outputting00
instead of0
is invalid.) - The shortest code in bytes wins.
Iff you enjoy my challenges, consider giving Block Building Bot Flocks! some love :)