# Display numbers lacking 2's

Display numbers from one to one-hundred (in increasing order), but number 2 shouldn’t appear anywhere in the sequence. So, for example, the numbers two (2) or twenty-three (23) shouldn't be in the sequence.

Here is an example output, with newlines separating the numbers:

1
3
4
5
6
7
8
9
10
11
13
14
15
16
17
18
19
30
31
33
34
35
36
37
38
39
40
41
43
44
45
46
47
48
49
50
51
53
54
55
56
57
58
59
60
61
63
64
65
66
67
68
69
70
71
73
74
75
76
77
78
79
80
81
83
84
85
86
87
88
89
90
91
93
94
95
96
97
98
99
100

• I assume the winning criteria is code-golf right? Oct 2, 2018 at 13:26
• Our site does not work the same way as other sites from the Stack Exchange network. Regarding accepted answers, please take this comment from Jonathan Allan into account. And please add a winning criterion. Oct 2, 2018 at 14:08
• Might I suggest using the Sandbox in the future to get feedback on your challenges before posting?
– Jo King
Oct 2, 2018 at 14:15
• @Monolica If the shortest answer wins, you'll need the tag [code-golf]. Here is a list for all available winning criteria tags for future reference. Oct 2, 2018 at 14:19
• @user202729 Arbitrary restriction is unwelcome, not disallowed. Oct 3, 2018 at 1:11

# K4, 15 bytes

Solution:

1_&~"2"in'$!101  Example: q)k)1_&~"2"in'$!101
1 3 4 5 6 7 8 9 10 11 13 14 15 16 17 18 19 30 31 33 34 35 36 37 38 39 40 41 43 44 45 46 47 48 49 50 51 53 54 55 56 57 58 59 60 61 63 64 65 66 67 68 69 70 71 73 74 75 76 77 78 79 80 81 83 84 85 86 87 88 89 90 91 93 94 95 96 97 98 99 100


Explanation:

1_&~"2"in'$!101 / the solution !101 / range 0..100$     / string
"2"in'      / is "2" in each?
~            / not
&             / indices where true
1_              / drop the first


Extra:

TIO for 16 byte K (oK) answer: Try it online!

# Jelly, 7 bytes

³D2eṆƲƇ


Try it online!

# Ruby, 35 31 bytes

100.times{|e|p e if/2/!~e.to_s}


Try it online!

Thanks to Conor O'Brien for -4 bytes

# Pyth, 17 bytes

VS100I!}\2%"%s"NN


Try it online

My first code golf submission, also my first time using Pyth, this took way longer to put together than I expected.

# Explanation

VS100 for loop from 1 to 100

I!}\2%"%s if the string "2" is in the string conversion of N

%"%sN convert N to a string. This is the only way I could figure out how to do this. If there is a shorter way to do int -> string conversion please let me know. I'm honestly just looking for a way to call str() or repr() on it with less chars.

N print n

EDIT: Looks like someone else beat me to it with Pyth. Turns out to convert N to a str I could do N, I'm going to leave mine the same because if I change it its almost the exact same solution.

• Welcome to the site! Oct 4, 2018 at 22:38

# Scala, 56484745 42 bytes

1 to'd'filter(_+""forall(50!=))map println


Prints the elements separated by a line.

Here is a 38 bytes version that calls toString (technically allowed by the rules):

print(1 to'd'filter(_+""forall(50!=)))


Resulting in:

Vector(1, 3, 4, ..., 100)


# Perl 5, 19 bytes

map{/2/||say}1..100


Try it online!

• Nice, can't come to with anything more terse just yet! You can save one bute going with map/2/||say,1..100 though! Jan 13 at 10:10

# Awk, 32 bytes

END{while(i++<100)print i~2?a:i}


Try it online!

To save space, I used Awk's behavior of assigning the null string to uninitialized variables to avoid initializing the counter and saving a byte with the null string literal.

# Uiua, 36 bytes

Returns an array with numbers from 1 to 100 where the elements containing a two are removed.

▽×∩(≠2)⌊⊃÷◿10.+1⇡100


Try It

▽×∩(≠2)⌊⊃÷◿10.+1⇡100
+1⇡100 # generate numbers 1..100
. # duplicate the array for filtering
⊃÷◿10 # both divide by 10 and mod 10 each number
⌊ # floor the division result so that we have the 10s place
∩(≠2) # for both the mod and floored division (ones and tens places)
# check that there is no 2
× # use multiply as and
▽ # filter the array


# JavaScript (ES6), 51 bytes

After reading Arnauld's answer, I wanted to try a non-recursive approach. Sure enough, it's longer, but it may be of some interest.

_=>[...Array(101).keys()].filter(n=>n*!/2/.test(n))


Try it online!

[...Array(101).keys()] creates a list of the numbers 0 through 100. We then filter to get rid of 0 and any number that contains a 2.

# Whitespace, 121 bytes

[S S S N
_Push_0][N
S S N
_Create_Label_LOOP][S S S T N
_Push_1][T  S S S _Add][S N
S _Duplicate][S S S T   T   S S T   S T N
_Push_101][T    S S T   _Subtract][N
T   S S N
_If_0_Jump_to_Label_EXIT][S N
S _Duplicate][S S S T   S T S N
_Push_10][T S T T   _Modulo][S S S T    S N
_Push_2][T  S S T   _Subtract][N
T   S N
_If_0_Jump_to_Label_LOOP][S N
S _Duplicate][S S S T   S T S N
_Push_10][T S T S _Integer_divide][S S S T  S N
_Push_2][T  S S T   _Subtract][N
T   S N
_If_0_Jump_to_Label_LOOP][S N
S _Duplicate][T N
S T _Print_integer_to_STDOUT][S S S T   S T S N
_Push_10][T N
S S _Print_character_to_STDOUT][N
S N
N
_Jump_to_Label_LOOP]


Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Pseudo-code:

Integer i = 0
Label LOOP:
i = i+1
If(i == 101):
Exit program
If(i modulo-10 == 2):
Go to next iteration of LOOP
If(i integer-divided by 10 == 2):
Go to next iteration of LOOP
Print i to STDOUT
Print a newline to STDOUT
Go to next iteration of LOOP


# Gol><>, 19 bytes

dFLP:aSD2=$2=+ZN|;  Try it online! Explanation: dFLP:aSD2=$2=+ZN|;

d                  //Push 100 to the stack
F                 //For loop from 0 to 99
LP               //  Push the loopcounter+1 to the stack,achieving a range [1..100]
:              //  Double for potential output later (saves 1 byte compared to pushing it again)
aSD           //  Push division & modulus of current number by 10
2=         //  Check if (num % 10) == 2 , pushes either 0 or 1
$2= // Check if (num / 10) == 2 , " + // Add the results acting as a logical or for is zero question ZN // If neither the mod nor the div are equal to 2 output the number |; //Exit after loop  # Ruby, 28 characters puts [*?1..'100'].grep_v /2/  Thanks to: Sample run: bash-4.4$ ruby -e 'puts [*?1.."100"].grep_v /2/' | head -15
1
3
4
5
6
7
8
9
10
11
13
14
15
16
17


Try it online!

# Forth (gforth), 58 bytes

: f 101 1 do i 10 /mod 2 <> swap 2 <> * if i . then loop ;


Try it online!

### Explanation

Loops from 1 to 100 (inclusive) and if both the ones and tens place of the index are not equal to 2, print it

### Code explanation

: f                    \ start a new word definition
101 1 do             \ start a loop from 1 to 100 inclusive
i 10 /mod          \ get the quotient and remainder of dividing i by 10
2 <>               \ check if the the tens place is not equal to 2
swap               \ move the result down the stack one
2 <>               \ check that the ones place is not equal to 2
*                  \ multiply results (cheaper AND)
if i . then        \ if true, output i
loop                 \ end counted loop
;                      \ end word definition


1 FOR I=1 TO 100:IF INSTR(STR$(I),"2")=0 THEN PRINT I 2 NEXT  There's probably a mathematical way to do this, but this approach is what first came to mind. # T-SQL, 65 bytes DECLARE @ INT=1a:IF @ NOT LIKE'%2%'PRINT @;SET @+=1IF @<101GOTO a  I'm a bit annoyed that this variable loop is shorter than the best set-based SQL variant I could find (91 bytes): SELECT DISTINCT number FROM spt_values WHERE number>0and number<101AND number NOT LIKE'%2%'  This one even has the additional restriction of needing to be run in the master database (since it uses an undocumented system table). The best set-based solution I could find without that requirement was 94 bytes: WITH t AS(SELECT 1n UNION ALL SELECT n+1FROM t WHERE n<100)SELECT*FROM t WHERE n NOT LIKE'%2%'  # Perl 5 (5.12+), 25 bytes $,=$/;say grep!/2/,1..100  Try it online! Pretty straightforward. The $,=$/ makes the following print put a newline between list elements, which is shorter than using join. # Attache, 24 bytes Output!2&!in@List\1:100  Try it online! Alternatively, Print=>2&!in@List\1:100. Filters (\) from 1:100 values whose digits (@List) do not contain (!in) a 2 (2&), which is then Outputted. # Ruby, 31 bytes p$.if"#{$.+=1}"!~/2/ until$.>99


Try it online!

A different, iterative approach than other ruby answers. This uses the not-match operator !~ as well as the implicitly-zero input line number variable $. as the iterator. The TIO link shows my golfing progress, from 48 bytes to 31 bytes. # APL(NARS), 17 chars, 34 bytes a/⍨∼'2'∊¨⍕¨a←⍳100  test  a/⍨∼'2'∊¨⍕¨a←⍳100 1 3 4 5 6 7 8 9 10 11 13 14 15 16 17 18 19 30 31 33 34 35 36 37 38 39 40 41 43 44 45 46 47 48 49 50 51 53 54 55 56 57 58 59 60 61 63 64 65 66 67 68 69 70 71 73 74 75 76 77 78 79 80 81 83 84 85 86 87 88 89 90 91 93 94 95 96 97 98 99 100  • I don't know much about APL (much less this specific variant), but aren't the instructions typically single bytes from a different codepage? – Jo King Oct 4, 2018 at 5:06 • @Joking in this case 'codepage' is 2 bytes for one char – user58988 Oct 4, 2018 at 5:11 # F#, 66 bytes ## Solution Seq.filter(fun i->i%10<>2&&i/10<>2){1..100}|>Seq.iter(printfn"%d")  ## Explanation Filters a sequence of numbers 1 to 100 by retaining those whose modulus of 10 is not 2, and which integer-divided by 10 do not result in 2, then prints the sequence with newlines between items. This works for numbers up to and including 199. A more general solution is slightly longer (74 bytes): Seq.filter(fun i->(string i).IndexOf '2'<0){1..100}|>Seq.iter(printfn"%d")  With proper spacing, and rearranged for readability, this looks as follows: {1 .. 100} |> Seq.filter (fun i -> (string i).IndexOf '2' < 0) |> Seq.iter (printfn "%d")  # V, 14 13 bytes Saved 1 byte thanks to @oktupol á199ñÄj<C-a>ñç2/d  Try it online! <C-A> represents the start of heading character (0x01) ## Explanation á1 Insert a 1 on the first line 99ñ Repeat 99 times: Ä Duplicate the last line j<C-A> Increment the last line by one ñç2/ For every line containing a 2: d Remove the line  • Use globals to save one byte: TIO Oct 4, 2018 at 13:52 # Rust, 65 bytes fn main(){for i in 1..101{if i%10!=2&&i/10!=2{println!("{}",i)}}}  Try it online! Similar to the answers by Meerkat and dumetrulo, just iterate through and print only those whose ones digit and tens digit are not 2. # Ohm v2, 7 9 bytes ⁸#u⁇2εX  Explanation: ⁸#u⁇2εX ⁸ Push 100 # Push range 0..a u Turn to string (vectorizes) ⁇ Filter only those that match conditional 2εX Match those that do not contain "2"  Try it online! # LUA, 1136847 45 bytes Thanks to LUA's rather loose dynamic typing, can straight-up search the integer. Thanks to @manatwork (twice) Thanks to @Jo King for x=1,100 do n=("").find(x,2)or print(x)end  Try it online! • “All solutions to challenges should: (…) Be a serious contender for the winning criteria in use. For example, an entry to a code golf contest needs to be golfed, and an entry to a speed contest should make some attempt to be fast.” — help center Please try to reduce the size of your solution. With just minimal refactoring can become 68 characters long: x=0 while x<100 do x=x+1 if not string.find(x,2)then print(x)end end. There are some Tips for golfing in Lua to help you out. Oct 5, 2018 at 9:58 • Welcome to PPCG! You can golf this down to 47 bytes using a for loop and a ternary statement instead of an if – Jo King Oct 5, 2018 at 10:02 • Thanks. Still trolling through that tips link to see what can apply. Tried a few things, but the online compiler didn't seem to like my attempts at brevity. There are obviously many answers here posted that were not going to be a contender (the C# is my favorite, but I like the Taxi answer as well). Oct 5, 2018 at 10:03 • Ah, most people consider code-golf a competition between submissions in the same language. While a Taxi or Shakespeare submission might appear absurdly long compared to regular programming languages, remember that they are far outgolfed by any highly focused golfing language. Hope you stick around, since there aren't that many Lua golfers :) – Jo King Oct 5, 2018 at 10:05 • Thanks for the welcome. Still wondering if there isn't some way curb this even more using a goto statement. Now taking a look at your 47 byte solution... Oct 5, 2018 at 10:08 # [C# (.NET Core)], 63 62 bytes Thanks to milk for saving one byte in the for loop. for(int i=0;++i<101;)if(i%10!=2&&i/10!=2)Console.WriteLine(i);  Try it online! Ungolfed: for (int i = 0; ++i < 101;) // all integers from 1-100 (starts from zero, increments before code runs) if (i % 10 != 2 // all numbers not ending with '2' && i / 10 != 2) // all numbers not starting with '2' Console.WriteLine(i); // write to console  • You can save 1 byte by using this pattern for the for loop: for(int i=0;++i<101;) – milk Oct 4, 2018 at 21:00 # Crystal, 65 bytes (1..100).each do |x| x.to_s().includes?('2') ? 0 : puts x end  Try it online! • Welcome to PPCG! You can golf this down to 49 bytes by removing some whitespace, and using the fact that do...end can be replaced by {...}: (1..100).each{|x|x.to_s.includes?('2')?0: puts x} If you use p x instead of puts x you can shave some more bytes Oct 5, 2018 at 14:37 • Im newer to crystal. So thanks, I didn't even know all that stuff! Oct 5, 2018 at 14:38 • @Dylan I would suggest to incorporate said golfing suggestions to improve your score. Oct 9, 2018 at 2:59 # Tidy, 27 bytes {{x:"2"!in x.0}from[1,100]}  Try it online! Function which returns the appropriate range. x.0 concatenates x and 0 as a string. # Haskell, 57 Bytes Solution import Data.List unlines.nub.map(filter(/='2').show).$[1..100]


or

import Data.List
nub.map(filter(/='2').show)$[1..100]  if you just want the list. Output Prelude Data.List> unlines.nub.map(filter(/='2').show)$[1..]
"1\n\n3\n4\n5\n6\n7\n8\n9\n10\n11\n13\n14\n15\n16\n17\n18\n19\n0\n30\n31\n33\n34\n35\n36\n37\n38\n39\n40\n41\n43\n44\n45\n46\n47\n48\n49\n50\n51\n53\n54\n55\n56\n57\n58\n59\n60\n61\n63\n64\n65\n66\n67\n68\n69\n70\n71\n73\n74\n75\n76\n77\n78\n79\n80\n81\n83\n84\n85\n86\n87\n88\n89\n90\n91\n93\n94\n95\n96\n97\n98\n99\n100


Explanation

Makes all numbers a string, removes their '2's and weeds out the duplicates.

• Hello and welcome to PPCG. We usually add all necessary imports to the byte count, here import Data.List. However, you can declare that your program is not written in plain Haskell, but in some variant that includes stuff by default, e.g. Haskell (lambdabot).
– nimi
Oct 6, 2018 at 14:36
• Additionally to what @nimi mentioned, your answer is not valid because your program lists values over 100 too, it will print two newlines after 1 and a 0 after 19. Oct 6, 2018 at 16:17

# Ruby, 33 bytes

### Solution:

puts (?1..'100').reject{|x|x[?2]}


# JavaScript (Node.js), 45 44 bytes

-1 byte thanks to Neil

for(i=0;i++<100;)/2/.test(i)||console.log(i)


Try it online!

• Can you not use || instead of ?0:?
– Neil
Oct 7, 2018 at 20:33