20
\$\begingroup\$

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!

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

14 Answers 14

8
\$\begingroup\$

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:
      Ʋ   -   last four links as a monad - f(X):
  D       -     convert {X} to decimal digits
   Ṛ      -     reverse
    Ḍ     -     convert from decimal digits
     +    -     add {X}
        ċ - 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!)

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – Neil
    Jan 20 at 1:42
  • \$\begingroup\$ Oh, very succinct @Neil. \$\endgroup\$ Jan 20 at 2:01
5
\$\begingroup\$

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!

\$\endgroup\$
5
\$\begingroup\$

Vyxal 3 s, 7 bytes

½⌊z-ᵛṁ=

Try it Online!

Port of Jelly answer

-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.

\$\endgroup\$
2
  • \$\begingroup\$ 7 \$\endgroup\$
    – pacman256
    Jan 20 at 7:53
  • 1
    \$\begingroup\$ @pacman256 done. \$\endgroup\$
    – Fmbalbuena
    Jan 20 at 14:57
4
\$\begingroup\$

Pyth, 9 bytes

/ec2m+s_`

Try it online!

Explanation

                 # implicitly assign Q = eval(input())
/ec2m+s_`ddQQ    # implicitly add ddQQ
    m      Q     # map lambda d over range(Q)
       _`d       #   reverse string(d)
      s          #   convert back to integer
     +    d      #   add d
  c2             # chop list into 2 parts of equal length
 e               # take the second part
/           Q    # count instances of Q
\$\endgroup\$
3
\$\begingroup\$

Charcoal, 16 15 bytes

NθIΣE⮌…·⊘θθ⁼κ⮌ι

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

Nθ              First input as a number
      …·        Inclusive range from
         θ      Input number
        ⊘       Halved
          θ     To input number
     ⮌          Reversed
    E           Map over values
            κ   Current index
           ⁼    Equals
             ι  Current value
            ⮌   Reversed
   Σ            Take the sum
  I             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.

\$\endgroup\$
2
\$\begingroup\$

Python 3, 56 bytes

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

Try it online!

or 53 bytes Python2 by Fmbalbuena

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

\$\endgroup\$
2
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

Uiua SBCS, 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
\$\endgroup\$
2
\$\begingroup\$

Ruby, 46 bytes

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

Attempt This Online!

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 7 bytes

;ŸDí+QO

Try it online or verify all test cases.

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)
\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

Scala 3, 53 bytes

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

Attempt This Online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.