13
\$\begingroup\$

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.

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

47 Answers 47

1
2
1
\$\begingroup\$

Pyth, 11

VhQIq`N_`NN

Example:

$ pyth -c 'VhQIq`N_`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
\$\endgroup\$
1
\$\begingroup\$

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

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.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ How does your script accept the input? In your example it seems to be part of the script itself. \$\endgroup\$
    – alephreish
    Nov 7 '14 at 11:06
  • \$\begingroup\$ @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. \$\endgroup\$
    – AndoDaan
    Nov 10 '14 at 5:42
  • \$\begingroup\$ 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. \$\endgroup\$
    – alephreish
    Nov 10 '14 at 17:00
  • \$\begingroup\$ @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. \$\endgroup\$
    – AndoDaan
    Nov 11 '14 at 4:20
  • \$\begingroup\$ @har-wradim wait, it's because I put [Input n] in my explenation, isn't it? Solved! \$\endgroup\$
    – AndoDaan
    Nov 11 '14 at 4:28
1
\$\begingroup\$

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

Python (54)

print filter(lambda i:`i`==`i`[::-1],range(input()+1))
\$\endgroup\$
1
1
\$\begingroup\$

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

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

Perl 5, 24 bytes

map$_-reverse||say,0..<>

Try it online!

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

Stax, 6 bytes

ô°hånⁿ

Run and debug it

-1 byte from Weijun Zhou's answer.

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

Javascript (ES6), 84 81 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?

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

Vyxal, j, 5 3 bytes

'Ḃ=

Try it Online!

Pretty simple answer really.

Explained

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

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.

\$\endgroup\$
0
\$\begingroup\$

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

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

Javascript, 98 bytes

n=>new Array(n+1).fill().map((a,b)=>b).filter(a=>[...''+a].reverse().join``==''+a&&(a<=n)).join` `
\$\endgroup\$
0
\$\begingroup\$

PowerShell, 67 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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