-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfizzbuzz.cfm
103 lines (76 loc) · 2.63 KB
/
fizzbuzz.cfm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!---
Was reading a blog that talked about the rules for FizzBuzz. I know it's a very basic (and old) coding
problem, but I'm pretty much a self-taught guy. I've never actually written FizzBuzz, so I figured I'd
throw it together in SQL, then in CF. It's a cute little thought exercise. And even though it's easy and
fun, that's what toys are for, right? :-)
Rules of the FizzBuzz Puzzle :
Write a program that prints the numbers from 1 to 100. For multiples of three print “Fizz” instead
of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three
and five print “FizzBuzz”. Otherwise print just the number.
--->
<cfscript>
WriteOutput( "FizzBuzz Test <br/><br/>" );
for(counter=1; counter LTE 100; counter++) {
writeme = "";
if ( counter%3 == 0 ) { writeme = "Fizz"; };
if ( counter%5 == 0 ) { writeme = writeme & "Buzz"; };
if ( LEN(writeme) == 0 ) { writeme = counter; };
WriteOutput( writeme & "<br/>" );
} ;
</cfscript>
<!--- SQL:
/* 3263 ms, 185client|3078server */
IF OBJECT_ID('tempdb.dbo.#TempInput') IS NOT NULL
DROP TABLE #TempInput
CREATE TABLE #TempInput (
InputVal INT
, FIZZBUZZ varchar(10)
)
INSERT INTO #TempInput (InputVal)
SELECT TOP (100000) nums = ROW_NUMBER() OVER (ORDER BY sysAll1.object_id)
FROM sys.all_objects AS sysAll1 /* grab lots of IDs */
CROSS JOIN sys.all_objects AS sysAll2
UPDATE #TempInput
SET FIZZBUZZ =
CASE
WHEN InputVal%3 = 0 AND InputVal%5 = 0 THEN 'FizzBuzz'
WHEN InputVal%3 = 0 THEN 'Fizz'
WHEN InputVal%5 = 0 THEN 'Buzz'
ELSE CONVERT(varchar(10),InputVal)
END
SELECT * FROM #TempInput
-------------------------------------------
/* 3271ms, 3248client|23server */
IF OBJECT_ID('tempdb.dbo.#TempInput') IS NOT NULL
DROP TABLE #TempInput
CREATE TABLE #TempInput (
InputVal INT
, FIZZBUZZ varchar(10)
)
DECLARE @Input INT = 1
WHILE @Input <= 100000
BEGIN
INSERT INTO #TempInput (InputVal, FIZZBUZZ)
VALUES (@Input,
CASE
WHEN @Input%3 = 0 AND @Input%5 = 0 THEN 'FizzBuzz'
WHEN @Input%3 = 0 THEN 'Fizz'
WHEN @Input%5 = 0 THEN 'Buzz'
ELSE CONVERT(varchar(10), @Input)
END
)
SET @Input = @Input + 1
END
SELECT * FROM #TempInput
-------------------------------------------
/* 2436ms, 2430client|6server - this method solves the problem but doesn't store the results. */
DECLARE @Input INT = 1
WHILE @Input <= 100000
BEGIN
DECLARE @FizzBuzz VARCHAR(10) = ''
IF @Input%3 = 0 BEGIN SET @FizzBuzz = 'FIZZ' END
IF @Input%5 = 0 BEGIN SET @FizzBuzz = @FizzBuzz + 'BUZZ' END
PRINT ( CASE WHEN LEN(@FizzBuzz) > 0 THEN @FizzBuzz ELSE CONVERT(varchar(10),@Input) END )
SET @Input = @Input+1
END
--->