Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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 prosperity:


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).
share|improve this question
1  
For purposes of this question, is 0 divisible by 3? –  Ourous Jun 23 at 7:07
2  
Is it "buzz" or "bzzt"? You wrote "buzz" twice so far. –  aditsu Jun 23 at 8:40
3  
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? –  nyuszika7h Jun 23 at 12:41
3  
I'd say bzzt for 1234. It's a 'common' drinking game here (only we often do it with 7) –  Martijn Jun 23 at 13:38
37  
"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. –  David Conrad Jun 23 at 15:54

62 Answers 62

up vote 21 down vote accepted

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

share|improve this answer
    
You can use the same trick as in my golfscript solution and save the logical and: 501{3sI3%<Is-I"bzzt"?N}fI. –  Howard Jun 23 at 10:29

Ruby, 43

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

Pretty straightforward.

Edit: Saved one byte, thanks Howard!

share|improve this answer
1  
You can save a single whitespace if you write "#{a}"[?3]||a%3<1. –  Howard Jun 23 at 9:52
    
@Howard: Indeed, thanks a lot! –  Ventero Jun 23 at 10:07
    
Even more unreadable but unfortunately same length: "#{a}"[?3[0,a%3]]. –  Howard Jun 23 at 10:19
    
What about replacing puts with p and saving 3 chars ? –  David Unric Jun 26 at 22:03
1  
@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. –  Ventero Jun 26 at 22:14

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
share|improve this answer
    
How does it work? –  nbubis Jun 25 at 5:27
    
History expansion is disabled by default in shell scripts. –  nyuszika7h Jun 25 at 16:14
    
@nyuszika7h: true, but I expect many will try the answer in an interactive shell. –  Thor Jun 25 at 21:47
1  
@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. –  Thor Jun 27 at 6:23

Perl - 35 40 42

print$_*!/3/%3?$_:bzzt,$/for 1..500
share|improve this answer

Javascript 50 49

-1 byte thanks to core1024

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

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
share|improve this answer
3  
RESPECT!!!!! Amazing! –  yossico Jun 25 at 18:33
    
@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. –  Matteo Italia Jun 25 at 22:47

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)
share|improve this answer
    
Thanks@core for the brackets and for() tip. The for made me crash my browser a few times ^^ –  Martijn Jun 23 at 8:53
23  
+1 for the nerve of feeding 500 popup to the user –  edc65 Jun 23 at 8:55
    
Haha. I tested it with console.log() obviously, but this is shorter. –  Martijn Jun 23 at 8:56
1  
Your Javascript solution counts to 501. –  nderscore Jun 23 at 15:22
1  
PHP for 52: <?for(;500>$i++;)echo$i%3*!strpbrk($i,3)?$i:bzzt,~õ; The õ is char 245, a bit-inverted \n. –  primo Jun 24 at 8:10

GolfScript, 30 29 characters

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

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

share|improve this answer

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)]
share|improve this answer
1  
A little shorter: for i in range(501):print[i,'Bzzt'][i%3<1or'3'in`i`] –  grc Jun 23 at 8:00
2  
One char shorter: ['3'[:i%3]in`i`] –  xnor Jun 23 at 23:25

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.

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

C# (71)

Can be directly executed in LinqPad.

for(var i=0;++i<501;)(i%3<1|(i+"").Contains("3")?"buzz":i+"").Dump();
share|improve this answer
1  
You can save a char by using bitwise-or (|) instead of logical-or. –  Johnbot Jun 24 at 9:23
    
@Johnbot Thanks for your suggestion. –  EvilFonti Jun 24 at 10:58
    
You can remove the parentheses around the conditional too. –  Johnbot Jun 24 at 11:09
    
Thank you again, post updated to match your sugestion –  EvilFonti Jun 26 at 7:24
    
@Johnbot: That's not bitwise-or. It's (non-short-circuiting) logical-or, since the operands are boolean. –  Ryan M Jun 26 at 21:43

Haskell: 88 82 80

main=mapM_ f[1..500]
f n|n`mod`3==0||'3'`elem`show n=putStrLn"bzzt"
f n=print n
share|improve this answer

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.

share|improve this answer
1  
I think you could start with for(a=i="";i<500;)a+=++i ... –  grc Jun 23 at 8:21
    
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) –  edc65 Jun 23 at 8:51

PowerShell, 42

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

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

share|improve this answer

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)
share|improve this answer
    
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. –  Joey Jun 23 at 9:13
    
Furthermore, you can use == instead of EQU and probably drop a few spaces here and there. –  Joey Jun 23 at 9:14
    
@Joey Thanks for the suggestions! –  Ourous Jun 23 at 9:42

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.

share|improve this answer
    
Correct me if I am wrong, but this does not seem to bzzt numbers containing 3's, e.g. 13. –  Thor Jun 24 at 10:37
    
@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. –  Doktoro Reichard Jun 24 at 13:59
    
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. –  Thor Jun 24 at 15:07
    
@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) –  Doktoro Reichard Jun 24 at 15:26

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
share|improve this answer

Julia 64 bytes

map(x->x%3==0||contains(string(x),"3")?"Bzzt":string(x),[1:500])
share|improve this answer
2  
[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. –  gggg Jun 25 at 19:45

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

share|improve this answer
1  
+1. You can swap (1..500).each for 500.times –  Will P Jun 27 at 17:20

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)));}
share|improve this answer
    
This has a small glitch - it skips the output for i = 0 –  anatolyg Jun 23 at 17:27
4  
@anatolyg "You must count exactly to 500, starting at 1 or 0 (you chose)." — I chose to start from 1. –  squeamish ossifrage Jun 23 at 17:45
    
Missed that bit. Sorry! –  anatolyg Jun 23 at 18:31
    
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! –  DreamWarrior Jun 24 at 22:33

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);}
share|improve this answer
    
1 bzzt 3 4 bzzt 6 7 bzzt 9 10 bzzt bzzt 13 bzzt ... There's something wrong here. –  squeamish ossifrage Jun 23 at 19:05
    
It seems I was too greedy, and stumbled into Undefined Behavior (variable modified and read between sequence points). Now fixed. –  anatolyg Jun 23 at 19:20
    
Working now :-) –  squeamish ossifrage Jun 23 at 22:18

Mathematica, 54 Characters

This feels too straightforward. Shorter solutions must be possible.

If[DigitCount[#][[3]] > 0 || #~Mod~3 < 1, Bzzt, #] & /@ Range@500
share|improve this answer
    
You don't need the apostrophes :) –  belisarius Jun 24 at 0:59
    
@belisarius indeed. Thanks. –  Michael Stern Jun 24 at 1:20

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

share|improve this answer
    
At first I thought your code is 2012.96 bytes long. –  nyuszika7h Jun 23 at 16:25
    
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 –  domager Jun 23 at 16:29
    
@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 –  Michael B Jun 23 at 17:13
    
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. –  Peter Taylor Jun 23 at 22:00
    
+= 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). –  Michael B Jun 24 at 12:56

Bash, 53 52 48 46

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

Requires GNU sed (uses the c extension).

share|improve this answer

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");}}
share|improve this answer
1  
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. –  WozzeC Jun 23 at 14:29

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"
share|improve this answer
    
Since many string operation functions (such as strsplit) throw error when fed with non-characters I assumed grepl did as well. Nice catch! +1 –  plannapus Jun 26 at 7:43
2  
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 –  plannapus Jun 26 at 7:45

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.

share|improve this answer

Scala, 70

Code

1 to 500 map(x=>if(x%3<1||(""+x).contains(51))"bzzt"else x)map println

Usage

scala -e '1 to 500 map(x=>if(x%3<1||(""+x).contains(51))"bzzt"else x)map println'

Demo

http://ideone.com/KfcO1M

Explanations

  • 1 to 500: produces values from 0 to 500
  • map: function which takes iterable argument at the left, and applies the function given at the right
  • x=>...: lambda expression
  • if(condition) value_if_condition_is_true else value_if_condition_is_false: returns a value (functional behavior) which can be either a java.lang.String or a scala.Int. The expression type is scala.Any in this case, because this is the lower type the two types can coerce to (see the Scala Class Hierarchy)
  • map println: takes each object (java.lang.String or scala.Int) and prints it with line feeds
share|improve this answer

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.

share|improve this answer

JavaScript - 57

Another JavaScript example:

for(i=0;i<500;)alert((++i+'').indexOf(3)<0&&i%3?i:'bzzt')
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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