# Enumerate all palindromic numbers (in decimal) between 0 and n

Given a non-negative integer n, enumerate all palindromic numbers (in decimal) between 0 and n (inclusive range). A palindromic number remains the same when its digits are reversed.

The first palindromic numbers (in base 10) are given here:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292, 303, 313, 323, 333, 343, 353, 363, 373, 383, 393, 404, 414, 424, 434, 444, 454, 464, 474, 484, 494, 505, 515, ...

This is a code golf with the prize going to fewest characters. The palindromic numbers should be output one per line to stdout. The program should read n from the commandline or stdin.

• Between 1 and n (as per title) or 0 and n (as per body)? And which of the bounds does "between" include? Aug 23 '11 at 11:45
• @wok: You still haven't answered if it's inclusive or exclusive range? Is n part of the range to include? Aug 23 '11 at 15:09
• @mellamokb Inclusive range.
– Wok
Aug 23 '11 at 15:15

# Pyth, 11

VhQIqN_NN


Example:

$pyth -c 'VhQIqN_NN' <<< 200 0 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 111 121 131 141 151 161 171 181 191  # C++, 145 143 #include<iostream> int i,j,n,r;int main(){std::cin>>n;for(;i<=n;i++){j=i;while(j!=0){r=r*10;r=r+j%10;j=j/10;}if(i==r)std::cout<<i<<' ';r=0;}}  Alternative [143 too]: #include<stdio.h> int i,j,n,r;int main(){scanf("%d",&n);for(;i<=n;i++){j=i;while(j!=0){r=r*10;r=r+j%10;j=j/10;}if(i==r)printf("%d ", i);r=0;}}  # Burlesque, 13 rz{J<-==}f[p^  Burlesque has some useful commands for golfing, in this case <- reverses anything you put before it.  rz creates a range from 0 to input { start of filtering (discards any element not returning 1) the created range J <- == duplicates element, reverses duplicate, compare }f[ end filtering, now we're left with a block of palindromic integer which p^ takes care of, and outputs the numbers as desired.  Try it here The linked example has 1000 as input, change it to see more or fewer palindromic numbers. • How does your script accept the input? In your example it seems to be part of the script itself. Nov 7 '14 at 11:06 • @har-wradim Burlesque takes input the same way as Golfscript does. Meaning that input is placed on the stack before the program code. [input][code]. In my program above input can be any positive number (the linked example has 1000 for input), which will be followed by the code rz{J<-==}f[p^ You can change the 1000 to 10 000 or 100 000 and the code will run all the palindromic number up until the input value. Nov 10 '14 at 5:42 • GolfScript is expected to push what comes from stdin on top of the stack by default. If this behavior is absent from the Burlesque specifications than your solution seems to violate one of the requirements. Nov 10 '14 at 17:00 • @har-wradim As does Burlesque. I linked to the online shell for ease of access, but feel free to download and compile the Burlesque source code to check the different ways it can read input. Nov 11 '14 at 4:20 • @har-wradim wait, it's because I put [Input n] in my explenation, isn't it? Solved! Nov 11 '14 at 4:28 # Gogh, 17 bytes This answer is non-competing, as the language was created after the challenge was posted. G{÷÷sR=¦?}m0%{Ƥ}m  Usage: $ ./gogh i 'G{÷÷sR=¦?}m0%{Ƥ}m' <input>


### Explanation

        “ Implicit input.                                      ”
G       “ Push an inclusive list.                              ”
{       “ Open a code block.                                   ”
÷÷     “ Push two copies of the TOS.                          ”
sR=    “ Check if the integer is palindromic.                 ”
¦?     “ If n is palindromic, yield n. If not, yield 0.       ”
}m      “ Close the code block and map it to the TOS.          ”
0%      “ Remove non-palindromic integers (denoted by zeroes). ”
{       “ Open a code block.                                   ”
Ƥ      “ Print.                                               ”
}m      “ Close the code block and map it to the TOS.          ”
“ Implicit output.                                     ”


Python (54)

print filter(lambda i:i==i[::-1],range(input()+1))

• Added print staement Mar 19 '18 at 13:50

# Husk, 6 bytes

#S=↔Θḣ


Try it online!

'Enumerates' palindromic numbers (so, outputs how many there are). To list all of them, use fS=↔Θḣ for the same number of bytes.

#        # number of elements that
S=      # are equal to themselves after
↔     # reversing their digits,
Θḣ   # among 0 followed by 1..input.


# Zsh, 34 33 bytes

eval \>{0..$1}';ls ls|rev;rm *'  Try it online! • {0..$1} - repeat the string with this part substituted with the numbers from 0 to the input
• evaluate that
• > - create the file with that number
• ls - list all the files in the current directory
• |rev - reverse that
• ls ls|rev - print the intersection of the files in the current directory and the output of ls|rev (i.e. filter palindromes)
• rm * - remove all files

map$_-reverse||say,0..<>  Try it online! # Stax, 6 bytes ô°hånⁿ  Run and debug it -1 byte from Weijun Zhou's answer. # Javascript (ES6), 8481 74 bytes u=(n,c=n+1)=>c?u(n,--c).concat(${c}.split.reverse().join==c?c:[]):[]

u is function.

There's got to be a better way to golf the loop. Php is annoying but at least it has builtins...

Rewrote the whole thing, recursive now. Is there something else for array.concat that pushes nothing?

Why did I have a random argument that did nothing?

• When answering with a function, you don't have to assign it to a variable. Feb 16 at 7:40
• @xigoi I think I do, because I need to push to result if necessary. Unless I try a recursive function... Back in 15 mins. Feb 16 at 8:01

# Vyxal, j, 5 3 bytes

'Ḃ=


Try it Online!

## Explained

'Ḃ=
'     # Filter by:
Ḇ=   #     Bifuricate the argument passed and check if the two results are equal
# The j flag joins on newlines


## T-SQL, 72

DECLARE @@ INT=X,@ INT=0#:IF @=REVERSE(@)PRINT @ SET @+=1IF @<=@@ GOTO #


Replace "X" with the input number. This code runs in SQL Server 2008 R2.

# Visual Basic for Applications, 66 bytes

Callable from the immediate window.

Sub u(b)
For i=0To b
If StrReverse(i)=i Then Debug.?i
Next
End Sub


# Stax, 7 characters

çi/i∩≈f


Run and debug online!

## Explanation

Uses the unpacked version to explain.

^rfcEcr=
^r          [0..n]
f         Print each element that matches the predicate on individual line
cE       Array of decimal digits
cr=    Equals its reverse


# Javascript, 98 bytes

n=>new Array(n+1).fill().map((a,b)=>b).filter(a=>[...''+a].reverse().join==''+a&&(a<=n)).join


# PowerShell, 67 bytes

0..$args[0]|%{$x=[string]$_;if($x-eq(-join$x[-1..-$x.length])){\$x}}


Try it online!

## Pyke, 6 bytes

S#D_q


Explanation:

S      - range(1, input()+1)
#     - filter where True
D_q -  str(num) == str(num)[::-1]


Try it here!