# Numbers with Rotational Symmetry

Given an integer, output a truthy value if it is the same upside-down (rotated 180°) or a falsy value otherwise.

0, 1, and 8 have rotational symmetry. 6 becomes 9 and vice versa.

Sequence of numbers producing truthy results: OEIS A000787

0, 1, 8, 11, 69, 88, 96, 101, 111, 181, 609, 619, 689, 808, 818, 888, 906, 916, 986, 1001, 1111, 1691, 1881, 1961, 6009, 6119, 6699, 6889, 6969, 8008, 8118, 8698, 8888, 8968, 9006, 9116, 9696, 9886, 9966, 10001, 10101, 10801, 11011, 11111, 11811, 16091, ...


This question is inspired by my own reputation at the time of posting: 6009.

• May we take the input as a string?
– xnor
Apr 15, 2016 at 21:26
• @xnor If the language has numeric types, it should use them, for function parameters, for example. But if, in Python for example, you take raw_input, the user entered an integer, which would become a string behind the scenes. That's fine. Apr 15, 2016 at 21:32
• On seven-segment displays, 🯲 and 🯵 both rotate to themselves - that would make a worthwhile extension to this challenge. Jul 6, 2022 at 7:39
• Jul 6, 2022 at 9:17

## Seriously, 23 bytes

,$;"01xxxx9x86"íMRεj=  Try it online! This is essentially a port of xnor's Python 2 solution. Explanation: ,$;"01xxxx9x86"íMRεj=
,$; push two copies of str(input) "01xxxx9x86"íM map: get each digit's rotated digit (or x if not rotatable) Rεj reverse and join on empty string to make a string = compare equality with original input  # R, 111 bytes With integer input and 0 output corresponding to truthy f=sum p=1:nchar(a<-scan()) b=a%%(l<-10^p)%/%(l/10) d=(b-rev(b))^2 f(b>2&b<8&b!=6)+f(d[b<2|b==8])+f(d[!b%%3]!=9)  Try it online! # Kotlin, 69 bytes This takes the number, converts it to a string, rotates it, and then compares it with the original as a string for equality. Non-rotatable digits are simply converted to 0 {i->"$i".map{"0100009086"[it-'0']}.joinToString("").reversed()=="$i"}  Test it Here! Nobody had posted a Python 3 solution so I'll go with my rubbish one # Python 3, 190 95 bytes lambda x:not str(x).replace('0','').replace('1','').replace('6','').replace('8','').replace('9','').replace('0','')and str(x)[::-1].replace('6','z').replace('9','6').replace('z','9')==str(x)  EDIT: lambda x:(x:=str(x))==[x:=x[::-1].replace(*p)for p in['6z','96','z9']][2]and not{*x}-{*'01689'}  # K (ngn/k), 25 24 bytes {d~|"01986""01689"?d:$x}


Try it online!

-1 byte thanks to @ngn

My first version was 74 bytes but then I read @xnor's solution and decided to translate that.

• Using a Bubbler train doesn’t save any bytes here. f:~/1(|"01986"@"01689"?)\$: – doug Aug 16, 2022 at 21:29 • you can remove the @ – ngn Aug 18, 2022 at 7:31 • shorter: {d~|x[<x]x?d:$y}"01986"
– ngn
Aug 18, 2022 at 7:39
• Clever! It’s enough of a leap that I’ll leave mine as is though. This feels like a fresh idea.
– doug
Aug 18, 2022 at 7:47
• ok, i'll post a separate answer. i have yet another idea.
– ngn
Aug 18, 2022 at 7:59

# K (ngn/k), 21 bytes

{y~|x[>x]x?y}/$68910,  Try it online! 68910, prepend 68910 to the argument $ format both as strings

{ }/ apply a function to x="68910" and y= the original argument as string

x?y find the positions of the elements of y among x. use "null" (0N) for "nout found".

x[>x] down-sorted x, i.e. "98610"

x[>x]x?y index x[<x] with x?y. out-of-bounds indexing produces spaces

| reverse

y~ test if y matches that

# Zsh, 65 56 bytes

Thanks to pxeger for saving a whopping 9 bytes!

>${(j..)${(Oas..)1}}<${${${${1//6/.}//<2-7>}//9/6}//./9}


Outputs via return code. The > construct creates and opens a file whose name is the reversed number for writing. The program will only exit truthy when the < construct attempts to read from a file with 6/9 swapped, and 2/3/4/5/7 removed.

• -2 bytes using the <-> glob syntax to match numbers: Try it online! Sep 12, 2022 at 12:23
• Save another 7 by abusing the filesystem for the string comparison: Try it online! Sep 12, 2022 at 12:33

# PHP, 83 bytes

<?=$argn==strrev(str_replace([6,9,x],[x,6,9],preg_replace("/[^01689]/","",$argn)));


Save the code in a file (let's say symmetry.php) and run it as:

php -d error_reporting=0 -F symmetry.php


Run it online on TIO.

The algorithm is simple: remove the non-symmetric characters, replace 6 with 9 and 9 with 6 then reverse the result. If it equals the input string then it is a rotationally symmetrical number.

# Brev, 80 bytes

(define(r n)(= n((as-list reverse(over(vector-ref #(0 1 3 4 5 6 9 8 8 6)x)))n)))


Non-minified:

(define (rotsym? n)
(= n ((as-list reverse (over (vector-ref #(0 1 3 4 5 6 9 8 8 6) x))) n)))


var a=[0,1,6,8,9],b=[0,1,9,8,6],c=readLine()!.map {Int(String($0))!},f = false,t = c.count;for i in 0..<t{f = c[t-i-1] == b[a.index(of: c[i]) ?? 0]}  # Scala, 184 bytes Golfed version. Try it online! def f(n:Int)=n.toString.map{case '1'=>'1';case '2'=>'w';case '3'=>'z';case '4'=>'y';case '5'=>'x';case '6'=>'9';case '7'=>'x';case '8'=>'8';case '9'=>'6';case c=>c}==n.toString.reverse  Ungolfed version. Try it online! object Main { def f(n: Int): Boolean = { val original = n.toString val transformed = original.map { case '1' => '1' case '2' => 'w' case '3' => 'z' case '4' => 'y' case '5' => 'x' case '6' => '9' case '7' => 'x' case '8' => '8' case '9' => '6' case c => c } original == transformed.reverse.mkString } def main(args: Array[String]): Unit = { for (n <- 1 until 1005 if f(n)) { println(s"$n satisfies the condition")
}
}
}


# Lua, 73 bytes

r=''s=...s:gsub('.',load'r=({0,1,0,0,0,0,9,0,8,6})[...+1]..r')print(r==s)


Try it online!

# Thunno 2, 12 bytes

kDḣ»©®£~»Ṇr=


Try it online!

Thunno 2, Vyxal, and 05AB1E all at 12 bytes!

#### Explanation

kDḣ»©®£~»Ṇr=  # Implicit input
kDḣ           # Push "123456789"