# The Double-Castle Numbers™

### Introduction

A double-castle number™ is a positive integer number that has a pattern of $$\underbrace{a\cdots a}_{m\text{ }a\text{s}}b\underbrace{a\cdots a}_{m\text{ }a\text{s}}b\underbrace{a\cdots a}_{m\text{ }a\text{s}}\underbrace{a\cdots a}_{n\text{ }a\text{s}}b\underbrace{a\cdots a}_{n\text{ }a\text{s}}b\underbrace{a\cdots a}_{n\text{ }a\text{s}}$$ Where $$\m>0\$$, $$\n>0\$$ and $$\a-b=1\$$ are all non-negative integers, when represented in an integer base $$\B\$$ where $$\B\ge2\$$. It is so named because a bar chart representing the base-$$\B\$$ digits of such a number resembles two castles of the same height place- side by side.

For example, $$\7029\$$* is a double-castle number because when represented in base 2 it becomes $$\1101101110101_2\$$, which can be split into $$\11011011\$$ and $$\10101\$$.

This is the case when $$\m=2\$$, $$\n=1\$$, $$\a=1\$$, $$\b=0\$$ and $$\B=2\$$.

$$\305421994212\$$ is also a double-castle number because when represented in base 8 it becomes $$\4343444344344_8\$$, which can be split into $$\43434\$$ and $$\44344344\$$.

This is the case when $$\m=1\$$, $$\n=2\$$, $$\a=4\$$, $$\b=3\$$ and $$\B=8\$$.

For $$\a>=10\$$, $$\a\$$ should be treated as a single base-$$\B\$$ "digit" with the value of $$\a\$$ in base-10. $$\206247763570426655730674346\$$ is a double-castle number in base-16, whose representation in base-16 is $$\\text{AA9AA9AAAAAA9AAAA9AAAA}_{16}\$$. Here, $$\a=10\$$ but is treated as a single digit $$\(10)_{16}=\text{A}_{16}\$$.

This is the case when $$\m=2\$$, $$\n=4\$$, $$\a=10\$$, $$\b=9\$$ and $$\B=16\$$.

### Challenge

Write a program or function that, given integers $$\m>0\$$, $$\n>0\$$, $$\1\le a and $$\B\ge2\$$, calculate the corresponding double-castle number™ and output it in base-10.

### Test Cases

The input below are in base-10, but in the case say when $$\a=11\$$ and $$\B=12\$$ the input should be understood as $$\B_{12}\$$.

m, n, a,  B  => Output
1, 1, 1,  2  => 693
2, 1, 1,  2  => 7029
1, 2, 3,  4  => 62651375
1, 2, 4,  8  => 305421994212
1, 4, 7,  10 => 7676777776777767777
2, 4, 8,  16 => 164983095594247313234036872
2, 4, 10, 16 => 206247763570426655730674346


### Winning Condition

This is a code-golf challenge, the shortest submission in each language wins. No standard loopholes allowed.

*7029 comes from my ID minus it written reversed.

• How are numbers > 10 in the input to be represented. Say, for base 16, will it be "10, 11, 12, 16", or "A, B, C, 16"? Are we free to pick? Jun 2, 2020 at 11:25
• Could you add one or multiple test case where $a\geq10$? Both my answers apparently failed those cases.. (i.e. 2, 4, 10, 16 => 206247763570426655730674346). Also, maybe some test cases where n and m are larger than 10. And change the term 'digits' to 'numbers' in the sentence "It is so named because a bar chart representing the digits of such a number resembles two castles of the same height placed side by side." please, since it's incorrect and actually made me thing we only had to work with digits. Jun 2, 2020 at 14:59
• @KevinCruijssen I have clarified that the inputs are shown in base-10 but should be treated as the digit with the same value in base-B. Jun 2, 2020 at 16:53
• @Abigail Oops I may have misunderstood your question, I mean you may receive like 10 as an input in base-16 but the number should be treated as $A_{16}$ instead of $10_{16}$ when calculating. Jun 2, 2020 at 16:59
• @KevinCruijssen To further clarify the case I am going to add an image illustrating what I mean “the base-B digits”. Jun 2, 2020 at 17:05

# Python 2, 83 81 79 72 bytes

-9 bytes thanks to @ovs!
-2 bytes thanks to @dingledooper!

m,n,a,B=input()
r=0
for s in n,m+n,m,m:r+=B**n;n-=~s
print~-B**n/~-B*a-r


Try it online!

Calculate aaaa...aaa in base B, then subtract 1 at the appropriate digits.

• 79 bytes Jun 2, 2020 at 21:32
• @dingledooper Thanks, I didn't know ** takes precedence over ~-. Jun 2, 2020 at 21:37
• 72 bytes as a full program.
– ovs
Jun 3, 2020 at 5:57
• @ovs Updating n is really clever, thanks! Jun 3, 2020 at 8:57
• @ovs Why did you delete your answer? I think you can make it work for arbitrary a at a cost of only 2 bytes (build up the first number using '1' instead of a and then multiply by a).
– Neil
Jun 4, 2020 at 0:07

# C (gcc) -lm, 83 $$\\cdots\$$ 81 80 bytes

Saved 1 2 bytes thanks to ceilingcat!!!

#define P-pow(B,n
f(m,n,a,B){n=-(1 P*3+3*m+4))/~-B*a P)P-~n)P*3+2+m)P*3+3+2*m);}


Try it online!

Calculates the wall to full height first and then crenellation is performed through subtraction.

# 05AB1E, 10 12 bytes

иεÐ)²<.ý}˜sβ


+2 bytes as bugfix for $$\a\geq10\$$ (initial 10-byter answer: ×εÐ¬<ý}Jsö - Try it online or verify all test cases).

Explanation:

и             # Repeat the second (implicit) input the first (implicit) inputs amount of
# times as list
#  i.e. [1,2] and 4 → [[4],[4,4]]
ε            # Map each to:
Ð           #  Triplicate the value
#   i.e. STACK: [4,4],[4,4],[4,4]
)          #  Wrap them into a list
#   i.e. STACK: [[4,4],[4,4],[4,4]]
²<        #  Push the second input - 1
#   i.e. STACK: [[4,4],[4,4],[4,4]],3
.ý      #  Intersperse this list with this value
#   i.e. STACK: [[4,4],3,[4,4],3,[4,4]]
}˜    # After the map: flatten the list
#  i.e. [[[4],3,[4],3,[4]],[[4,4],3,[4,4],3,[4,4]]]
#   → [4,3,4,3,4,4,4,3,4,4,3,4,4]
s   # Swap to get the third (implicit) input
β  # Base-convert the list we created to an integer using the input as base
#  i.e. [4,3,4,3,4,4,4,3,4,4,3,4,4] and 8 → 305421994212
# (after which the result is output implicitly)


# K (ngn/k), 26 bytes

{z/,/(2+3*x)#'(x#'y),'y-1}


Explanation

{z/,/(2+3*x)#'(x#'y),'y-1} / using x=2 1; y=1; z=2 as an example
(x#'y)       / (m;n) copies of a;     ex: (1 1;1)
,'y-1  / append b=a-1 to each   ex: (1 1 0;1 0)
(2+3*x)               / length of each sublist ex: (8;5)
#'             / copy to each length    ex: (1 1 0 1 1 0 1 1; 1 0 1 0 1)
,/                      / join                   ex: (1 1 0 1 1 0 1 1 1 0 1 0 1)
z/                        / convert from base B    ex: 7029


Try it online!

# perl -Mbigint -alp, 87 bytes

($m,$n,$a,$B)=@F;$o=$o*$B+$_ for(($a)x$m,$a-1)x2,($a)x$m,(($a)x$n,$a-1)x2,($a)x$n;$_=$o


Try it online!

We take the input, turn it into a corresponding list of digits, then convert it to base 10. Input is taken as space separated numbers on STDIN.

The TIO code does have some header code; this is just there so it works with multiple inputs -- without the header, it will only do the first line correctly. (those header bytes aren't counted).

# Jelly,  12  11 bytes

‘3×þṬạṖ€Ẏḅ⁵


A full program accepting three arguments: [m,n] a B which prints the result.

Try it online!

### How?

‘3×þṬạṖ€Ẏḅ⁵ - Main Link: [m,n]; a
‘           - increment -> [m+1, n+1]
3          - three
þ        -   outer product ([1, 2, 3] by [m+1, n+1]) with:
×         -     multiplication -> indexes of low points (with an extra low on the
right for each of castle)
Ṭ       - un-truth (vectorises) -> two lists of 1s and zeros (1s at low points)
ạ      - absolute difference (with a) -> convert zeros to a and 1s to a-1=b
Ṗ€    - pop from each -> remove the extra low points)
Ẏ   - tighten -> from a list of two lists to a flat list
⁵ - programs 5th argument = B
ḅ  - convert dfrom base
- implicit print


# Charcoal, 27 bytes

Ｎθ≔⭆Ｅ²Ｎ⪫Ｅ³×0ι1δＩ⁻×Ｎ⍘⭆δ1θ⍘δθ


Try it online! Link is to verbose version of code. Takes input in the order B, m, n, a. Explanation:

Ｎθ


Input B.

≔⭆Ｅ²Ｎ⪫Ｅ³×0ι1δ


Input m and n. For each value, create a string of that many 0s. Join 3 of those strings with 1s. Join the final strings together.

Ｉ⁻×Ｎ⍘⭆δ1θ⍘δθ


Replace all of the characters in the string with 1s, convert from base B, multiply by a, then subtract the string converted from base B, and output in decimal.

Note that although Charcoal's string based conversion accepts numeric bases higher than 62 this only succeeds when (as here) all the digit values are less than 62.

Slightly shorter solutions are possible that place upper limits on the supported value of B:

ＮθＩ↨Ｅ⭆Ｅ²Ｎ⪫Ｅ³×ι℅θ℅⊖θ℅ιＮ


Try it online! Link is to verbose version of code. Takes input in the order a, m, n, B. Works for B up to 65536. Works by creating a string of characters whose ordinal is a or b, then converting back into ordinals and decoding using base B. 22 bytes.

ＮθＩ⍘⭆Ｅ²Ｎ⪫Ｅ³×ι⍘θφ⍘⊖θφＮ


Try it online! Link is to verbose version of code. Takes input in the order a, m, n, B. Works for B up to 62. Works by creating a string of characters whose base B code is a or b, then decoding using base B. 21 bytes.

Ｉ⍘⭆Ｅ²Ｎ⪫Ｅ³×ιεＩ⊖εＮ


Try it online! Link is to verbose version of code. Takes input in the order a, m, n, B. Works for B up to 10. Works by creating a string of characters whose value is a or b, then decoding using base B. 16 bytes.

# APL (Dyalog Unicode), 16 bytes

⎕⊥⎕-~1\⍨∊5⍴¨⎕,¨0


Try it online!

A full program that takes m n, a, B on three lines of stdin.

### How it works

⎕⊥⎕-~1\⍨∊5⍴¨⎕,¨0
⎕     ⍝ Take (m n) from stdin
,¨0  ⍝ Append 0 to each; (m 0)(n 0)
5⍴¨      ⍝ Repeat each to length 5; (m 0 m 0 m)(n 0 n 0 n)
∊  ⍝ Flatten; (m 0 m 0 m n 0 n 0 n)
1\⍨   ⍝ Expand 1 by above: m/n become m/n copies of 1, 0 becomes single 0
~      ⍝ Boolean negate the above
⎕-       ⍝ Subtract each from a (taken from stdin)
⎕⊥         ⍝ Convert from base B (taken from stdin) to integer

• Does J have any analog of APL's ⎕? It seems to help a lot in challenges with many args. Jun 3, 2020 at 3:19
• @Jonah IIRC anything close to stdin costs at least 5 bytes, so I'd just use explicit adverb 1 :'body' / conjunction 2 :'body'. Jun 3, 2020 at 3:25

# J, 35 bytes

{:@[#.((#10$5$0 1-~{.)~[:,5\$"1,.&1)


Try it online!

# Husk, 12 11 bytes

BṁȯJ←¹R3R


Try it online! Takes arguments in the order a [m,n] B.

# Explanation

BṁȯJ←¹R3R  Implicit arguments a, [m,n], B.
Say a=3, m=1, n=2, B=4.
ṁȯ         Map over [m,n] and concatenate:
Use n=2 as example.
R    Repeat a that many times: [3,3]
R3      Repeat three times: [[3,3],[3,3],[3,3]]
←¹        a decremented: 2
J          Join by it: [3,3,2,3,3,2,3,3]
Result: [3,2,3,2,3,3,3,2,3,3,2,3,3]
B           Interpret in base B: 62651375


# JavaScript, 75 bytes

There may be rounding errors due to JavaScript's integer limit.

(m,n,a,B)=>B**(3*(m+n)+4)/~-B*a-B**(n-~n)-B**(m+3*n+2)-B**(2*m-3*~n)-B**n-1


Try it online!