# Number of ways to partition n into n = K + ꓘ

In this challenge you are asked to find in how many ways we can express a positive integer n as the sum of two positive integers k and the reverse of k.

Here is an example using n = 1069 :
188 + 881 = 1069
287 + 782 = 1069
386 + 683 = 1069
485 + 584 = 1069
980 + 89 = 1069
So, the answer here would be 5.

rules
As you can see we count every pair once: {188,881} is the same as {881,188}.
If a number cannot be expressed as such, your program should return 0.
Your program should take as input a positive integer and return an integer.

test cases

1 -> 0
2 -> 1 (this is 1 + 1 = 2)
22 -> 2
101 -> 1 ( this is 100 + 001 = 100 + 1 = 101 )
132 -> 4
8547 -> 20
49886 -> 0
955548 -> 113
1100000 -> 450


This is CodeGolf! The shortest code in bytes, wins!

• Is there case when two K + ꓘ both result in n?
– l4m2
Jan 19 at 20:26
• Yes, first one is 22 = 11 + 11 = 20 + 2 Jan 19 at 20:29
• Is 20=010+010??
– l4m2
Jan 19 at 20:41
• 010 is 010 reversed. You should specify that the greater integer (or both if they are the same) must have no leading zeros.
– Tbw
Jan 19 at 20:47
• 010 is not an integer. 300 is an integer and the reverse is 3. Should I also define integers?? Jan 19 at 20:49

# Jelly, 9 bytes

rHDṚḌ+Ʋ€ċ


A monadic Link that accepts a positive integer and yields the number of valid pairs distinct up to reversing

Try it online!

### How?

rHDṚḌ+Ʋ€ċ - Link: positive integer, N
H        - halve {N} -> N/2
r         - {N} inclusive range {that} -> [N, N-1, ..., floor(N/2)]
€  - for each such "potential larger of the pair"*, X:
D       -     convert {X} to decimal digits
Ṛ      -     reverse
Ḍ     -     convert from decimal digits
ċ - count occurrences of {N}


* Note: while odd $$\N\$$ do include one "potential larger of the pair" that would not be the larger of the pair ($$\X=\lfloor \frac{N}{2} \rfloor\$$), that value can never work. This is because $$\N=\lfloor \frac{N}{2} \rfloor + \lceil \frac{N}{2} \rceil\$$ and so $$\\text{reverse}(X) - X = 1\$$ but this isn't a multiple of $$\9\$$. (Thanks to Neil for simplifying my explanation!)

• When N is odd the case ⌊N/2⌋ + ⌈N/2⌉ is impossible since the difference between a number and its reverse is always a multiple of 9.
– Neil
Jan 20 at 1:42
• Oh, very succinct @Neil. Jan 20 at 2:01

# R, 70 bytes

\(x)sum(sapply(x:(x/2),\(z)z%/%(b=10^(0:log10(z)))%%10%*%rev(b)+z)==x)


Attempt This Online!

More or less an R translation of @JonathanAllan’s Jelly answer, so be sure to upvote that one too!

# Vyxal 3s, 7 bytes

½⌊z-ᵛṁ=


Try it Online!

-2 bytes thanks to @pacman256

½⌊z-ᵛṁ=­⁡​‎‎⁪⁡⁪⁠⁪⁤⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁢⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁢⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁢⁣⁪‏‏​⁡⁠⁡‌⁢⁡​‎‏​⁢⁠⁡‌­
-     ## ‎⁡n -
½⌊z      ## ‎⁢[0 .. floor(N / 2)]
ᵛṁ   ## ‎⁣for every element n, n + mirror(n)
=  ## ‎⁤equals to itself? (vectorized)
## ‎⁢⁡The flag -s counts every Truthy values.
💎


Created with the help of Luminespire.

• 7 Jan 20 at 7:53
• @pacman256 done. Jan 20 at 14:57

# Pyth, 9 bytes

/ec2m+s_


Try it online!

### Explanation

                 # implicitly assign Q = eval(input())
m      Q     # map lambda d over range(Q)
_d       #   reverse string(d)
s          #   convert back to integer
c2             # chop list into 2 parts of equal length
e               # take the second part
/           Q    # count instances of Q


# Charcoal, 16 15 bytes

ＮθＩΣＥ⮌…·⊘θθ⁼κ⮌ι


Try it online! Link is to verbose version of code. Explanation:

Ｎθ              First input as a number
…·        Inclusive range from
θ      Input number
⊘       Halved
θ     To input number
⮌          Reversed
Ｅ           Map over values
κ   Current index
⁼    Equals
ι  Current value
⮌   Reversed
Σ            Take the sum
Ｉ             Cast to string
Implicitly print


Reversing the range means the value plus index always equals the input number, so we just need to check that the latter is the reverse of the former.

# Python 3, 56 bytes

lambda x:sum(i>=x-i==int(str(i)[::-1])for i in range(x))


Try it online!

# JavaScript (Node.js), 59 bytes

x=>(g=t=>t&&g(t-1)+!([...x-t+''].reverse().join^t))(x>>1)


Try it online!

-1 Byte from Arnauld

• 64 bytes Jan 19 at 20:49
• 53 bytes by changing to Python 2 Jan 19 at 21:03

# APL (Dyalog Unicode), 40 35 bytes (SBCS)

{+/⍵=10⊥¨(⌽+⊢)¨10⊥⍣¯1¨¯1+(⌊⍵÷2)+⍳⍵}


Try it online!

37 32 with ⎕IO←0:

{+/⍵=10⊥¨(⌽+⊢)¨10⊥⍣¯1¨(⌊⍵÷2)+⍳⍵}


Try it online!

# UiuaSBCS, 19 bytes

/+=+⋕∵⇌°⋕.-+1⇡⌊÷2..


Try it!

/+=+⋕∵⇌°⋕.-+1⇡⌊÷2..
.. # duplicate input twice
⌊÷2   # halve and floor
⇡      # range
+1       # increment
-         # subtract from input
.          # duplicate
°⋕           # unparse (convert to array of boxed strings)
∵⇌             # reverse each
⋕               # convert all to integers
+                # add reversed to non-reversed
=                 # where is it equal to input?
/+                  # sum


# Ruby, 46 bytes

->x{(x/2..x).count{x==_1+_1.digits.join.to_i}}


Attempt This Online!

# 05AB1E, 7 bytes

;ŸDí+QO


Explanation:

;        # Halve the (implicit) input-integer
Ÿ       # Pop and push a list in the range [(implicit) input, floor(input/2)]
D      # Duplicate this list
í     # Reverse each inner value in the copy
+    # Add the values at the same positions in the lists together
Q   # Check for each whether it equals the (implicit) input
O  # Sum the amount of truthy values
# (which is output implicitly as result)


# Zsh, 48 bytes

for ((i=$1;i-->$1/2;c+=rev<<<$i+i==$1)):
<<<$c  Attempt This Online! Terribly slow, because to reverse a string in Zsh you have to spawn a process! # Perl 5, 48 bytes (30 bytes w/o args or return) ($x,$n)=(0,@_);$x+=$_+reverse==$n for$n/2..$n;\$x


Try it online!

# Desmos, 80 bytes

f(k)=∑_{n=.5k}^k0^{(k-n-∑_{d=0}^n10^{floor(logn)-d}mod(floor(n/10^d),10))^2}


Try It On Desmos!

Try It On Desmos! - Prettified

# Scala 3, 53 bytes

x=>(x/2 to x).count{i=>i+i.toString.reverse.toInt==x}
`

Attempt This Online!