22
\$\begingroup\$

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
\$\endgroup\$
  • 7
    \$\begingroup\$ I assume the winning criteria is code-golf right? \$\endgroup\$ – Luis felipe De jesus Munoz Oct 2 '18 at 13:26
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ – Arnauld Oct 2 '18 at 14:08
  • 1
    \$\begingroup\$ Might I suggest using the Sandbox in the future to get feedback on your challenges before posting? \$\endgroup\$ – Jo King Oct 2 '18 at 14:15
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Kevin Cruijssen Oct 2 '18 at 14:19
  • 4
    \$\begingroup\$ @user202729 Arbitrary restriction is unwelcome, not disallowed. \$\endgroup\$ – Jonathan Frech Oct 3 '18 at 1:11

67 Answers 67

1
\$\begingroup\$

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!

\$\endgroup\$
  • 2
    \$\begingroup\$ Can you not use || instead of ?0:? \$\endgroup\$ – Neil Oct 7 '18 at 20:33
1
\$\begingroup\$

JavaScript, 57 bytes

_=>`${[...Array(101).keys()]}`.replace(/^0,|\d?2.?,/g,'')

Try it online!

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

Windows Batch, 49 48 bytes

@for /l %%G in (1,1,100)do @echo %%G|find /v "2"

Loops from 1 to 100, and for each number looped, only output those who does not contain any "2".

1 byte is saved by @Neil.

\$\endgroup\$
  • \$\begingroup\$ You don't need that ~ do you? \$\endgroup\$ – Neil Oct 7 '18 at 20:35
  • \$\begingroup\$ Oh! You're right. It looks like I have forgotten how to code golf. \$\endgroup\$ – stevefestl Oct 9 '18 at 13:14
0
\$\begingroup\$

SmileBASIC, 50 bytes

@L
N=N+1IF.>INSTR(STR$(N),"2")THEN?N
ON N>99GOTO@L

Explanation:

@LABEL
 N = N + 1
 IF 0 > INSTR(STR$(N), "2") THEN PRINT N
ON N>99 GOTO @LABEL

INSTR(STR$(N), "2") is -1 if N doesn't contain a 2.

ON ... GOTO takes a list of labels, and jumps to a label in that list corresponding to the value of an expression.
If N isn't greater than 99, N>99 returns 0 so it jumps to the 0th label, @LABEL.
Otherwise, N>99 is 1 so it tries to jump to label 1 in the list, but since the list isn't long enough, it doesn't jump and the program ends.

ON is meant to be used like switch/case, with a longer list of labels. But ON x GOTO @LABEL can be used as a shorter way of writing IF !(x) GOTO @LABEL:

IF N<=99GOTO@L
IF N<100GOTO@L
ON N>99GOTO@L
\$\endgroup\$
0
\$\begingroup\$

ink, 34 bytes

-(i){("{i}"!?"2"):{i}}
{i<100:->i}

Try it online!

Explanation

-(i)             // Labelled gather - keeps track of how many times it's been visited (in variable i)
{"{i}"!?"2":     // If "2" is not a substring of i...
{i}              // ...print i
}
{i<100:->i}      // If i is less than 100, go back to i.
\$\endgroup\$
0
\$\begingroup\$

Vim, 18 bytes/keystrokes

100o0<esc>V{g<C-a>J:g/2/d<cr>

Try it online!

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

C++, 84 81 bytes

-3 bytes thanks to ceilingcat

The function needs a void return value to compile with MSVC

#include<cstdio>
void f(){for(int i=0;++i<101;)i%10-2&&i/10-2&&printf("%d\n",i);}

The if read as "if the first digit isn't 2 and the second digit isn't 2"

\$\endgroup\$

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.