55
\$\begingroup\$

Winner: Aditsu's CJam answer! A whopping 25 bytes! Nice!

You may continue to submit your answer, however you can no longer win. Original post kept for posterity:


The "Bzzt" game is a game where you must count to a number (in this case 500). However, if the number has a 3 in it, or is divisible by 3, you don't say the number. Instead, you say "Bzzt".

Rules:

  • You can't hard-code the numbers.
  • The number only has to satisfy at least 1 of the following requirements
    • Divisible by 3
    • Number contains a 3
  • Some type of separator is mandatory (12bzzt14 doesn't count)
  • Score is measured in bytes.
  • You must count exactly to 500, starting at 1 or 0 (you chose).
  • The numbers must be output, but it doesn't matter how (e.g., stdout, writing to a text file, etc.).
  • 0 can be divisible by 3 or not divisible. You can choose.
  • You can output the numbers one at a time (ex, output 1, then 2, then bzzt, then 4, etc) or all at once (e.g., output 1 2 bzzt 4 5).
  • You must replace the letter 3 with the word "bzzt". This is not case sensitive (bZzt, Bzzt, bzzt are all okay).

  • This is a challenge, so the shortest code wins.
  • This contest ends June 30th 2014 (7 days from posting).
\$\endgroup\$
36
  • 1
    \$\begingroup\$ For purposes of this question, is 0 divisible by 3? \$\endgroup\$
    – Οurous
    Commented Jun 23, 2014 at 7:07
  • 2
    \$\begingroup\$ Is it "buzz" or "bzzt"? You wrote "buzz" twice so far. \$\endgroup\$ Commented Jun 23, 2014 at 8:40
  • 3
    \$\begingroup\$ Please clarify. Do I have to output buzz or bzzt if both of the requirements apply? Do I have to output 12bzzt4 or bzzt for 1234? \$\endgroup\$
    – user344
    Commented Jun 23, 2014 at 12:41
  • 4
    \$\begingroup\$ I'd say bzzt for 1234. It's a 'common' drinking game here (only we often do it with 7) \$\endgroup\$
    – Martijn
    Commented Jun 23, 2014 at 13:38
  • 70
    \$\begingroup\$ "0 can be divisible by 3 or not divisible. You can choose." I really don't think you can choose. 0 mod 3 is 0, that's not really a matter of opinion. \$\endgroup\$ Commented Jun 23, 2014 at 15:54

82 Answers 82

32
\$\begingroup\$

CJam - 25

501{3sI3%<Is-I"bzzt"?N}fI

Thanks Howard :)

Try it at http://cjam.aditsu.net/

Explanation:

501{…}fI is basically for(int I=0; I<501; ++I) {…}
3s converts 3 to string, i.e. "3"
I3% is I % 3
< gets the left substring - "3".substring(0, I % 3) - which is "" for I % 3 == 0 and "3" otherwise
Is converts I to string
- with 2 strings does a set difference, resulting in an empty string iff I % 3 == 0 (the first string was empty) or I has a 3 digit in it
…I"bzzt"? is like … ? I : "bzzt"; the previous string is treated as a boolean value, where "" is false and any other string is true
N adds a newline

\$\endgroup\$
1
  • \$\begingroup\$ You can use the same trick as in my golfscript solution and save the logical and: 501{3sI3%<Is-I"bzzt"?N}fI. \$\endgroup\$
    – Howard
    Commented Jun 23, 2014 at 10:29
30
\$\begingroup\$

Ruby, 43

501.times{|a|puts"#{a}"[?3]||a%3<1?:Bzzt:a}

Pretty straightforward.

Edit: Saved one byte, thanks Howard!

\$\endgroup\$
6
  • 1
    \$\begingroup\$ You can save a single whitespace if you write "#{a}"[?3]||a%3<1. \$\endgroup\$
    – Howard
    Commented Jun 23, 2014 at 9:52
  • \$\begingroup\$ @Howard: Indeed, thanks a lot! \$\endgroup\$
    – Ventero
    Commented Jun 23, 2014 at 10:07
  • 1
    \$\begingroup\$ Even more unreadable but unfortunately same length: "#{a}"[?3[0,a%3]]. \$\endgroup\$
    – Howard
    Commented Jun 23, 2014 at 10:19
  • \$\begingroup\$ What about replacing puts with p and saving 3 chars ? \$\endgroup\$ Commented Jun 26, 2014 at 22:03
  • 1
    \$\begingroup\$ @DavidUnric p prints the result of calling inspect on its argument (compared to puts, which calls to_s). So instead of printing Bzzt (which is :Bzzt.to_s), it would print :Bzzt, which doesn't match the output specifications. \$\endgroup\$
    – Ventero
    Commented Jun 26, 2014 at 22:14
25
\$\begingroup\$

seq and GNU sed - 42 33 31 30

Works directly in dash, some other shells might need to have history expansion disabled, e.g. with bash set +H:

seq 500|sed 0~3!{/3/!b}\;cbzzt
\$\endgroup\$
4
  • \$\begingroup\$ How does it work? \$\endgroup\$ Commented Jun 25, 2014 at 5:27
  • \$\begingroup\$ History expansion is disabled by default in shell scripts. \$\endgroup\$
    – user344
    Commented Jun 25, 2014 at 16:14
  • \$\begingroup\$ @nyuszika7h: true, but I expect many will try the answer in an interactive shell. \$\endgroup\$
    – Thor
    Commented Jun 25, 2014 at 21:47
  • 1
    \$\begingroup\$ @nbubis: the updated version works by generating the sequence with seq. 0~3! runs {/3/!b} and together these expressions leave the line as is if it is not divisible by and does not contain a 3. The last bit "corrects" the line to bzzt. \$\endgroup\$
    – Thor
    Commented Jun 27, 2014 at 6:23
18
\$\begingroup\$

x86 machine code on DOS (.com file) - 71 bytes

00000000  31 c9 68 20 24 89 e5 89  c8 bb 03 00 31 d2 f7 f3  |1.h $.......1...|
00000010  85 d2 74 1a 89 c8 b3 0a  31 d2 f7 f3 80 fa 03 74  |..t.....1......t|
00000020  0d 80 c2 30 86 d6 52 44  85 c0 75 ea eb 08 89 ec  |...0..RD..u.....|
00000030  68 7a 74 68 62 7a 89 e2  b4 09 cd 21 89 ec 41 81  |hzthbz.....!..A.|
00000040  f9 f4 01 7e c2 59 c3                              |...~.Y.|

Prints the required output to stdout with space as a delimiter; can be run without problems in DosBox.

Commented assembly:

    org 100h

start:
    ; 0 - 500 counter
    xor cx,cx
    ; we'll use the stack as scratch space to build the strings to print
    ; first of all, push ' $' on the stack (in reverse order); this will be
    ; the end of each string we'll print
    push 2420h
    ; save the stack pointer, to get back to this position after each print
    mov bp,sp
mainloop:
    ; first try to divide by three
    mov ax,cx
    mov bx,3
    xor dx,dx
    div bx
    test dx,dx
    ; no remainder => bzzt
    jz bzzt
    ; otherwise, go into the decimal-print loop
    mov ax,cx
divloop:
    ; bh is already at zero due to the mov bx,3 above
    mov bl,10
    xor dx,dx
    ; divide by 10
    div bx
    ; remainder is 3 => bzzt
    cmp dl,3
    je bzzt
    ; convert number to digit
    add dl,'0'
    ; trick: we move the digit to the upper byte of dx: this allows us to
    ; push the whole dx (you cannot do push dl) but to easily kill the
    ; undesidered byte by touching the stack pointer (it will be overwritten
    ; by the next stack operation/ignored by the print)
    xchg dl,dh
    push dx
    inc sp
    ; check is there anything left to print, rinse & repeat
    test ax,ax
    jnz divloop
    ; skip straight to print
    jmp print
bzzt:
    ; since we may be here from inside divloop, reset the stack pointer to
    ; where we left ' $'
    mov sp,bp
    ; push 'bzzt'
    push 747ah
    push 7a62h
print:
    ; print what is on the stack
    mov dx,sp
    mov ah,9h
    int 21h
    ; move us back to ' $'
    mov sp,bp

    ; increment cx and repeat while we are <=500
    inc cx
    cmp cx,500
    jle mainloop
end:
    ; fix back the stack pointer to the original value (=kill the first push)
    pop cx
    ; quit
    ret
\$\endgroup\$
2
  • 5
    \$\begingroup\$ RESPECT!!!!! Amazing! \$\endgroup\$
    – yossico
    Commented Jun 25, 2014 at 18:33
  • \$\begingroup\$ @yossico: thank you! :) actually, it's nothing really special, I'm sure that people who actually wrote assembly for DOS could surely shave some more bytes off. \$\endgroup\$ Commented Jun 25, 2014 at 22:47
17
\$\begingroup\$

Perl - 35 40 42

print$_*!/3/%3?$_:bzzt,$/for 1..500
\$\endgroup\$
16
\$\begingroup\$

PHP, no separator - 62, 61,59,58,52,49 47

It doesn't say there should be a space/newline/separator between them, without:

while(++$i<501)echo$i%3*!strpbrk($i,3)?$i:bzzt;

With the separator, 68,67,65,64,58/55,53/52 51/50

while(++$i<501)echo$i%3*!strpbrk($i,3)?$i:bzzt,' '; // 51
while(++$i<501)echo$i%3*!strpbrk($i,3)?$i:bzzt,~õ; // 50
  • Just found a small 'cheat', don't need a space after echo, saved me a bit.
  • The creates a newline
  • Another small 'cheat', the bzzt doesn't need quotes (tested it). Not the way to go, but it works.

Javascript - 54,51 50

Same principle, but javascript functions:

for(i=0;i++<500;)alert(i%3<1|/3/.test(i)?'bzzt':i)
\$\endgroup\$
20
  • \$\begingroup\$ Thanks@core for the brackets and for() tip. The for made me crash my browser a few times ^^ \$\endgroup\$
    – Martijn
    Commented Jun 23, 2014 at 8:53
  • \$\begingroup\$ Haha. I tested it with console.log() obviously, but this is shorter. \$\endgroup\$
    – Martijn
    Commented Jun 23, 2014 at 8:56
  • 1
    \$\begingroup\$ Your Javascript solution counts to 501. \$\endgroup\$
    – nderscore
    Commented Jun 23, 2014 at 15:22
  • 1
    \$\begingroup\$ PHP for 52: <?for(;500>$i++;)echo$i%3*!strpbrk($i,3)?$i:bzzt,~õ; The õ is char 245, a bit-inverted \n. \$\endgroup\$
    – primo
    Commented Jun 24, 2014 at 8:10
  • 1
    \$\begingroup\$ You can replace !strpbrk($1,3) with trim(3,$i) to save 4 bytes \$\endgroup\$
    – aross
    Commented Apr 11, 2016 at 9:13
15
\$\begingroup\$

Javascript 50 49

-1 byte thanks to core1024

for(i=0;i++<500;)alert(i%3*!/3/.test(i)?i:'bzzt')
\$\endgroup\$
6
  • \$\begingroup\$ I knew that could be done! But didn't find how \$\endgroup\$
    – edc65
    Commented Jun 23, 2014 at 15:34
  • 1
    \$\begingroup\$ You can imagine my face when a JavaScript solution beats my LiveScript one. \$\endgroup\$
    – user344
    Commented Jun 23, 2014 at 19:55
  • \$\begingroup\$ in yout second solution you could remove one & sign and it will still work \$\endgroup\$
    – user902383
    Commented Jun 24, 2014 at 8:56
  • \$\begingroup\$ @user902383 It's not a valid result without a boolean && operator. For example: 497 % 3 = 2 => 2 & true = 0 => bzzt \$\endgroup\$
    – nderscore
    Commented Jun 24, 2014 at 12:19
  • \$\begingroup\$ for(i=0;++i<501;)alert(!/3/.test(i)*i%3?i:'bzzt') - 49 :P \$\endgroup\$
    – core1024
    Commented Jun 25, 2014 at 14:46
11
\$\begingroup\$

GolfScript, 30 29 characters

501,{:^`3`^3%<?)'bzzt'^if n}/

Not so straightforward implementation in GolfScript, can be tested here.

\$\endgroup\$
10
\$\begingroup\$

Perl, 36

print$_%3&&!/3/?$_:Bzzt,$/for 1..500

Edit: I'm not a Perl monk, so core1024 seems to have managed to golf another byte out of this in his answer.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Use say to save 4 bytes: say$_%3&&!/3/?$_:bzzt for 1..5e2 \$\endgroup\$
    – Zaid
    Commented Jun 23, 2014 at 14:30
  • \$\begingroup\$ @Zaid why 5e2 and not 500? \$\endgroup\$ Commented Jun 23, 2014 at 20:08
  • \$\begingroup\$ @Charles: they are equivalent \$\endgroup\$
    – Zaid
    Commented Jun 24, 2014 at 1:27
  • 1
    \$\begingroup\$ @Zaid so why not be clearer? \$\endgroup\$ Commented Jun 24, 2014 at 3:12
  • 4
    \$\begingroup\$ @Charles : In the context of a code-golf challenge, should one even care? \$\endgroup\$
    – Zaid
    Commented Jun 24, 2014 at 9:26
10
\$\begingroup\$

C# (71)

Can be directly executed in LinqPad.

for(var i=0;++i<501;)(i%3<1|(i+"").Contains("3")?"buzz":i+"").Dump();
\$\endgroup\$
6
  • 1
    \$\begingroup\$ You can save a char by using bitwise-or (|) instead of logical-or. \$\endgroup\$
    – Johnbot
    Commented Jun 24, 2014 at 9:23
  • \$\begingroup\$ @Johnbot Thanks for your suggestion. \$\endgroup\$
    – EvilFonti
    Commented Jun 24, 2014 at 10:58
  • \$\begingroup\$ You can remove the parentheses around the conditional too. \$\endgroup\$
    – Johnbot
    Commented Jun 24, 2014 at 11:09
  • \$\begingroup\$ @Johnbot: That's not bitwise-or. It's (non-short-circuiting) logical-or, since the operands are boolean. \$\endgroup\$
    – Ryan M
    Commented Jun 26, 2014 at 21:43
  • \$\begingroup\$ Dump is just LinqPad though, correct? Doing this in C# as an application would require an extension method to be added. (And it's 'bzzt' not 'buzz' =) ) \$\endgroup\$
    – Paul
    Commented Jun 27, 2014 at 21:14
9
\$\begingroup\$

Python (52)

Thanks grc!

for i in range(501):print[i,'Bzzt'][i%3<1or'3'in`i`]

Old version:

print['Bzzt'if'3'in`i`or i%3<1 else`i`for i in range(501)]
\$\endgroup\$
2
  • 2
    \$\begingroup\$ A little shorter: for i in range(501):print[i,'Bzzt'][i%3<1or'3'in`i`] \$\endgroup\$
    – grc
    Commented Jun 23, 2014 at 8:00
  • 5
    \$\begingroup\$ One char shorter: ['3'[:i%3]in`i`] \$\endgroup\$
    – xnor
    Commented Jun 23, 2014 at 23:25
7
\$\begingroup\$

Haskell: 88 82 80 79

main=mapM_ f[1..500]
f n|n`mod`3<1||'3'`elem`show n=putStrLn"bzzt"
f n=print n
\$\endgroup\$
4
  • \$\begingroup\$ If i were writing an answer i would ditch IO and return a string value. \$\endgroup\$ Commented Aug 23, 2014 at 10:35
  • 1
    \$\begingroup\$ You should also switch==0 with <1 \$\endgroup\$ Commented Aug 23, 2014 at 10:36
  • \$\begingroup\$ Also, try making elem prefix. I think you should be able to write elem'3'$show n, which is a char shorter. Or not. I didn't check. \$\endgroup\$ Commented Aug 23, 2014 at 10:38
  • \$\begingroup\$ @proudhaskeller: The spec states that the "numbers must be output", so IO has to be involved. And using elem in prefix notation doesn't save any characters because there has to be a space before the apostrophe; otherwise you'll get Not in scope: elem'3'. Good eye with <1, though! \$\endgroup\$ Commented Aug 23, 2014 at 14:55
6
\$\begingroup\$

JavaScript 66 63 60

for(a=i=[];i<500;)a[i++]=i%3&&!/3/.test(i)?i:'bzzt';alert(a)

Thanks to edc65 for the suggestion to use array. The output will now be comma-separated.


Old versions

Version 1a - 66

Print from 1 to 500 in an alert box according to the rule. The output is space-separated.

a="";for(i=1;i<501;i++)a+=i%3&&!/3/.test(i)?i+" ":"bzzt ";alert(a)

Version 1b - 65

If we consider 0 to be not divisible by 3, we can shorten the solution to 65 character:

a=0;for(i=1;i<501;i++)a+=i%3&&!/3/.test(i)?" "+i:" bzzt";alert(a)

Version 2 - 63

for(a=i="";i<501;)a+=++i%3&&!/3/.test(i)?i+" ":"bzzt ";alert(a)

Thanks to grc for the suggestion to reduce the length.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I think you could start with for(a=i="";i<500;)a+=++i ... \$\endgroup\$
    – grc
    Commented Jun 23, 2014 at 8:21
  • \$\begingroup\$ I comma separeted output is OK, an array will be shorter: for(o=i=[];i<500;)o[i++]=i%3&&!/3/.test(i)?i:'bzzt';alert(o) \$\endgroup\$
    – edc65
    Commented Jun 23, 2014 at 8:51
6
\$\begingroup\$

PowerShell, 42

1..500|%{($_,'bzzt')[$_-match3-or!($_%3)]}

Mostly Ventero's work, with a little help on syntax by me ;-)

\$\endgroup\$
6
\$\begingroup\$

R, 49 characters

a=1:500;b='bzzt';a[!a%%3]=b;a[grep(3,a)]=b;cat(a)

Explained:

a=1:500 #Creates a vector with all integers from 1 to 500
b='bzzt'
a[!a%%3]=b #Replace all multiples of 3 by 'bzzt', thus coercing all other integers to character strings
a[grep(3,a)]=b #Replaces the character strings containing 3 by 'bzzt'
cat(a) #Print to stdout

Usage:

> a=1:500;b='bzzt';a[!a%%3]=b;a[grep(3,a)]=b;cat(a)
1 2 bzzt 4 5 bzzt 7 8 bzzt 10 11 bzzt bzzt 14 bzzt 16 17 bzzt 19 20 bzzt 22 bzzt bzzt 25 26 bzzt 28 29 bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt 40 41 bzzt bzzt 44 bzzt 46 47 bzzt 49 50 bzzt 52 bzzt bzzt 55 56 bzzt 58 59 bzzt 61 62 bzzt 64 65 bzzt 67 68 bzzt 70 71 bzzt bzzt 74 bzzt 76 77 bzzt 79 80 bzzt 82 bzzt bzzt 85 86 bzzt 88 89 bzzt 91 92 bzzt 94 95 bzzt 97 98 bzzt 100 101 bzzt bzzt 104 bzzt 106 107 bzzt 109 110 bzzt 112 bzzt bzzt 115 116 bzzt 118 119 bzzt 121 122 bzzt 124 125 bzzt 127 128 bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt 140 bzzt 142 bzzt bzzt 145 146 bzzt 148 149 bzzt 151 152 bzzt 154 155 bzzt 157 158 bzzt 160 161 bzzt bzzt 164 bzzt 166 167 bzzt 169 170 bzzt 172 bzzt bzzt 175 176 bzzt 178 179 bzzt 181 182 bzzt 184 185 bzzt 187 188 bzzt 190 191 bzzt bzzt 194 bzzt 196 197 bzzt 199 200 bzzt 202 bzzt bzzt 205 206 bzzt 208 209 bzzt 211 212 bzzt 214 215 bzzt 217 218 bzzt 220 221 bzzt bzzt 224 bzzt 226 227 bzzt 229 bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt 241 242 bzzt 244 245 bzzt 247 248 bzzt 250 251 bzzt bzzt 254 bzzt 256 257 bzzt 259 260 bzzt 262 bzzt bzzt 265 266 bzzt 268 269 bzzt 271 272 bzzt 274 275 bzzt 277 278 bzzt 280 281 bzzt bzzt 284 bzzt 286 287 bzzt 289 290 bzzt 292 bzzt bzzt 295 296 bzzt 298 299 bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt 400 401 bzzt bzzt 404 bzzt 406 407 bzzt 409 410 bzzt 412 bzzt bzzt 415 416 bzzt 418 419 bzzt 421 422 bzzt 424 425 bzzt 427 428 bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt bzzt 440 bzzt 442 bzzt bzzt 445 446 bzzt 448 449 bzzt 451 452 bzzt 454 455 bzzt 457 458 bzzt 460 461 bzzt bzzt 464 bzzt 466 467 bzzt 469 470 bzzt 472 bzzt bzzt 475 476 bzzt 478 479 bzzt 481 482 bzzt 484 485 bzzt 487 488 bzzt 490 491 bzzt bzzt 494 bzzt 496 497 bzzt 499 500
\$\endgroup\$
6
\$\begingroup\$

Cobra - 70

class P
    def main
        for i in 501,print if('3'in'[i]'or i%3<1,'Bzzt',i)

Batch - 222

Because I genuinely love this language... for some reason...

SETLOCAL ENABLEDELAYEDEXPANSION
for /l %%n in (1,1,500) do (
    set a=%%n&set /ai=!a!%%3
    if "!a:~0,1!"=="3" set a=Bzzt
    if "!a:~1,2!"=="3" set a=Bzzt
    if "!a:~2,3!"=="3" set a=Bzzt
    if !i!==0 set a=Bzzt
    echo !a!>>x)
\$\endgroup\$
3
  • \$\begingroup\$ You should be able to drop the quotation marks around the operands in the first if because you know that there is at least one digit. Also concatenating the complete block within the for with & might work to save the parentheses. \$\endgroup\$
    – Joey
    Commented Jun 23, 2014 at 9:13
  • \$\begingroup\$ Furthermore, you can use == instead of EQU and probably drop a few spaces here and there. \$\endgroup\$
    – Joey
    Commented Jun 23, 2014 at 9:14
  • \$\begingroup\$ @Joey Thanks for the suggestions! \$\endgroup\$
    – Οurous
    Commented Jun 23, 2014 at 9:42
6
\$\begingroup\$

TI-BASIC - 31 (32)(34)(35)(36)(43)

:While X<500
:X+1→X
:If not(fPart(X/3
:"bzzt
:Disp Ans
:End

Total: 25 + 6 lines = 31

Note that most commands on TI-BASIC are represented as single-byte entities.

The code requires X to be initialized to 0 beforehand (otherwise, it's an extra 3 bytes).

I've made several attempts to shave several bytes, so I'm not detailing everything I've done here, as to not clutter the post. They have mostly been directed at shortening the loop, which I've done in this version by a While loop and by shortening the If condition with the help of the Ans variable.

\$\endgroup\$
5
  • \$\begingroup\$ Correct me if I am wrong, but this does not seem to bzzt numbers containing 3's, e.g. 13. \$\endgroup\$
    – Thor
    Commented Jun 24, 2014 at 10:37
  • \$\begingroup\$ @Thor You are correct, but I'm within the rules, as they state that I may print bzzt for only the numbers that are divisible by 3. Adding the other way could be possible, but would need more instructions. \$\endgroup\$ Commented Jun 24, 2014 at 13:59
  • 1
    \$\begingroup\$ The rules say: if the number has a 3 in it, or is divisible by 3, you don't say the number. Instead, you say "Bzzt", so I would say both should be replaced. \$\endgroup\$
    – Thor
    Commented Jun 24, 2014 at 15:07
  • \$\begingroup\$ @Thor What bothers me now most is that teeny tiny comma in the middle of the sentence. The way I've interpreted it, both ways of doing are equally valid, which also explains the 2nd rule: The number only has to satisfy 1 of the requirements... (either have a 3 or be divisible by 3) \$\endgroup\$ Commented Jun 24, 2014 at 15:26
  • 1
    \$\begingroup\$ You miscounted the bytes here because lowercase letters are two-byte tokens. I agree with Thor's interpretation of the rules as it is the one that every answer above this uses. \$\endgroup\$
    – lirtosiast
    Commented Jun 7, 2015 at 2:11
5
\$\begingroup\$

C, 93

Just for the hell of it...

main(i){char s[9]="bzzt";while(i<498+sprintf(s+5,"%d",i))puts(s+5*(i++%3&&!strchr(s+5,51)));}
\$\endgroup\$
4
  • \$\begingroup\$ This has a small glitch - it skips the output for i = 0 \$\endgroup\$
    – anatolyg
    Commented Jun 23, 2014 at 17:27
  • 4
    \$\begingroup\$ @anatolyg "You must count exactly to 500, starting at 1 or 0 (you chose)." — I chose to start from 1. \$\endgroup\$
    – r3mainer
    Commented Jun 23, 2014 at 17:45
  • \$\begingroup\$ Missed that bit. Sorry! \$\endgroup\$
    – anatolyg
    Commented Jun 23, 2014 at 18:31
  • \$\begingroup\$ Skips a lot more if you pass it arguments, lol. But, it is well golfed, I'll give you that! Right down to using the return value of sprintf as you trend towards the three digit numbers to control the while, lol. So bad....haha! \$\endgroup\$ Commented Jun 24, 2014 at 22:33
5
\$\begingroup\$

Julia 64 bytes

map(x->x%3==0||contains(string(x),"3")?"Bzzt":string(x),[1:500])
\$\endgroup\$
1
  • 2
    \$\begingroup\$ [println(a%3==0||3 in digits(a)?"bzzt":a) for a=1:500] is 54 bytes. Also returning an array doesn't print the entire array, so I'm not sure if it counts as being "output". If returning an array counts then [a%3==0||3 in digits(a)?"bzzt":a for a=1:500] might count and score 45. \$\endgroup\$
    – gggg
    Commented Jun 25, 2014 at 19:45
5
\$\begingroup\$

cmd.exe - 91

for /l %x in (1,1,500)do @set/a %x%3|find "0">nul&&echo bzzt||echo %x|find/v"3"||echo bzzt

...because why use a batch file when a perfectly straightforward one-liner exists...:-)

\$\endgroup\$
5
\$\begingroup\$

groovy - 51

500.times{println"$it".find('3')||it%3<1?'bzzt':it}

edit: using times the printing version is now as short as the "displaying". thanks @will-p

\$\endgroup\$
1
  • 1
    \$\begingroup\$ +1. You can swap (1..500).each for 500.times \$\endgroup\$
    – Will Lp
    Commented Jun 27, 2014 at 17:20
5
\$\begingroup\$

k4 (37) (35)

{$(x;`bzzt)(|/51=$x)|~"."in$x%3}'!501

k4 lacks a built-in modulo command, and using the q one would take an extra character; this version (ab)uses the built-in output format to check if the string of the result of dividing x by 3 has a decimal point in it.

EDIT:

{$(x;`bzzt)(|/51=$x)|x=3*_x%3}'!501

Clever as matching the decimal point may be, I can shave two bytes by checking that 3*floor x%3 is still x.

\$\endgroup\$
2
  • \$\begingroup\$ Upvoted for cleverly matching the decimal point! \$\endgroup\$
    – Mark
    Commented Jul 1, 2014 at 20:23
  • \$\begingroup\$ Please don't update all of your answers at ones. That will push all the active challenges off the front page. It would be nice if you only did one or two a time. (Although I don't even think changing between h1 and h2 is important enough to warrant an edit in the first place.) \$\endgroup\$ Commented Dec 18, 2014 at 20:02
4
\$\begingroup\$

C, 80

Using spaces as separators instead of line breaks.

n;main(){for(;n++<500;)printf(n%10^3&&n/10%10^3&&n/100^3&&n%3?"%d ":"bzzt ",n);}
\$\endgroup\$
3
  • \$\begingroup\$ 1 bzzt 3 4 bzzt 6 7 bzzt 9 10 bzzt bzzt 13 bzzt ... There's something wrong here. \$\endgroup\$
    – r3mainer
    Commented Jun 23, 2014 at 19:05
  • \$\begingroup\$ It seems I was too greedy, and stumbled into Undefined Behavior (variable modified and read between sequence points). Now fixed. \$\endgroup\$
    – anatolyg
    Commented Jun 23, 2014 at 19:20
  • \$\begingroup\$ Working now :-) \$\endgroup\$
    – r3mainer
    Commented Jun 23, 2014 at 22:18
4
\$\begingroup\$

Mathematica, 54 Characters

This feels too straightforward. Shorter solutions must be possible.

If[DigitCount[#][[3]] > 0 || #~Mod~3 < 1, Bzzt, #] & /@ Range@500
\$\endgroup\$
2
  • \$\begingroup\$ You don't need the apostrophes :) \$\endgroup\$ Commented Jun 24, 2014 at 0:59
  • \$\begingroup\$ @belisarius indeed. Thanks. \$\endgroup\$ Commented Jun 24, 2014 at 1:20
4
\$\begingroup\$

T-SQL 2008 - 80

Not going to win or anything but fun none the less: Tweaked thanks to @domager:

declare @ int=0t:if(@)like'%3%'or @%3=0print'bzzt'print @;set @+=1if @<501goto t

A little known fact, @ is a valid name for a variable. It feels weird as the set based code is the more SQL variant, but shorter is shorter! This version works on any database. Edit: I was able to remove two of the semis as they were not needed. I'm pretty sure this is as optimal as it gets.

Edit2: Never say never. Here it's now even grosser using goto, but it allows us to avoid the block. We can Replace while,begin,end with the shorter if,t:,goto saving 6 characters. We also rearrange statement by rewriting the loop as a pseudo do-while,semantically equivalent. Edit3: Yeah, somehow if is now shorter. Original:

select top 501iif(number like'%3%'or number%3=0,'bzzt',str(number))from spt_values where'p'=type

Must be run on the master database. I love T-SQL despite its noisy and ugly ways. There might be a way to make this simpler, but unfortunately, the iif built-in requires both sides agree on types. Sql servers precedence rules give int higher precedence than strings. Number is also really long, but aliasing is more characters than it is worth. There might be a better way to turn number in to a string. Edit: str works too. 2 less characters than ltrim

\$\endgroup\$
6
  • \$\begingroup\$ At first I thought your code is 2012.96 bytes long. \$\endgroup\$
    – user344
    Commented Jun 23, 2014 at 16:25
  • \$\begingroup\$ slightly longer in @ 101, but doesn't rely on a table declare @i int=1;while(@i<501)begin;if(@i)like'%3%'or @i%3=0 print'bzzt'else print @i;set @i=@i+1 end \$\endgroup\$
    – domager
    Commented Jun 23, 2014 at 16:29
  • \$\begingroup\$ @domager, totally right, plus we can save quite a few characters by switching the variable to @. Then using iif() is still shorter than an if(...)print else print so we gain quite a bit by using the iif expression. Also we can use the shorter @+=1 to save a char \$\endgroup\$
    – Michael B
    Commented Jun 23, 2014 at 17:13
  • \$\begingroup\$ I didn't know T-SQL supported +=. I don't have it handy to test, but I'm pretty sure it supports ''+@ for string conversion using the invariant locale. \$\endgroup\$ Commented Jun 23, 2014 at 22:00
  • \$\begingroup\$ += was added in 2008. It does support ''+@, but it doesn't do what you want. As I said, precedence rules cast to int first so it casts '' to int, resulting in zero so ''+@ is @ still typed as an int.str works and it isn't that much more expensive than anything else (2 extra chars for the parens). I chose t-sql 2012 here because of the IIF operator, who knows maybe 2016's release sql will get rid of some of the noise and start being competitive (unlikely). \$\endgroup\$
    – Michael B
    Commented Jun 24, 2014 at 12:56
4
\$\begingroup\$

VBA: 54

for x=0to 500:?iif(x/3=x\3=instr(1,x,3),x,"Bzzt"):next

Open up your favorite Office program, hit alt+F11 to open the VBA IDE and paste the code into the immediate pane and press enter.

In VBA, : is the line separator, ? is shorthand for print, iif means inline if (think x?"Y":"N"), x/3 does floating point division and x\3 does integer division, instr returns the position of a char in a string, or 0 otherwise, true=-1 and false=0.

The code basically increments x and outputs x if x/3=x\3=instr(1,x,3) is true and "Bzzt" otherwise. x/3=x\3 compares (float)(x/3) to (int)(x/3) and returns a boolean (0 is false and -1 is true). instr(1,x,3) returns 0 if "3" is not in the number, and a positive integer otherwise. The only time the expression returns true is when (x/3=x\3) is false (0) and instr(1,x,3) is 0, or in other words, when x is not divisible by 3 and does not contain the digit "3", which is exactly what we are looking for.

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

Bash, 53 52 48 46

seq 500|factor|sed '/3.*:\| 3 */cBzzt
s/:.*//'

Requires GNU sed (uses the c extension).

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

Java, 142 131 thank to WozzeC suggestion

public class a{public static void main(String[]a){for(int i=1;i<501;i++)System.out.println(i%3>0&(""+i).indexOf(51)<0?i:"bzzt");}}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ My Java is a bit rusty. But wont it be shorter with an and operand? i%3>0&&(""+i).indexOf(51)<0?i:"bzzt" . It might also be possible to skip the { } for the forloop. \$\endgroup\$
    – WozzeC
    Commented Jun 23, 2014 at 14:29
  • \$\begingroup\$ this is 122 bytes, 9 less: class A{public static void main(String[]a){for(int i=0;++i<501;System.out.println(i%3>0&(""+i).indexOf(51)<0?i:"bzzt"));}} \$\endgroup\$ Commented Jul 25, 2015 at 20:35
3
\$\begingroup\$

R (40) (36)

This is basically plannapus answer a little bit shortened, but I can not comment yet

Update: -4 characters (see plannapus's comment)

a=1:500;a[grepl(3,a)|!a%%3]='bzzt';a

Output:

  [1] "1"    "2"    "bzzt" "4"    "5"    "bzzt" "7"    "8"    "bzzt" "10"   "11"   "bzzt" "bzzt" "14"   "bzzt" "16"   "17"   "bzzt" "19"   "20"   "bzzt" "22"   "bzzt" "bzzt" "25"   "26"   "bzzt" "28"   "29"   "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "40"   "41"   "bzzt" "bzzt" "44"   "bzzt" "46"   "47"   "bzzt" "49"   "50"   "bzzt" "52"   "bzzt" "bzzt" "55"   "56"   "bzzt" "58"   "59"   "bzzt" "61"   "62"   "bzzt" "64"   "65"   "bzzt" "67"   "68"   "bzzt" "70"   "71"   "bzzt" "bzzt" "74"   "bzzt" "76"   "77"   "bzzt" "79"   "80"   "bzzt" "82"   "bzzt" "bzzt" "85"   "86"   "bzzt" "88"   "89"   "bzzt" "91"   "92"   "bzzt" "94"   "95"   "bzzt" "97"   "98"   "bzzt" "100"  "101"  "bzzt" "bzzt" "104"  "bzzt" "106"  "107"  "bzzt" "109"  "110"  "bzzt" "112"  "bzzt" "bzzt" "115"  "116"  "bzzt" "118"  "119"  "bzzt" "121"  "122"  "bzzt" "124"  "125"  "bzzt" "127"  "128"  "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "140"  "bzzt" "142"  "bzzt" "bzzt" "145"  "146"  "bzzt" "148"  "149"  "bzzt" "151"  "152"  "bzzt" "154"  "155"  "bzzt" "157"  "158"  "bzzt" "160"  "161"  "bzzt" "bzzt" "164"  "bzzt" "166"  "167"  "bzzt" "169"  "170"  "bzzt" "172"  "bzzt" "bzzt" "175"  "176"  "bzzt" "178"  "179"  "bzzt" "181"  "182"  "bzzt" "184"  "185"  "bzzt" "187"  "188"  "bzzt" "190"  "191"  "bzzt" "bzzt" "194"  "bzzt" "196"  "197"  "bzzt" "199"  "200"  "bzzt" "202"  "bzzt" "bzzt" "205"  "206"  "bzzt" "208"  "209"  "bzzt" "211"  "212"  "bzzt" "214"  "215"  "bzzt" "217"  "218"  "bzzt" "220"  "221"  "bzzt" "bzzt" "224"  "bzzt" "226"  "227"  "bzzt" "229"  "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "241"  "242"  "bzzt" "244"  "245"  "bzzt" "247"  "248"  "bzzt" "250"  "251"  "bzzt" "bzzt" "254"  "bzzt" "256"  "257"  "bzzt" "259"  "260"  "bzzt" "262"  "bzzt" "bzzt" "265"  "266"  "bzzt" "268"  "269"  "bzzt" "271"  "272"  "bzzt" "274"  "275"  "bzzt" "277"  "278"  "bzzt" "280"  "281"  "bzzt" "bzzt" "284"  "bzzt" "286"  "287"  "bzzt" "289"  "290"  "bzzt" "292"  "bzzt" "bzzt" "295"  "296"  "bzzt" "298"  "299"  "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "400"  "401"  "bzzt" "bzzt" "404"  "bzzt" "406"  "407"  "bzzt" "409"  "410"  "bzzt" "412"  "bzzt" "bzzt" "415"  "416"  "bzzt" "418"  "419"  "bzzt" "421"  "422"  "bzzt" "424"  "425"  "bzzt" "427"  "428"  "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "bzzt" "440"  "bzzt" "442"  "bzzt" "bzzt" "445"  "446"  "bzzt" "448"  "449"  "bzzt" "451"  "452"  "bzzt" "454"  "455"  "bzzt" "457"  "458"  "bzzt" "460"  "461"  "bzzt" "bzzt" "464"  "bzzt" "466"  "467"  "bzzt" "469"  "470"  "bzzt" "472"  "bzzt" "bzzt" "475"  "476"  "bzzt" "478"  "479"  "bzzt" "481"  "482"  "bzzt" "484"  "485"  "bzzt" "487"  "488"  "bzzt" "490"  "491"  "bzzt" "bzzt" "494"  "bzzt" "496"  "497"  "bzzt" "499"  "500"
\$\endgroup\$
2
  • \$\begingroup\$ Since many string operation functions (such as strsplit) throw error when fed with non-characters I assumed grepl did as well. Nice catch! +1 \$\endgroup\$
    – plannapus
    Commented Jun 26, 2014 at 7:43
  • 2
    \$\begingroup\$ By the way since you reduced it to one expression you don't need to define b prior to that: a=1:500;a[grepl(3,a)|!a%%3]='bzzt';a \$\endgroup\$
    – plannapus
    Commented Jun 26, 2014 at 7:45
3
\$\begingroup\$

Fortran - 118 114 111

A hopelessly unlikely candidate, though originally developed to fit on punch cards. Using all the obscure constructs from the past, some short code may still be written:

do3 i=1,500
j=i
if(mod(i,3))2,1,2
1 print*,'bzzt'
cycle
2 goto(1)mod(j,10)-2
j=j/10
if(j>0)goto2
3 print*,i
end

The "computed goto" goto(L1,L2,...,Ln) x branches to one of the labels L if and only if 1 <= x <= n.

Edit: Managed to shave off 4 bytes by rearranging the loop that checks for the digit 3. As a bonus, the code now also contains the arithmetic if-statement if(x) a,b,c, which always branches to one of three labels: a if x < 0, b if x == 0 or c if x > 0.

Unfortunately, the first two versions did not produce the correct output. The digit-3 loop now works correctly, and the code now also includes a modern logical if-statement. Three more bytes are gone, because who needs an enddo statement? The output may be verified here.

\$\endgroup\$

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.