Here is mine. I did it without % because I couldn't find the mod function by name in Books Online and did not want to give in and go to Google. I searched for 'mod', 'modulus', and under Mathematical Functions and (after seeing the examples) under '%' all without success. It turns out that the entry is under '% modulo'.
As a result, it took me a lot longer, and I don't know if looking for the decimal is reliable in all cases, but it worked when I ran in it in SQL 2000. Here it is, warts and all. 🙂 Two things in support of my effort, though: (1) I did take less than 10 minutes to do it in ColdFusion since I knew the function for mod and (2) I saw at once that I would have to check for the case of divisible by both 3 and 5 first, or else the if statements would miss that case.
That's a great puzzle - not too hard or too easy. Thanks!
DECLARE @count int
DECLARE @DivResultBy3 float
DECLARE @DivResultBy5 float
SET @count = 1
WHILE @count <= 100
SET @DivResultBy3 = CAST(@count / 3.0 AS varchar(10))
SET @DivResultBy5 = CAST(@count / 5.0 AS varchar(10))
IF (CHARINDEX('.',@DivResultBy3) = 0
AND CHARINDEX('.',@DivResultBy5) = 0)
IF CHARINDEX('.',@DivResultBy3) = 0
IF CHARINDEX('.',@DivResultBy5) = 0
SET @count = @count + 1
I also did it in ColdFusion, for what it's worth:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!--- I was reading the latest Simple-Talk email that linked me to an article on Coding Horror, "Why Can't Programmers - Program?" In the article they talked about a simple test that they gave developers to verify their abilities and decide whether or not to continue the interview. Here's the test: Write code that counts from 1 to 100 For each number evenly divisible by 3, substitute 'Bizz' For each number evenly divisible by 5, substitute 'Buzz' For each number divisible by both substitute 'BizzBuzz' --->
<cfloop index="count" from="1" to="100">
<cfif (count mod 3 is 0) and (count mod 5 is 0)>
<cfelseif count mod 3 is 0>
<cfelseif count mod 5 is 0>