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

2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

Jelly, 7 bytes

³D2eṆƲƇ

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

Scala, 56 48 47 45 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)
\$\endgroup\$
1
\$\begingroup\$

Pip, 9 bytes

2NI_FI\,h

Use any of the flags -l, -n, -s, -p to get nice-looking output. Try it online!

Explanation

        h  Preset variable: 100
      \,   1-based range: [1 2 3 ... 100]
    FI     FIlter on this function:
2NI_        2 is Not In the argument
           Print the resulting list (implicit)
\$\endgroup\$
1
\$\begingroup\$

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.

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

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

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

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!

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

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

MBASIC, 59 bytes

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.

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

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

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.

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

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.

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

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.

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

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
\$\endgroup\$
  • \$\begingroup\$ I don't know much about APL (much less this specific variant), but aren't the instructions typically single bytes from a different codepage? \$\endgroup\$ – Jo King Oct 4 '18 at 5:06
  • \$\begingroup\$ @Joking in this case 'codepage' is 2 bytes for one char \$\endgroup\$ – RosLuP Oct 4 '18 at 5:11
1
\$\begingroup\$

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

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
\$\endgroup\$
  • 1
    \$\begingroup\$ Use globals to save one byte: TIO \$\endgroup\$ – oktupol Oct 4 '18 at 13:52
1
\$\begingroup\$

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.

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

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!

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

LUA, 113 68 47 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!

\$\endgroup\$
  • \$\begingroup\$ “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. \$\endgroup\$ – manatwork Oct 5 '18 at 9:58
  • \$\begingroup\$ Welcome to PPCG! You can golf this down to 47 bytes using a for loop and a ternary statement instead of an if \$\endgroup\$ – Jo King Oct 5 '18 at 10:02
  • \$\begingroup\$ 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). \$\endgroup\$ – ouflak Oct 5 '18 at 10:03
  • \$\begingroup\$ 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 :) \$\endgroup\$ – Jo King Oct 5 '18 at 10:05
  • \$\begingroup\$ 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... \$\endgroup\$ – ouflak Oct 5 '18 at 10:08
1
\$\begingroup\$

[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
\$\endgroup\$
  • 2
    \$\begingroup\$ You can save 1 byte by using this pattern for the for loop: for(int i=0;++i<101;) \$\endgroup\$ – milk Oct 4 '18 at 21:00
1
\$\begingroup\$

Crystal, 65 bytes

(1..100).each do |x|
    x.to_s().includes?('2') ? 0 : puts x
end

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ – Conor O'Brien Oct 5 '18 at 14:37
  • 1
    \$\begingroup\$ Im newer to crystal. So thanks, I didn't even know all that stuff! \$\endgroup\$ – Dylan Oct 5 '18 at 14:38
  • \$\begingroup\$ @Dylan I would suggest to incorporate said golfing suggestions to improve your score. \$\endgroup\$ – Jonathan Frech Oct 9 '18 at 2:59
1
\$\begingroup\$

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.

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

C# with only Linq, 170/184 bytes

using System.Linq;class a{static void Main(string[]b)=>Enumerable.Range(1,100).ToList().ForEach(x=>System.Console.Write(x.ToString().Contains("2")?"":x.ToString()+" "));}

while this code does work, dotnetfiddle requires both the class and the main method to be public, so that the total is 14 bytes more.

Try it Online!

\$\endgroup\$
  • \$\begingroup\$ It's way shorter not to use LINQ or ToString(): public class a{public static void Main(){for(var x=0;++x<101;)System.Console.Write((""+x).Contains("2")?"":x+" ");}} \$\endgroup\$ – milk Oct 4 '18 at 20:51
  • \$\begingroup\$ @milk huh, you're right, I actually didn't even consider that... I'll change the title to C# Linq then. \$\endgroup\$ – user71809 Oct 5 '18 at 17:43
1
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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). \$\endgroup\$ – nimi Oct 6 '18 at 14:36
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – ბიმო Oct 6 '18 at 16:17
1
\$\begingroup\$

Ruby, 33 bytes

Solution:

puts (?1..'100').reject{|x|x[?2]}
\$\endgroup\$
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\$

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.