| Bytes | Lang | Time | Link |
|---|---|---|---|
| 032 | jq | 250914T191319Z | pmf |
| 041 | Python 3 | 250901T193453Z | Jonathan |
| 168 | APLNARS | 250905T091315Z | Rosario |
| 9387 | F# | 250904T223339Z | Juuz |
| 006 | Japt æ | 250902T172530Z | Shaggy |
| 008 | Jelly | 250904T002958Z | caird co |
| 041 | Ruby nl | 250903T230226Z | Value In |
| 028 | Perl 5 pa | 250903T183252Z | Xcali |
| 006 | Nekomata | 250903T014551Z | alephalp |
| nan | C++ | 250902T171828Z | ayaan098 |
| 031 | x8664 machine code | 250902T163204Z | m90 |
| 038 | Python 2 | 250901T182531Z | M-- |
| 057 | Python 3.8 prerelease | 250902T091433Z | Ted |
| 030 | Julia 1.0 | 250902T093333Z | MarcMush |
| 007 | 05AB1E | 250902T082837Z | Kevin Cr |
| 008 | Brachylog | 250902T081310Z | Fatalize |
| 014 | Charcoal | 250901T233627Z | Neil |
| 110 | Google Sheets | 250901T195024Z | doubleun |
| 015 | Dyalog APL | 250901T174303Z | Aaron |
| 043 | JavaScript ES6 | 250901T161144Z | Arnauld |
| 049 | Retina | 250901T185417Z | Neil |
| 056 | R | 250901T185017Z | pajonk |
| 021 | cQuents | 250901T170258Z | Stephen |
| 006 | Vyxal | 250901T143819Z | lyxal |
Python 3, 41 bytes
(Port from my golf of M--'s Python 2 answer)
f=lambda n:n-int(str(n)[::-1])and-~f(n+1)
A recursive function that accepts a positive integer and returns the minimal amount to add to make a palindrome (assuming an unbounded stack).
How?
f=lambda n: # f is a function that accepts n, which...
n- # subtract from n the value:
str(n) # get the string representation of n
[::-1] # reversed (start=None, Stop=None, Step=-1)
int( ) # cast to an integer
and # short circuit if that is zero, returning 0; otherwise...
f(n+1) # call f again, but with n incremented
~ # bitwise NOT -> -1 - f(n+1)
- # times -1 -> f(n+1) + 1
Note that no palindromic n after zero ends in a zero, so the cast to an int of the reversal is fit for purpose (i.e. it doesn't matter that with n=60000, for example, int(str(n)[::-1]) is 6).
Python 2, 36 bytes
f=lambda n:`n`!=`n`[::-1]and-~f(n+1)
Assuming an unbounded stack, this works up to \$9223372036302733229\$ (the last palindrome before \$2^{63}\$), after which Longs come into play, making for a trailing L in the repr (`n`), which we'll then never find at the beginning of our number.
APL(NARS), 168 chars
fm←{k←(t←⌊2÷⍨≢w)↑w←⍕⍵⋄2∣≢w:k,w[t+1],⌽k⋄k,⌽k}⋄fp←{k←(t←⌊2÷⍨≢w)↑w←⍕⍵⋄2∣≢w:k,⌽¯1↓k⊣k←⍕1+⍎k,w[t+1],'x'⋄k,⌽k←⍕1+⍎k,'x'}⋄f←{a←⍵-⍨⍎(fm⍵),'x'⋄b←⍵-⍨⍎(fp⍵),'x'⋄(a≥0)∧(∣b)≥∣a:a⋄b}
fm and fp has as input one number a, and return both one chars palidrome build on a, in the way (⍎fm a)<⍎fp a and a<⍎fp a. This seems ok on test but something could be not ok (for the series "prove me wrong").
Test:
f 5
0
f 14
8
f 999
0
f 9999
0
f 200
2
f 819
9
f 1100
11
f 1122
99
f 1
0
f 9
0
f 10
1
f 58676078971371873291721082108321061783156251745146321863197319x
1318344856467492670996123870366
58676078971371873291721082108321061783156251745146321863197319x+1318344856467492670996123870366x
58676078971371873291721082108322380128012719237817317987067685
F#, 93 87 bytes
let rec i y x=let s=($"{x+y}").ToCharArray()in if s=Array.rev s then y else i(y+1)x
i 0
A function expression that takes an int and returns another int.
Commented
let rec i y x = // inner "loop" function
let s = ($"{x+y}").ToCharArray() in // store the formatted number as char array
if s = Array.rev s then // check if the array is a palindrome
y // if true, found y
else
i (y + 1) x // otherwise, look at y + 1
i 0 // start by looking at y = 0
-6 bytes by using a single function definition and partially applying the first parameter.
Japt -æ, 6 bytes
N¥N°ìÔ
N¥N°ìÔ :Implicit filter of range [0,input)
N :Array of inputs, initially (i.e., [X])
¥ :Is loosely equal to
N° : Postfix increment N for next iteration, coercing to an integer on first iteration
ì : Convert to digit array
Ô : Reverse and convert back to integer
:Implicit output of first element of resulting array
Ruby -nl, 41 bytes
succ (short for successor) is a magical function that lets me increment a string that's a number, letting me work directly on stdin input and thus saving me overhead needed in a function taking integer input, which needs to cast to string to check if it's a palindrome. succ! is the same but modifies in-place, perfect for what we need. && shortcutting to increment the counter because it saves a byte over putting it in the loop.
i=0
$_.succ!while$_!=$_.reverse&&i+=1
p i
Nekomata, 6 bytes
ᵏ{+Ɗƀ=
ᵏ{+Ɗƀ=
ᵏ{ Find the first non-negative integer that satisfies the following:
+ Add the input
Ɗ Decimal digits
ƀ= Check that it is a palindrome
C++, 159 140 bytes
(Thanks to @PeterCordes for saving 18 bytes.)
#include <bits/stdc++.h>
int f(int x){for(int y=0;;++y){auto s=std::to_string(x+y);if(std::ranges::equal(s|std::views::reverse,s))throw y;}}
Returns a value by throwing an int.
x86-64 machine code, 31 bytes
6A 0A 5E 8D 47 FF FF C0 50 31 C9 99 F7 F6 6B C9 0A 01 D1 85 C0 75 F4 58 39 C8 75 EA 29 F8 C3
Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes X as a 32-bit integer in EDI and returns Y as a 32-bit integer in EAX.
In assembly:
f: push 10; pop rsi # Set RSI (the 64-bit register containing ESI) to 10.
lea eax, [rdi - 1] # Set EAX to X minus 1.
r: inc eax # Add 1 to EAX.
push rax # Save RAX (contains EAX) onto the stack.
xor ecx, ecx # Set ECX to 0.
rd: cdq # Sign-extend EAX, setting up for the next instruction.
div esi # Divide by ESI (10); put the quotient in EAX and the remainder in EDX.
imul ecx, ecx, 10 # Multiply ECX by 10.
add ecx, edx # Add the remainder to ECX. (Digits are transferred one by one.)
test eax, eax # Set flags based on the value of EAX.
jnz rd # Jump back, to repeat the division, if it's nonzero.
pop rax # Restore the value of RAX (contains EAX) from the stack.
cmp eax, ecx # Compare EAX and ECX (the original and reversed number).
jne r # Jump back, to add 1 and repeat, if they are not equal.
sub eax, edi # Subtract EDI (X) from EAX, leaving Y.
ret # Return.
Python 2, 49 39 38* bytes
f=lambda n:n-int(`n`[::-1])and-~f(n+1)
Inspired by ArBo's answer.
* Update based on Jonathan Allan's comment.
Python 3.8 (pre-release), 57 bytes
lambda n:[x for x in range(n)if(s:=str(x+n))==s[::-1]][0]
Different python approach.
I'm pretty sure that the result will always be within domain [n,2*n] as each digit must have changed during that domain. So therefore it's enough to search that range for a match.
05AB1E, 7 bytes
∞<.Δ+ÂQ
Try it online or verify all test cases.
Explanation:
∞ # Push an infinite positive list: [1,2,3,...]
< # Decrease each value by 1 to make it non-negative: [0,1,2,...]
.Δ # Find the first value in this list that's truthy for:
+ # Add the current value to the (implicit) input-integer
 # Bifurcate it; short for Duplicate & Reverse copy
Q # Check if the two are the same (aka, it's a palindrome)
# (after which the found result is output implicitly)
Brachylog, 9 8 bytes
;.+I↔I∧ℕ
Explanation
;. Given a list [Input, Output]
+I I = Input + Output
I↔I I reversed is I
∧ℕ Output is a non-negative integer
(Implicit: find the first value of Output that satisfies the constraints)
Charcoal, 14 bytes
NηW⁻η⮌η≦⊕ηI⁻ηθ
Try it online! Link is to verbose version of code. Explanation:
Nη
Make a copy of X.
W⁻η⮌η
Until X is a palindrome...
≦⊕η
... increment X.
I⁻ηθ
Subtract the original value of X to find Y.
Google Sheets, 110 bytes
=let(f,lambda(f,n,let(l,len(n),s,sequence(l),if(sort(and(mid(n,s,1)=mid(n,l-s+1,1))),n,f(f,n+1)))),f(f,A1)-A1)
Recursive function that expects \$n\$ in cell A1. Checks if \$n\$ is a palidrome, and if not, checks \$n + 1\$. Finally subtracts the original value of \$n\$.
Text functions such as len() and mid() coerce numbers to strings, but there's no built-in for reverse, so the function compares each digit with the digit at the opposite index.

Ungolfed:
=let(
f, lambda(f, n, let(
l, len(n),
s, sequence(l),
if(
sort(and(mid(n, s, 1) = mid(n, l - s + 1, 1))),
n,
f(f, n + 1)
)
)),
f(f, A1) - A1
)
Dyalog APL, 22 17 15 bytes
-2 bytes thanks to @Adám
1∘+⍣{⌽⍛≡⍕⍵}-+∘1
1∘+ # Add one to the input
⍣{ } # until
⍕⍵ # The string of the right arg
≡ # matches itself
⌽⍛ # when reversed
- # and subtract
+∘1 # one more than the input
💎
Created with the help of Luminespire.
JavaScript (ES6), 43 bytes
f=n=>[...n+""].reverse().join``-n&&1+f(n+1)
Commented
f = n => // f is a recursive function taking n
[...n + ""] // coerce n to a string and split it
.reverse() // reverse
.join`` // join back
- n && // if this is not equal to n:
1 + // add 1 to the final result
f(n + 1) // and do a recursive call with n + 1
Retina, 49 bytes
$
¶0
/^(.)*.?(?<-1>\1)*(?(1)^)¶/^+%`.+
$.(*__
0A`
Try it online! Link includes test cases. Explanation:
$
¶0
Append a counter.
/^(.)*.?(?<-1>\1)*(?(1)^)¶/^+`
While the input is not palindromic...
%`.+
$.(*__
... increment both it and the counter.
0A`
Delete the palindromised input, leaving the counter.
R, 56 bytes
\(X){while(any(rev(d<-utf8ToInt(c(X+F,"")))-d))F=F+1
+F}
Recursive:
R, 62 bytes
`?`=\(X,Y=0,d=utf8ToInt(c(X+Y,"")))`if`(any(d-rev(d)),X?Y+1,Y)
cQuents, 21 bytes
#|1:-1+#N-1+A=\rN-1+A
cQuents conditionals start at 1, so a lot of bending over backwards to check for 0 as well.
Explanation
#|1:-1+#N-1+A=\rN-1+A .
#|1 set n (last input) = 1
: mode sequence: output the nth term in the sequence
each term is
-1+ -1 +
# ) first N such that ( )
N-1+A N - 1 + first input
= =
\r ) reverse( )
N-1+A N - 1 + first input
Vyxal, 6 bytes
?+Ḃ=)Ṅ
Nice and simple
Explained
?+Ḃ=)Ṅ
)Ṅ # Find the first Y >= 0 where:
?+ # X + Y
= # equals
Ḃ # it's reverse
💎
Created with the help of Luminespire.