Count without 3

45

2

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

Dennis

Posted 2016-11-03T01:59:10.163

Reputation: 196 637

5I feel like we had a challenge like this... – Conor O'Brien – 2016-11-03T02:06:38.017

Goddangit the fricking rationale. – Mama Fun Roll – 2016-11-03T03:23:40.457

5It 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. – mbomb007 – 2016-11-03T13:51:10.540

1@mbomb007 Different countries, different games. :) – Dennis – 2016-11-03T14:04:50.460

Related – mbomb007 – 2016-11-03T14:08:45.110

12@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. – tomasz – 2016-11-03T21:49:55.207

@tomasz Seems like proof that drinking is bad for your intelligence. – mbomb007 – 2016-11-04T14:09:34.043

10

@mbomb007 Depends on the blood alcohol concentration.

– Dennis – 2016-11-04T14:16:13.137

4@mbomb007: That would depend on the proof of whatever you are drinking. – tomasz – 2016-11-04T14:16:49.610

Related. I am not aware of the existence of this question when I posted that. – Weijun Zhou – 2018-03-02T08:54:49.083

can I begin in 1->1? – sergiol – 2018-12-13T21:26:56.887

@sergiol The input is the previous number, not an index. – Dennis – 2018-12-13T21:37:32.680

Counting without 3… I feel like it's favorite Valve's game. – val says Reinstate Monica – 2019-04-12T20:34:15.477

Answers

21

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

Fatalize

Posted 2016-11-03T01:59:10.163

Reputation: 32 976

3Answers like this are so beautiful in Brachylog :) – Emigna – 2016-11-03T13:40:20.053

3@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 :) – Fatalize – 2016-11-03T13:41:57.943

14

JavaScript (ES6), 30 bytes

f=n=>++n%3*!/3/.test(n)?n:f(n)

ETHproductions

Posted 2016-11-03T01:59:10.163

Reputation: 47 880

Both index 2 and index 3 return the number 4 with this function – nl-x – 2016-11-03T16:11:57.967

1@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. – ETHproductions – 2016-11-03T16:12:50.567

I think I'm starting to understand it... My bad – nl-x – 2016-11-03T16:14:58.660

8

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

Daniel

Posted 2016-11-03T01:59:10.163

Reputation: 6 425

1The 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. – xnor – 2016-11-03T05:15:05.937

@xnor, oh yeah silly me I don't know why I did that – Daniel – 2016-11-03T11:32:16.947

One byte improvement by starting with x=~input(), subtracting instead of adding, and printing -x. – Mitch Schwartz – 2016-11-03T14:39:54.513

@MitchSchwartz, oh thanks! – Daniel – 2016-11-03T15:36:15.587

Best edit ever. – Mitch Schwartz – 2016-11-03T16:46:01.403

@Dopapp Since you're counting backwards, did you mean x-=1? Right now, 2 outputs 2 when it's meant to output 4 – Artyer – 2016-11-03T19:02:39.103

1@Artyer That's only 1 of the 3 mistakes introduced in that edit. – Mitch Schwartz – 2016-11-03T19:21:33.260

@MitchSchwartz, are the mistakes that x+=1 should be x-=1 and print-x should be print -x. And your name. That was autocorrect. – Daniel – 2016-11-03T20:02:27.053

@Dopapp print-x works for me – Artyer – 2016-11-03T20:31:12.537

@Artyer, so what's the third mistake? – Daniel – 2016-11-03T20:39:42.947

1

@Dopapp The current revision (Without the space) is 43 bytes? https://mothereff.in/byte-counter#x%3D~input%28%29%0Awhile%273%27%5B%3Ax%253%5Din%60x%60%3Ax-%3D1%0Aprint-x

– Artyer – 2016-11-03T20:52:59.810

8

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

miles

Posted 2016-11-03T01:59:10.163

Reputation: 15 654

Well, J is probably the most smiley-prone programming language. – Adám – 2016-12-12T00:24:14.397

7

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

Emigna

Posted 2016-11-03T01:59:10.163

Reputation: 50 798

7

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

Dom Hastings

Posted 2016-11-03T01:59:10.163

Reputation: 16 415

1@dan1111 It's Perl, what did you expect? Clarity? – Erik the Outgolfer – 2016-11-04T14:46:21.057

1@EriktheGolfer what? This is the very definition of "self-documenting code". – None – 2016-11-04T14:47:20.717

@dan1111 It seems you know Perl. I have no idea of how Perl works, because of its famous weirdness. – Erik the Outgolfer – 2016-11-04T14:50:33.967

@dan1111 Thanks! Quite happy with how short it turned out! – Dom Hastings – 2016-11-04T16:58:04.947

@EriktheGolfer I don't know what you mean! I've heard people describe Perl as write only code, but surely not this... :D – Dom Hastings – 2016-11-04T16:58:35.690

1

@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 :)

– Erik the Outgolfer – 2016-11-04T17:05:29.957

6

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

Socratic Phoenix

Posted 2016-11-03T01:59:10.163

Reputation: 1 629

2You can use | instead of || – Numberknot – 2016-11-03T04:13:22.873

1@Numberknot I had no idea bitwise operators functioned as logical ones in some contexts! Thanks! – Socratic Phoenix – 2016-11-03T10:43:47.100

1Why the do-while? Just a regular for-loop is shorter: i->{for(;++i%3<1|(i+"").contains("3"););return i;} (50 bytes) – Kevin Cruijssen – 2016-11-03T11:08:47.820

@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! – Socratic Phoenix – 2016-11-03T11:33:20.830

5

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

ETHproductions

Posted 2016-11-03T01:59:10.163

Reputation: 47 880

5

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

AdmBorkBork

Posted 2016-11-03T01:59:10.163

Reputation: 41 581

4

R, 46 bytes

n=scan()+1;while(!n%%3|grepl(3,n))n=n+1;cat(n)

plannapus

Posted 2016-11-03T01:59:10.163

Reputation: 8 610

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). – rturnbull – 2016-11-04T14:52:00.997

4

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.

0WJYxW9FMN

Posted 2016-11-03T01:59:10.163

Reputation: 2 663

1You 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. – Erik the Outgolfer – 2016-11-04T14:41:45.750

@EriktheGolfer Thanks! I'll change it to Python 2, as it's way shorter. – 0WJYxW9FMN – 2016-11-04T17:28:45.490

42s: f=lambda x:'3'[:~x%3]in\~x`and f(x+1)or-~xandf=lambda x:f(x+1)if'3'[:~x%3]in`~x`else-~x` – Mitch Schwartz – 2016-11-04T17:56:40.313

3

Lua, 58 Bytes

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

Cyv

Posted 2016-11-03T01:59:10.163

Reputation: 211

3

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 ^

Blue

Posted 2016-11-03T01:59:10.163

Reputation: 26 661

1At first I though this said while at the beginning. – Conor O'Brien – 2016-11-03T18:37:44.370

If you glance at it I can see that – Blue – 2016-11-03T18:38:47.933

3

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

Laikoni

Posted 2016-11-03T01:59:10.163

Reputation: 23 676

1Also 50 bytes: until(\x->mod x 3>0&&notElem '3'(show x))succ.succ. – nimi – 2016-11-03T13:51:03.110

3

C#, 56, 51 bytes.

This is surprisingly short for a C# answer!

x=>{while(++x%3<1|(x+"").Contains("3"));return x;};

Morgan Thrapp

Posted 2016-11-03T01:59:10.163

Reputation: 3 574

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. – Grax32 – 2016-11-03T17:17:47.147

The problem is that if I make it recursive, I then have to count the function and type definitions. – Morgan Thrapp – 2016-11-03T17:19:29.433

Is there somewhere I can go to see these guidelines? I find C# golfing confusing on here. – Grax32 – 2016-11-03T17:21:53.163

@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. – Morgan Thrapp – 2016-11-03T17:24:39.720

@MorganThrapp please check out my c# answer with recursion at 49 bytes :) – lee – 2018-03-02T06:26:46.513

3

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

Jakube

Posted 2016-11-03T01:59:10.163

Reputation: 21 462

2

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.

Titus

Posted 2016-11-03T01:59:10.163

Reputation: 13 814

2

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

Jo King

Posted 2016-11-03T01:59:10.163

Reputation: 38 234

2

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

Ven

Posted 2016-11-03T01:59:10.163

Reputation: 3 382

2

GolfSharp, 43 bytes

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

downrep_nation

Posted 2016-11-03T01:59:10.163

Reputation: 1 152

2

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.

Xanderhall

Posted 2016-11-03T01:59:10.163

Reputation: 1 236

1You 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. – user59178 – 2016-11-03T16:39:46.207

@user59178 I assumed the function had to return $i – Xanderhall – 2016-11-03T19:14:31.313

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. – user59178 – 2016-11-04T09:03:26.193

Save a byte with strpos(_.$i,'3') – Alex Howansky – 2016-11-04T18:12:00.563

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 – Titus – 2016-12-06T16:15:28.460

2

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.

Elenian

Posted 2016-11-03T01:59:10.163

Reputation: 71

you can use i instead of "#{i}" – Mhmd – 2016-11-05T07:21:19.380

2

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

Luis Mendo

Posted 2016-11-03T01:59:10.163

Reputation: 87 464

2

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.

Robert Hickman

Posted 2016-11-03T01:59:10.163

Reputation: 661

1

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.)

Dennis

Posted 2016-11-03T01:59:10.163

Reputation: 196 637

1

Mathematica, 51 bytes

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

Try it online!

J42161217

Posted 2016-11-03T01:59:10.163

Reputation: 15 931

1

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.

SuperJedi224

Posted 2016-11-03T01:59:10.163

Reputation: 11 342

Can you explain this answer? – sintax – 2019-03-21T15:51:46.663

@sintax Added some explanatory comments at the provided link. – SuperJedi224 – 2019-03-23T02:12:50.740

1

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

recursive

Posted 2016-11-03T01:59:10.163

Reputation: 8 616

1

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

reffu

Posted 2016-11-03T01:59:10.163

Reputation: 1 361

1

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;}

Steadybox

Posted 2016-11-03T01:59:10.163

Reputation: 15 798

1

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

Conor O'Brien

Posted 2016-11-03T01:59:10.163

Reputation: 36 228

1

JavaScript, 35 bytes

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

Try it online!

Oliver

Posted 2016-11-03T01:59:10.163

Reputation: 7 160

4

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.

– Post Rock Garf Hunter – 2016-11-03T20:29:57.323

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. – Oliver – 2016-11-03T20:31:55.030

4No 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 [tag:programming-puzzle] tag. Welcome to PPCG I hope you have fun. – Post Rock Garf Hunter – 2016-11-03T20:36:45.083

"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.

– James – 2016-11-03T21:26:38.767

1

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.

Neil

Posted 2016-11-03T01:59:10.163

Reputation: 95 035

1

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

Erik the Outgolfer

Posted 2016-11-03T01:59:10.163

Reputation: 38 134

1

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

Erik the Outgolfer

Posted 2016-11-03T01:59:10.163

Reputation: 38 134

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`. – Jakube – 2016-11-08T19:01:32.690

Sorry: W|!%=hQ3/`Q\3;Q – Jakube – 2016-11-08T19:07:33.893

@Jakube The variable is not just incrementing but thanks. – Erik the Outgolfer – 2016-11-09T11:44:30.620

1

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)))

Carcigenicate

Posted 2016-11-03T01:59:10.163

Reputation: 3 295

0

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

lee

Posted 2016-11-03T01:59:10.163

Reputation: 200

0

Japt, 11 bytes

_%3«Zsø3}a°U

Run it online

Oliver

Posted 2016-11-03T01:59:10.163

Reputation: 7 160

0

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

maxb

Posted 2016-11-03T01:59:10.163

Reputation: 5 754

0

Ruby, 29 bytes

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

Try it online!

G B

Posted 2016-11-03T01:59:10.163

Reputation: 11 099

0

Perl 6, 39 bytes

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

bb94

Posted 2016-11-03T01:59:10.163

Reputation: 1 831

0

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.

SuperJedi224

Posted 2016-11-03T01:59:10.163

Reputation: 11 342

0

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 

RosLuP

Posted 2016-11-03T01:59:10.163

Reputation: 3 036

0

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

SuperJedi224

Posted 2016-11-03T01:59:10.163

Reputation: 11 342

0

TI-BASIC, 36 bytes

Repeat fPart(Ans/3) and 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
  and 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.

Tau

Posted 2016-11-03T01:59:10.163

Reputation: 1 935

0

C, 59 bytes

n,m;f(){if(++n%3)for(m=n;m;m/=10)(m%10==3)?f():0;else f();}

user1460016

Posted 2016-11-03T01:59:10.163

Reputation: 1

1

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.

– Dennis – 2019-04-14T01:24:53.250

0

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++)

streetster

Posted 2016-11-03T01:59:10.163

Reputation: 3 635