57
\$\begingroup\$

Background

When I was in elementary school, we used to play a game in math class that goes as follows.

All kids sit in a big circle and take turns counting, starting from 1.

However, the following numbers must be skipped while counting:

  • Numbers that are multiples of 3.
  • Numbers that have a 3 in its decimal representation.

The first 15 numbers the kids should say are

1 2 4 5 7 8 10 11 14 16 17 19 20 22 25

Whenever somebody gets a number wrong – says a number that isn't in the sequence or skips a number that is – he's removed from the circle. This goes on until there's only one kid left.

Task

You're bad at this game, so you decide to cheat. Write a program or a function that, given a number of the sequence, calculates the next number of the sequence.

You don't have to handle numbers that cannot be represented using your language's native numeric type, provided that your program works correctly up to input 251 and that your algorithm works for arbitrarily large inputs.

Input and output can use any convenient base.

Since you have to conceal your code, it must be as short as possible. In fact, this is , so the shortest code in bytes wins.

Test cases

  1 ->   2
  2 ->   4
 11 ->  14
 22 ->  25
 29 ->  40
251 -> 254
\$\endgroup\$
13
  • 7
    \$\begingroup\$ I feel like we had a challenge like this... \$\endgroup\$ Nov 3, 2016 at 2:06
  • 7
    \$\begingroup\$ It was always 7 that was skipped when I played it, but you'd say something else, instead, rather than going to the next number in line. \$\endgroup\$
    – mbomb007
    Nov 3, 2016 at 13:51
  • 15
    \$\begingroup\$ @mbomb007: When I played it, you would not be removed from the circle. Instead, you would drink. But that wasn't in the elementary school. Anyway, getting over 80 was near impossible, especially after the first hour. \$\endgroup\$
    – tomasz
    Nov 3, 2016 at 21:49
  • 12
    \$\begingroup\$ @mbomb007 Depends on the blood alcohol concentration. \$\endgroup\$
    – Dennis
    Nov 4, 2016 at 14:16
  • 5
    \$\begingroup\$ @mbomb007: That would depend on the proof of whatever you are drinking. \$\endgroup\$
    – tomasz
    Nov 4, 2016 at 14:16

59 Answers 59

25
\$\begingroup\$

Brachylog, 10 bytes

<.='e3:I'*

Try it online!

Explanation

(?)<.                Output > Input
    .=               Assign a value to the Output
    . 'e3            3 cannot be an element of the Output (i.e. one of its digits)
        3:I'*(.)     There is no I such that 3*I = Output
\$\endgroup\$
2
  • 3
    \$\begingroup\$ Answers like this are so beautiful in Brachylog :) \$\endgroup\$
    – Emigna
    Nov 3, 2016 at 13:40
  • 3
    \$\begingroup\$ @Emigna It almost doesn't feel golfy enough sometimes because it basically describes the challenge directly. That's the case for a lot of answers in that language :) \$\endgroup\$
    – Fatalize
    Nov 3, 2016 at 13:41
15
\$\begingroup\$

JavaScript (ES6), 30 bytes

f=n=>++n%3*!/3/.test(n)?n:f(n)
\$\endgroup\$
3
  • \$\begingroup\$ Both index 2 and index 3 return the number 4 with this function \$\endgroup\$
    – nl-x
    Nov 3, 2016 at 16:11
  • 1
    \$\begingroup\$ @nl-x Yes, because 4 is the next number in the sequence after both 2 and 3. It's not indexed; it's simply the next number in the sequence. \$\endgroup\$ Nov 3, 2016 at 16:12
  • \$\begingroup\$ I think I'm starting to understand it... My bad \$\endgroup\$
    – nl-x
    Nov 3, 2016 at 16:14
9
\$\begingroup\$

J, 24 bytes

3(]0&({$:)~e.&":+.0=|)>:

Straight-forward approach that just iterates forward from input n until it finds the next number that is valid by the rules.

Forms five smileys, $:, :), 0=, =|, and >:.

Usage

   f =: 3(]0&({$:)~e.&":+.0=|)>:
   (,.f"0) 1 2 11 22 29 251
  1   2
  2   4
 11  14
 22  25
 29  40
251 254

Explanation

3(]0&({$:)~e.&":+.0=|)>:  Input: integer n
                      >:  Increment n
3                         The constant 3
 (                   )    Operate dyadically with 3 (LHS) and n+1 (RHS)
                    |       Take (n+1) mod 3
                  0=        Test if equal to 0
             &":            Format both 3 and n+1 as a string
           e.               Test if it contains '3' in str(n+1)
                +.          Logical OR the results from those two tests
  ]                         Right identity, gets n+1
   0&(   )~                 If the result from logical OR is true
       $:                     Call recursively on n+1
      {                       Return that as the result
                            Else act as identity function and return n+1
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Well, J is probably the most smiley-prone programming language. \$\endgroup\$
    – Adám
    Dec 12, 2016 at 0:24
8
\$\begingroup\$

Python 2, 73 66 43 bytes

Thanks to xnor for telling me I was being silly by using 2 variables, and thanks to Mitch Schwartz too.

x=~input()
while'3'[:x%3]in`x`:x-=1
print-x
\$\endgroup\$
11
  • 1
    \$\begingroup\$ The two-variable updating looks much too complicated. I think you just need x=input()+1 while'3'[:x%3]in`x`:x+=1 print x. \$\endgroup\$
    – xnor
    Nov 3, 2016 at 5:15
  • \$\begingroup\$ @xnor, oh yeah silly me I don't know why I did that \$\endgroup\$
    – Daniel
    Nov 3, 2016 at 11:32
  • \$\begingroup\$ One byte improvement by starting with x=~input(), subtracting instead of adding, and printing -x. \$\endgroup\$ Nov 3, 2016 at 14:39
  • 1
    \$\begingroup\$ @Artyer That's only 1 of the 3 mistakes introduced in that edit. \$\endgroup\$ Nov 3, 2016 at 19:21
  • 1
    \$\begingroup\$ @Dopapp The current revision (Without the space) is 43 bytes? mothereff.in/… \$\endgroup\$
    – Artyer
    Nov 3, 2016 at 20:52
8
\$\begingroup\$

Perl, 19 bytes

18 bytes code + 1 for -p.

++$_%3&&!/3/||redo

Usage

perl -pe '++$_%3&&!/3/||redo' <<< 8
10

perl -pe '++$_%3&&!/3/||redo' <<< 11
14
\$\endgroup\$
7
  • 1
    \$\begingroup\$ @dan1111 It's Perl, what did you expect? Clarity? \$\endgroup\$ Nov 4, 2016 at 14:46
  • 1
    \$\begingroup\$ @EriktheGolfer what? This is the very definition of "self-documenting code". \$\endgroup\$
    – user7486
    Nov 4, 2016 at 14:47
  • \$\begingroup\$ @dan1111 It seems you know Perl. I have no idea of how Perl works, because of its famous weirdness. \$\endgroup\$ Nov 4, 2016 at 14:50
  • \$\begingroup\$ @dan1111 Thanks! Quite happy with how short it turned out! \$\endgroup\$ Nov 4, 2016 at 16:58
  • 1
    \$\begingroup\$ @DomHastings Well, in PPCG, we take Perl as the top level of weirdness, and Jelly/Actually/O5AB1E as the top level of mess. It seems that you haven't ever seen this challenge then :) \$\endgroup\$ Nov 4, 2016 at 17:05
7
\$\begingroup\$

05AB1E, 11 bytes

[>Ð3ås3Ö~_#

Try it online!

Explanation

               # implicit input
[              # start loop
 >             # increase current number
  Ð            # triplicate
          #    # break loop IF
         _     # logical negation of
   3å          # number has one or more 3's in it
        ~      # OR
     s3Ö       # number % 3 == 0
\$\endgroup\$
6
\$\begingroup\$

Java 8, 57 56 55 50 bytes

Thanks to @Numberknot for 1 byte Thanks to @Kevin Cruijssen for 5 bytes

i->{for(;++i%3<1|(i+"").contains("3"););return i;}

This is a Function<Integer, Integer>

Explanation

Naive implementation that simply increments until it reaches an acceptable number.

Test Class

public class CodeGolf {

    public static void main(String[] args) {
        Function<Integer, Integer> countingGame = i->{for(;++i%3<1|(i+"").contains("3"););return i;};
        int val = 1;
        for (int i = 0; i < 10; i++) {
            System.out.print(val + " ");
            val = countingGame.apply(val);
        }
    }

}

Output of Test Class:

1 2 4 5 7 8 10 11 14 16
\$\endgroup\$
4
  • 2
    \$\begingroup\$ You can use | instead of || \$\endgroup\$
    – Numberknot
    Nov 3, 2016 at 4:13
  • 1
    \$\begingroup\$ @Numberknot I had no idea bitwise operators functioned as logical ones in some contexts! Thanks! \$\endgroup\$ Nov 3, 2016 at 10:43
  • 1
    \$\begingroup\$ Why the do-while? Just a regular for-loop is shorter: i->{for(;++i%3<1|(i+"").contains("3"););return i;} (50 bytes) \$\endgroup\$ Nov 3, 2016 at 11:08
  • \$\begingroup\$ @KevinCruijssen Well... I thought of comparing while and do-while, and they both gave me the same score, but I liked the way do-while looked... I didn't think of using a for loop... Thanks! \$\endgroup\$ Nov 3, 2016 at 11:33
6
\$\begingroup\$

Python 2, 49 44 42 bytes

f=lambda x:'3'[:~x%3]in`~x`and f(x+1)or-~x

The other Python entry beats this (edit: not any more :-D), but I posted it because I rather like its recursive approach. Thanks to Mitch Schwarz and Erik the Golfer for helping me make this shorter.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ You can do this in Python 2: f=lambda x:f(x+1)if x%3>1or'3'in`x+1`else-~x. If you want to keep Python 3, you can golf the last x+1 to -~x and remove the space. \$\endgroup\$ Nov 4, 2016 at 14:41
  • \$\begingroup\$ @EriktheGolfer Thanks! I'll change it to Python 2, as it's way shorter. \$\endgroup\$
    – 0WJYxW9FMN
    Nov 4, 2016 at 17:28
  • \$\begingroup\$ 42s: f=lambda x:'3'[:~x%3]in`~x`and f(x+1)or-~x and f=lambda x:f(x+1)if'3'[:~x%3]in`~x`else-~x \$\endgroup\$ Nov 4, 2016 at 17:56
5
\$\begingroup\$

Japt, 18 bytes

°U%3*!Us f'3 ?U:ßU

Test it online

I finally have a chance to use ß :-)

How it works

                    // Implicit: U = input integer
°U%3                // Increment U, and take its modulo by 3.
     !Us f'3        // Take all matches of /3/ in the number, then take logical NOT.
                    // This returns true if the number does not contain a 3.
    *               // Multiply. Returns 0 if U%3 === 0  or the number contains a 3.
             ?U     // If this is truthy (non-zero), return U.
               :ßU  // Otherwise, return the result of running the program again on U.
                    // Implicit: output last expression
\$\endgroup\$
0
5
\$\begingroup\$

PowerShell v2+, 46 bytes

for($a=$args[0]+1;$a-match3-or!($a%3)){$a++}$a

Takes input $args[0], adds 1, saves into $a, starts a for loop. The conditional keeps the loop going while either $a-match3 (regex match) -or $a%3 is zero (the ! of which is 1). The loop simply increments $a++. At the end of the loop, we simply place $a on the pipeline, and output via implicit Write-Output happens at program completion.

Examples

PS C:\Tools\Scripts\golfing> 1,2,11,22,29,33,102,251,254|%{"$_ --> "+(.\count-without-three.ps1 $_)}
1 --> 2
2 --> 4
11 --> 14
22 --> 25
29 --> 40
33 --> 40
102 --> 104
251 --> 254
254 --> 256
\$\endgroup\$
4
\$\begingroup\$

R, 46 bytes

n=scan()+1;while(!n%%3|grepl(3,n))n=n+1;cat(n)
\$\endgroup\$
1
  • \$\begingroup\$ I think that returning a value (rather than printing to stdout) is allowed, so you can save 5 bytes by having just n instead of cat(n). \$\endgroup\$
    – rturnbull
    Nov 4, 2016 at 14:52
4
\$\begingroup\$

Haskell, 50 48 bytes

f n=[x|x<-[n..],mod x 3>0,notElem '3'$show x]!!1

Try it on Ideone. Saved 2 bytes thanks to @Charlie Harding.

Alternative: (50 bytes)

g=f.(+1)
f n|mod n 3<1||(elem '3'.show)n=g n|1<3=n
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Also 50 bytes: until(\x->mod x 3>0&&notElem '3'(show x))succ.succ. \$\endgroup\$
    – nimi
    Nov 3, 2016 at 13:51
4
\$\begingroup\$

Labyrinth, 117 102 bytes

?       """""""""""_
):_3    (         0/{!@
;  %;:}_';:_3-_10 1
"  1            %;_
""""_""""""""{;;'

Try it online!

Labyrinth is a two-dimensional, stack-based programming language and at junctions, direction is determined by the top of the stack (positive goes right, negative goes left, zero goes straight). There are two main loops in this programs. The first mods the integer input by 3 and increments if 0. The second repeatedly checks if the last digit is 3 (by subtracting 3 and modding by 10) and then dividing by 10 to get a new last digit.

\$\endgroup\$
3
\$\begingroup\$

Lua, 58 Bytes

i=...+1while(i%3==0or(i..""):find"3")do i=i+1 end print(i)
\$\endgroup\$
3
\$\begingroup\$

Pyke, 13 bytes

Whii3%!3`i`{|

Try it here!

              - i = input
W             - do:
 hi           -  i += 1
   i3%!       -    not (i % 3)
            | -   ^ or V
       3`i`{  -    "3" in str(i)
              - while ^
\$\endgroup\$
2
  • 1
    \$\begingroup\$ At first I though this said while at the beginning. \$\endgroup\$ Nov 3, 2016 at 18:37
  • \$\begingroup\$ If you glance at it I can see that \$\endgroup\$
    – Blue
    Nov 3, 2016 at 18:38
3
\$\begingroup\$

C#, 56, 51 bytes.

This is surprisingly short for a C# answer!

x=>{while(++x%3<1|(x+"").Contains("3"));return x;};
\$\endgroup\$
5
  • \$\begingroup\$ You can get it down to 43 if you make it recursive t=x=>(++x)%3<1|(x+"").Contains("3")?t(x):x; In Visual Studio, you just need to define the variable and set it to null Func<int, int> t = null; and then define the recursive function on the following line. \$\endgroup\$
    – Grax32
    Nov 3, 2016 at 17:17
  • 1
    \$\begingroup\$ The problem is that if I make it recursive, I then have to count the function and type definitions. \$\endgroup\$ Nov 3, 2016 at 17:19
  • \$\begingroup\$ Is there somewhere I can go to see these guidelines? I find C# golfing confusing on here. \$\endgroup\$
    – Grax32
    Nov 3, 2016 at 17:21
  • \$\begingroup\$ @Grax Basically, you need to include any code required for the code to run except the assignment to a name in the case of a non-recursive function. I don't know where you would find a concrete set of guidelines, unfortunately. \$\endgroup\$ Nov 3, 2016 at 17:24
  • \$\begingroup\$ @MorganThrapp please check out my c# answer with recursion at 49 bytes :) \$\endgroup\$
    – lee
    Mar 2, 2018 at 6:26
3
\$\begingroup\$

Pyth, 11 bytes

f&-I`T3%T3h

Try it online: Demonstration or Test Suite

Explanation:

f&-I`T3%T3hQ   implicit Q at the end
f         hQ   find the smallest integer T >= input + 1 which fulfills:
  -I`T3           T is invariant under removing the digit 3
 &                and
       %T3        T mod 3 leaves a positive remainder
\$\endgroup\$
3
\$\begingroup\$

Excel, 79 66 bytes

It Taylor Rained 13 bytes

=MIN(LET(r,ROW(A:A),FILTER(r,ISERR(FIND(3,r))*MOD(r,3)*(r>A1)>0)))

Input is in the cell A1. Output is wherever the formula is.

The LET() function allows you to define variables for later reference so ti works in pairs of variable,value. The last argument is unpaired as it is the output.

  • r,ROW(A:A) creates an array of 1 to whatever the last row is. In the latest version of Excel, that's 1,048,576.
  • g,FILTER(r,~*~*~>0) takes that array and filters just for the values that aren't multiples of 3 and don't have a 3 in them. This done is by multiplying:
    • ISERR(FIND(3,r)) which returns TRUE if if a 3 is not in the number and FALSE if it is
    • MOD(r,3) which returns 0 if it's a multiple of 3 and some positive integer if it isn't
    • (r>A1) creates an array of TRUE / FALSE where the first TRUE value will be the first term that's larger than the input.
    • When you do math on booleans, Excel uses TRUE=1 and FALSE=0 so the formula ~*~*~>0 only returns TRUE when the three pieces are TRUE (no 3 in number), some positive integer (not a multiple of 3), and TRUE (larger than the input).
  • MIN(LET(~)) finds the smallest value in the filtered array which only includes the "without 3" numbers larger than the input so the first value in the array is the next value in the sequence.

Screenshot

\$\endgroup\$
2
  • \$\begingroup\$ Would =MIN(LET(r,ROW(A:A),FILTER(r,ISERR(FIND(3,r))*MOD(r,3)*(r>A1)>0))) work for all tests? \$\endgroup\$ May 3, 2022 at 2:13
  • \$\begingroup\$ Nice, not sure if appreciated but for 64 bytes you can use: =LET(r,ROW(A:A),MATCH(1,ISERR(FIND(3,r))*(MOD(r,3)>0)*(r>K1),0)) \$\endgroup\$
    – JvdV
    Sep 5, 2022 at 18:10
3
\$\begingroup\$

Python 3, 49 48 bytes

-3 bytes thx to The Thonnu, I missed some spaces and swapping else x+1 and else-~x allowed another space to be taken out, not

+2 bytes bc I didn't know I need to count f= on a recursive function, thx xnor :)

seems clunky but i cant see more golfs, please lmk if you see something!

f=lambda x:f(x+1)if x%3>1or'3'in str(x+1)else-~x

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I think you can remove the spaces between 1 or and str(x+1) else \$\endgroup\$ Jun 29, 2023 at 23:37
  • 1
    \$\begingroup\$ 46 bytes \$\endgroup\$
    – The Thonnu
    Jun 30, 2023 at 5:22
  • 1
    \$\begingroup\$ You need to count the f= because your function is recursive. \$\endgroup\$
    – xnor
    Jun 30, 2023 at 5:57
2
\$\begingroup\$

GolfSharp, 43 bytes

m=>r(m,m).w(n=>n%3>0&!n.t().I("3")).a()[1];
\$\endgroup\$
2
\$\begingroup\$

Ruby, 47 bytes

i=gets.to_i;i while(i+=1)%3==0||"#{i}"=~/3/;p i

I really feel like this can be golfed further.

\$\endgroup\$
1
  • \$\begingroup\$ you can use i instead of "#{i}" \$\endgroup\$
    – Mohammad
    Nov 5, 2016 at 7:21
2
\$\begingroup\$

MATL, 14 bytes

`Qtt3\wV51-hA~

Try it online!

Explanation

`       % Do...while
  Q     %   Add 1. Takes input implicitly in the first iteration
  tt    %   Duplicate twice
  3\    %   Modulo 3
  wV    %   Swap, string representation
  51-   %   Subtract 51, which is ASCII for '3'
  h     %   Concatenate
  A~    %   True if any result was 0. That indicates that the number
        %   was a multiple of 3 or had some '3' digit; and thus a 
        %   new iteration is needed
\$\endgroup\$
2
\$\begingroup\$

PHP, 60 55 54 46 bytes

Thanks to @user59178 for shaving off a few bytes, @AlexHowansky for a byte, @Titus for another few bytes

for(;strstr($i=++$argv[1],51)|$i%3<1;);echo$i;

Called from command line with -r. Naive method that loops while the number is a multiple of 3, or has 3 in its digits.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ You can save 7 bytes by just using a program taking input from the command line rather than a function: for($i=$argv[1];!(++$i%3)|strpos(" $i",'3'););echo$i; it may be possible to do better by assigning $i while using it too. \$\endgroup\$
    – user59178
    Nov 3, 2016 at 16:39
  • \$\begingroup\$ @user59178 I assumed the function had to return $i \$\endgroup\$
    – Xanderhall
    Nov 3, 2016 at 19:14
  • \$\begingroup\$ most of the time questions are pretty flexible in how input and output is done, as long as the right thing is given and received. Besides, looking at answers in other languages, most choose to print to stdout. \$\endgroup\$
    – user59178
    Nov 4, 2016 at 9:03
  • \$\begingroup\$ Save a byte with strpos(_.$i,'3') \$\endgroup\$ Nov 4, 2016 at 18:12
  • \$\begingroup\$ Save one byte with %3<1, one with 51 instead of '3', two more with strstr($i) instead of strpos(_.$i) and another two by swapping the | operands in the second version: <?for(;strstr($i=++$argv[1],51)|$i%3<1;);echo$i; -> 48 bytes \$\endgroup\$
    – Titus
    Dec 6, 2016 at 16:15
2
\$\begingroup\$

Jelly, 11 bytes

D;Æf3ḟµ‘#2ị

Try it online!

How it works

D;Æf3ḟµ‘#2ị  Main link. Argument: n

      µ      Combine the links to the left into a chain.
       ‘#    Execute the chain for k = n, n + 1, n + 2, ... until n + 1 matches
             were found. Yield the array of all n + 1 matches.
D            Decimal; yield the array of k's decimal digits.
  Æf         Yield the array of k's prime factors.
 ;           Concatenate both.
    3ḟ       Filter false; remove digits and factors from [3].
             This yields [3] (truthy) if neither digits nor factors contain 3,
             [] (falsy) if they do.
         2ị  Extract the second match. (The first match is n.)
\$\endgroup\$
2
\$\begingroup\$

PHP, 47 41 bytes

inspired by Xanderhall, but the latest idea finally justifies an own answer.

while(strstr($n+=$n=&$argn%3,51));echo$n;

or

while(strpbrk($n+=$n=&$argn%3,3));echo$n;

This takes advantage from the fact that the input is also from the sequence: For $n%3==1, the new modulo is 2. For $n%3==2, the new modulo is 4-3=1. $n%3==0 never happens.

Run as pipe with -R or try them online.

\$\endgroup\$
2
+100
\$\begingroup\$

APL (Dyalog Unicode), 33 28 27 19 bytesSBCS

1∘+⍣{('3'∊⍕⍺)<×3|⍺}

Try it online!

-6 thanks to Adám. -8 thanks to ngn.

Old Explanation:

1-⍨g⍣((×3|⊢)>'3'∊⍕)∘(g←+∘1)
                       +∘1  ⍝ curry + with 1, gives the increment function
                            ⍝ increments the left argument so we do not return the number itself
                    (g←   ) ⍝ assign to "g"
                   ∘        ⍝ compose g with the repeat
                 ⍕          ⍝ does parsing the argument to a string...
             '3'∊           ⍝ ...contain '3'?
        3|⊢                 ⍝ residue of a division by 3
      (×   )                ⍝ direction (0 if 0, 1 if greater, ¯1 is lower)
     (      >     )         ⍝ and not (we want the left side to be 1, the right side 0)
   g⍣                       ⍝ repeat "g" (increment) until this function is true ^
1-⍨                         ⍝ afterwards, decrement: inversed -

APL (Dyalog Extended), 23 17 bytesSBCS

1∘+⍣(3(×⍤|>∊⍥⍕)⊣)

Try it online!

Thanks to Adám. -6 thanks to ngn.

Old Explanation:

0+⍣(3(×⍤|>∊⍥⍕)⊢)⍢(1+⊢)⊢
0                       ⍝ the left argument (⍺)
 +⍣(3(×⍤|>∊⍥⍕)⊢)        ⍝ the left function (⍺⍺)
                 (1+⊢)  ⍝ the right function (⍵⍵)
                        ⍝     (increments its argument)
                      ⊢ ⍝ the right argument (⍵)
                        ⍝     (just returns the input)
                ⍢       ⍝ under:
                        ⍝     calls (⍵⍵ ⍵) first, which increments the input
                        ⍝     also (⍵⍵ ⍺) which gives 1
                        ⍝     then calls (⍺incremented ⍺⍺ ⍵incremented)
                        ⍝     afterwards, does the opposite of ⍵⍵, and decrements the result
  ⍣                     ⍝ ⍣ fixpoint: repeats the left operation until the right side is truthy
 +                      ⍝ calls + with ⍺incremented and the input (so, 1+input)
   (3(×⍤|>∊⍥⍕)⊢)        ⍝ right operation
    3                   ⍝ on its left, "3"
              ⊢         ⍝ on its right, the current iteration
      ×⍤|               ⍝ divisibility check: × atop |
        |               ⍝     starts with 3|⊢ (residue of ⊢/3)
      ×                 ⍝     then returns the direction (0 if 0, 1 if greater, ¯1 is lower)
          ∊⍥⍕           ⍝ contains 3:
            ⍕           ⍝    stringifies both its arguments (3 and ⊢)
          ∊⍥            ⍝    checks for membership
         >              ⍝ divisibility "and not" contains 3
\$\endgroup\$
2
\$\begingroup\$

Turing Machine Code, 390 bytes

0 * * r 0
0 _ _ l 1
1 1 2 l 2
1 2 3 r 0
1 3 4 l 2
1 4 5 l 2
1 5 6 l 2
1 6 7 l 2
1 7 8 l 2
1 8 9 l 2
1 9 0 l 1
1 * 1 l 2
2 * * l 2
2 _ _ r 3
3 1 1 r 4
3 4 4 r 4
3 7 7 r 4
3 2 2 r 5
3 5 5 r 5
3 8 8 r 5
3 _ _ l 1
4 1 1 r 5
4 4 4 r 5
4 7 7 r 5
4 2 2 r 3
4 5 5 r 3
4 8 8 r 3    
5 1 1 r 3
5 4 4 r 3
5 7 7 r 3
5 2 2 r 4
5 5 5 r 4
5 8 8 r 4    
* 0 0 r *
* 6 6 r *
* 9 9 r *
* 3 3 r 0
* _ _ * halt

Try it online.

\$\endgroup\$
2
  • \$\begingroup\$ Can you explain this answer? \$\endgroup\$
    – sintax
    Mar 21, 2019 at 15:51
  • \$\begingroup\$ @sintax Added some explanatory comments at the provided link. \$\endgroup\$ Mar 23, 2019 at 2:12
2
\$\begingroup\$

Perl 6, 27 25 24 bytes

{max $_+1...{!/3/&$_%3}}

Try it online!

Finds the first number larger than the input that doesn't have a three and has a remainder when moduloed by 3. I was hoping to do something fancy with the condition, like !/3/&*%3 but it doesn't work with the !. :(

Explanation:

{                      }   # Anonymous code block
     $_+1                  # From the input+1
         ...               # Get the series
            {         }    # That ends when
             !/3/            # The number does not contain a 3
                 &           # and
                  $_%3       # The number is not divisible by 3
 max                       # And get the last element of the series
\$\endgroup\$
2
\$\begingroup\$

APOL, 51 bytes

v(0 +(⧣ 1));w(|(!(%(⁰ 3)) c(t(⁰) '3')) ∆(0));-(⁰ 1)

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

Swift, 84 bytes

func f(_ n:Int)->Int{var n=n+1;while String(n).contains("3")||n%3==0{n+=1};return n}
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Golf, and nice first submission! \$\endgroup\$
    – noodle man
    Dec 3, 2023 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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