58
\$\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\$ Commented 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
    Commented 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
    Commented Nov 3, 2016 at 21:49
  • 12
    \$\begingroup\$ @mbomb007 Depends on the blood alcohol concentration. \$\endgroup\$
    – Dennis
    Commented Nov 4, 2016 at 14:16
  • 5
    \$\begingroup\$ @mbomb007: That would depend on the proof of whatever you are drinking. \$\endgroup\$
    – tomasz
    Commented Nov 4, 2016 at 14:16

59 Answers 59

1
2
2
\$\begingroup\$

Google Sheets, 63 bytes

=+let(n,sequence(254),filter(n,n>A1,mod(n,3),iserr(find(3,n))))

Put the input in cell A1 and the formula in B1.

The filter() gets all matching values above the value in A1 and the unary plus + gets the first value from that list.

\$\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\$ Commented Dec 3, 2023 at 15:29
1
\$\begingroup\$

C, 81 bytes

f(int n){int m;l:if(++n%3){for(m=n;m>0;m/=10)if(m%10==3)goto l;return n;}goto l;}
\$\endgroup\$
1
\$\begingroup\$

reticular, 30 bytes

in v
?v$>1+d3,qds:3@cQm*
;\$o

Try it online!

Explanation

1: initialization

in v

This converts the input to a number, then goes down (v)

2: loop

?v$>1+d3,qds:3@cQm*
   >                 go right!              [n]
    1+               add 1                  [n+1]
      d3,            duplicate and mod 3    [n+1, (n+1)%3]
         qd          reverse and duplicate  [(n+1)%3, n+1, n+1]
           s         cast to string         [(n+1)%3, n+1, `n+1`]
            :3@c     count numbers of "3"   [(n+1)%3, n+1, `n+1`.count(3)]
                Qm*  negate and rotate      [n+1, continue?]
?v                   terminate if continue
  $                  drop continue

3: final

;\$o
 \$o  drop and output
;     terminate
\$\endgroup\$
1
\$\begingroup\$

Batch, 93 bytes

@set/pn=
:l
@set/an+=1,r=n%%3
@if %r%==0 goto l
@if not "%n:3=%"=="%n%" goto l
@echo %n%

Takes input on STDIN.

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

CJam, 19 bytes

ri{)__3%!\`'3e=e|}g

ONLINE

Explanation:

ri{)__3%!\`'3e=e|}g
r                   Get token
 i                  Convert to integer
  {              }  Block
   )                 Increment
    _                Duplicate
     _               Duplicate
      3              Push 3
       %             Modulo
        !            NOT gate
         \           Swap
          `          String representation
           '3        Push '3'
             e=      Count occurrences
               e|    OR gate
                  g While popped ToS is true

If a less verbose explanation was asked, I would have done this:

ri{)__3%!\`'3e=e|}g
ri                  Get integer
  {              }  Block
   )                 Increment
    __               Triplicate
      3%!            Test non-divisibility with 3
         \           Swap
          `'3e=      Count occurrences of '3' in string repr
               e|    OR gate
                  g While popped ToS is true
\$\endgroup\$
1
\$\begingroup\$

Pyth, 19 bytes

JhQW|!%J3/`J\3=hJ;J

Test suite

I am sure I can golf this... it's the same as my CJam answer.

Explanation:

JhQW|!%J3/`J\3=hJ;J
  Q                 Evaluated input
 h                  Increment
J                   Assign J to value
       J            Variable J
        3           Value 3
      %             Modulo
     !              Logical NOT
           J        Variable J
          `         String representation
            \3      Value "3"
         /          Count occurrences
    |               Logical OR
               h    Increment
                J   Variable J
              =     Apply function then assign
                 ;  End statement block
                  J Variable J
\$\endgroup\$
3
  • \$\begingroup\$ I posted a way shorter solution. Nevertheless here is a tip for your approach: Don't use the variable J. You can increment Q. And if your doing it clever, you can inline the operation into the while condition: W|!%=hQ3/Q\3;Q`. \$\endgroup\$
    – Jakube
    Commented Nov 8, 2016 at 19:01
  • \$\begingroup\$ Sorry: W|!%=hQ3/`Q\3;Q \$\endgroup\$
    – Jakube
    Commented Nov 8, 2016 at 19:07
  • \$\begingroup\$ @Jakube The variable is not just incrementing but thanks. \$\endgroup\$ Commented Nov 9, 2016 at 11:44
1
\$\begingroup\$

Clojure, 73 bytes

(fn c[n](let[m(inc n)](if(or(=(rem m 3)0)(some #(=\3 %)(str m)))(c m)m)))

Recursively loops while n is divisible by 3, or contains a 3 in its string representation. Although I'm using unoptimized recursion, it was able to handle 2999999 as an input, so it should be ok.

Ungolfed

(defn count-without-3 [n]
  (let [m (inc n)]
    (if (or (= (rem m 3) 0)
            (some #(= \3 %) (str m)))
      (count-without-3 m)
      m)))
\$\endgroup\$
1
\$\begingroup\$

Mathematica, 51 bytes

#+1//.t_/;t~Mod~3<1||!IntegerDigits@t~FreeQ~3:>t+1&

Try it online!

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

JavaScript, 35 bytes

f=n=>++n%3&&!(n+"").match(3)?n:f(n)

Try it online!

\$\endgroup\$
4
  • 4
    \$\begingroup\$ I am not an expert in JavaScript but this doesn't look very golfed. It looks like you have a lot of whitespace that could be removed. You might want to check out the tips page for JavaScipt. \$\endgroup\$
    – Wheat Wizard
    Commented Nov 3, 2016 at 20:29
  • \$\begingroup\$ Ah, sorry, First time on this stackexchange. I thought this was just a problem to solve. Didn't realize there were rules. I wasn't sure what "golfed" meant. \$\endgroup\$
    – Oliver
    Commented Nov 3, 2016 at 20:31
  • 4
    \$\begingroup\$ No need to apologize. All puzzles here have a "winning criterion", quite often, such as in this challenge, it is code golf. If you want more general puzzles look into the programming-puzzle tag. Welcome to PPCG I hope you have fun. \$\endgroup\$
    – Wheat Wizard
    Commented Nov 3, 2016 at 20:36
  • \$\begingroup\$ "Golfed" means you try to make the program as small as possible. Then you compete against other users to get the shortest Javascript answer. I would read through this page and edit your post if you can come up with a shorter solution. \$\endgroup\$
    – DJMcMayhem
    Commented Nov 3, 2016 at 21:26
1
\$\begingroup\$

Ruby, 29 bytes

->n{0until"#{n+=n%3}"!~/3/;n}

Try it online!

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

Stax, 9 bytes

ú╜H▌»NWì╤

Run and debug it

Unpacked, ungolfed, and commented it looks like this.

w       repeat rest of the program while the produced value is true
  ^c    increment and copy value
  E3#   (a) count number of digits that are 3
  n     copy incremented value
  3%!   (b) is multiple of 3?
  +     sum of (a) + (b) values

Run and debug this one

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

Forth (gforth), 81 bytes

: f begin 1+ >r i i 3 mod 1 r> begin 10 /mod >r 3 <> * r> ?dup 0= until * until ;

Try it online!

Explanation

  1. Add 1
  2. check if multiple of 3
  3. check if contains a 3
  4. if the result of step 3 or step 4 is true, repeat from step 1

Code Explanation

: f               \ start a new word definition
  begin           \ start an indefinite loop
    1+            \ add 1 to current number
    >r i i        \ put current number on return stack, then place on normal stack twice
    3 mod         \ check if number is multiple of 3
    1 r>          \ place a 1 on the stack, then move the current number to the normal stack
    begin         \ start another indefinite loop
      10 /mod     \ get quotient and remainder of dividing by 10
      >r          \ put quotient on return stack
      3 <>        \ check if remainder does not equal 3
      *           \ multiply by accumulator (cheaper version of "and") 
      r>          \ remove quotient from return stack
      ?dup 0=     \ if quotient does not equal 0, duplicate, else put -1 on stack
     until        \ end loop if top of stack does not equal 0
     *            \ multiply result of both checks (cheaper "and")
   until          \ end outer loop
 ;                \ end word definition
\$\endgroup\$
1
\$\begingroup\$

Ruby, 39 bytes

f=->n{n+=1;(n%3<1||n.to_s=~/3/)?f[n]:n}

Try it online!

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

Julia 1.0, 34 bytes

!n='3'∈"$(n+=1)"||n%3<1 ? !n : n

Try it online!

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

TI-BASIC, 36 35 bytes

Repeat fPart(Ans/3)max(3≠int(10fPart(Ans₁₀^(seq(-X-1,X,0,log(Ans:Ans+1:End:Ans

Input is in Ans.
Output is in Ans and is implicitly printed when the program completes.

Examples:

17
              17
prgmCDGFF
              19
299
             299
prgmCDGFF
             400

Explantion:
(Newlines added for readability. They do not appear in the source code.)

Repeat                                              ;loop until...
  fPart(Ans/3)                                      ; the current number is not
                                                    ;  a multiple of 3
  max(3≠int(10fPart(Ans₁₀^(seq(-X-1,X,0,log(Ans     ; and until it does not
                                                    ;  contain any 3s
Ans+1                                               ;add 1 to the current number
End
Ans                                                 ;leave the current number
                                                    ; in "Ans" and implicitly
                                                    ; print it

Note: TI-BASIC is a tokenized language. Character count does not equal byte count.

\$\endgroup\$
3
  • \$\begingroup\$ -1 byte by removing the and (A and B ~ A*B = AB) [I think] \$\endgroup\$
    – MarcMush
    Commented May 4, 2022 at 14:36
  • 1
    \$\begingroup\$ @MarcMush and is a 1-byte token, so AB would be the only byte save of the two suggestions (A*B vs AB). Unless I misread your comment, in which AB would indeed be a byte saved. \$\endgroup\$ Commented May 4, 2022 at 16:43
  • \$\begingroup\$ yes, that's what I meant \$\endgroup\$
    – MarcMush
    Commented May 4, 2022 at 23:59
1
\$\begingroup\$

Vyxal, 10 bytes

λ›D₃$3c∨[x

Try it Online!

It would be 9 bytes with (parallel apply), but that seems to clear the stack, so that didn't work.

How?

λ›D₃$3c∨[x
λ           # Open a lambda (for recursion)
 ›          # Increment
  D         # Triplicate, push three copies of a to the stack
   ₃        # Is it divisible by three?
    $       # Swap
     3c     # Does the number contain a three?
       ∨    # Is either of the top two things on the stack true?
        [x  # Then recurse
\$\endgroup\$
1
\$\begingroup\$

Thunno 2 +, 11 bytes

(D3Ḋsd3Ƈ|;⁺

Attempt This Online!

Explanation

(D3Ḋsd3Ƈ|;⁺  # Implicit input
             # Increment
(            # While loop
 D           # (condition)  Duplicate
  3Ḋ         #              Divisible by 3?
    s        #              Swap
     d       #              Digits
      3Ƈ     #              Contains 3?
        |    #              Logical OR
         ;⁺  # (body)  Increment
             # Implicit input
\$\endgroup\$
1
\$\begingroup\$

Japt, 12 bytes

_%3«Zsø3}a°U

Run it online

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

Go, 100 bytes

import(."strings";."fmt")
func f(n int)int{i:=n+1
for;i%3<1&&Contains(Sprint(i),"3");i++{}
return i}

Try it online!

\$\endgroup\$
1
+200
\$\begingroup\$

Vyxal 3, 9 bytes

э∥3c3Ḋ∵¬Ẇ

Try it Online!

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

C#, 49 bytes

int A(int n)=>(++n+"").IndexOf('3')*n%3<0?n:A(n);

Slightly different and shorter than the above c#, using recursion and another trick I came up with to condense the two conditions into one

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

MathGolf, 10 9 bytes

ö)_3‼╧÷+▲

Try it online!

Explanation

The program loops until it finds a number that is neither divisible by 3 or contains the number 3. is a new addition to MathGolf, and is newer than this challenge (and this answer). It has been an idea I had in mind for a while, but I finally got everything together to implement it.

ö           start block of length 7
 )          increment
  _         duplicate TOS
   3        push 3
    ‼       apply next two operators separately to the stack
     ╧      pop a, b, a.contains(b)
      ÷     is divisible
       +    pop a, b : push(a+b) (works as a logical OR)
        ▲   do while true with pop
\$\endgroup\$
0
\$\begingroup\$

Perl 6, 39 bytes

{my$a=$_+1;$a++while $a%%3||$a~~/3/;$a}
\$\endgroup\$
0
\$\begingroup\$

BotEngine, 770 107x7 = 749 (740 bytes)

This can almost certainly be made rather more compact than it currently is, but I finally managed to get it working at least.

v  0 1 2 3 4 5 6 7 8 9 $ $   0 1 2 3 4 5 6 7 8 9 $  0 1 2 3 4 5 6 7 8 9 $> 0 1 2 3 4 5 6 7 8 9 $v$ $3
I>>S S S S S S S S S S S>S$v>S S S S S S S S S S S >S S S S S S S S S S S >S S S S S S S S S S S>e>SS~v
R  e1e2e3e4e5e6e7e8e9e0e1>e ^e0e1e2e3e4e5e6e7e8e9e$ e0e1e2e3e4e5e6e7e8e9>^ e0e1e2e3e4e5e6e7e8e9>^  Re3  $
$e^                  < e$   ^<     <     <     <        <     <     <        <     <     <         P>  >S~v
>^ >>>>>>>>>>>>>>>>>>>>>^ ~<   >     >     >       ^<     <     <     <        <     <     <      ^   <^  <
                                 >     >     >        >     >     >       ^<     <     <     <          e$
  ^                                              <                                                      <

Input and output is in standard big-endian decimal, although most of the program uses little-endian internally to make the increment cycle more straightforwards.

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

Binary-Encoded Golfical, 80 bytes

This binary-encoded format can be converted to the standard graphical version using the encoder utility included in the github repo, or run directly using the interpreter by adding the -x flag.

Hexdump of binary encoding:

01 70 03 15 14 14 1b 1a 14 04 01 14 14 2c 14 14
00 03 14 14 0a 01 14 14 2f 14 14 53 2d 14 23 1d
14 32 14 14 1b 14 1a 00 0a 14 14 0a 01 14 14 2f
14 14 53 31 14 06 03 14 14 23 1d 14 00 0a 14 14
0a 01 1c 14 2d 17 14 3d 2d 14 23 1d 14 1c 2c 1d

Original image (23x3):

enter image description here

Magnified 20x:

enter image description here

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

APL(NARS), 54 chars, 108 bytes

r←h w;c
r←c←0
→3×⍳(0=3∣r)∨'3'∊⍕r⋄c+←1⋄→0×⍳r>w
r+←1⋄→2

I prefer this, the loop, to all recursive functions; test:

  h¨1 2 11 22 29 251    
2 4 14 25 40 254 
  h¨3 6 10   
4 7 11 
\$\endgroup\$
0
\$\begingroup\$

C, 59 bytes

n,m;f(){if(++n%3)for(m=n;m;m/=10)(m%10==3)?f():0;else f();}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to PPCG! Which compiler did you use to test this? I can't seem to get it working with gcc, tcc, or clang. Also, functions are required to take input using one of these method. \$\endgroup\$
    – Dennis
    Commented Apr 14, 2019 at 1:24
0
\$\begingroup\$

K (oK), 24 23 bytes

Solution:

(1+)/[{(~3!x)|51in$x};]

Try it online!

Explanation:

Iterate from input+1 until valid number is found. Bit of a cheat dropping whitespace between 51 and in.

(1+)/[{(~3!x)|51in$x};] / the solution
    /[{             };] / iterate (/) while {} is true
                  $x    / string x, e.g. 12 => "12"
              51in      / is "3" (51 in ASCII) in the string?
             |          / or
       (    )           / do this together
         3!x            / x modulo 3
        ~               / not
 1+                     / add 1 to x (e.g. x++)
\$\endgroup\$
1
2

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.