Page 1 of 1

## 4 Replies - 950 Views - Last Post: 30 June 2011 - 01:49 PM

### #1 modi123_1

• Suitor #2

Reputation: 14045
• Posts: 56,207
• Joined: 12-June 08

Posted 30 June 2011 - 10:05 AM

http://tech.puredang...ojure-fizzbuzz/

An interesting look at a common question...

Quote

• Print “Fizz” if number is a multiple of 3
• Print “Buzz” if number is a multiple of 5
• Print “FizzBuzz” if number is a multiple of both
• Otherwise, print the number

Is This A Good Question/Topic? 2

## Replies To: [link] Clojure FizzBuzz

### #2 I X Code X 1

Reputation: 47
• Posts: 66
• Joined: 28-July 10

Posted 30 June 2011 - 10:58 AM

Well, this isn't done in Clojure, but it is done in Common Lisp. As far as I can tell this is working correctly. Just wrote it up in like 10 minutes so hope I didn't miss something!

```(defun fizz-buzz ()
"Fizz if a multiple of 3, Buzz if multiple of 5, Fizz-Buzz if multiple of both"
(loop for x from 1 to 100 do
(cond
((and (equal (mod x 3) 0) (equal(mod x 5) 0)) (format t "~a - Fizz-Buzz~%" x))
((equal (mod x 3) 0) (format t "~a - Fizz~%" x))
((equal (mod x 5) 0) (format t "~a - Buzz~%" x))
(t (format t "~a~%" x)))))
```

```COMMON-LISP-USER> (fizz-buzz)

1
2
3 - Fizz
4
5 - Buzz
6 - Fizz
7
8
9 - Fizz
10 - Buzz
11
12 - Fizz
13
14
15 - Fizz-Buzz
16
17
18 - Fizz
19
20 - Buzz
21 - Fizz
22
23
24 - Fizz
25 - Buzz
26
27 - Fizz
28
29
30 - Fizz-Buzz
31
32
33 - Fizz
34
35 - Buzz
36 - Fizz
37
38
39 - Fizz
40 - Buzz
41
42 - Fizz
43
44
45 - Fizz-Buzz
46
47
48 - Fizz
49
50 - Buzz
51 - Fizz
52
53
54 - Fizz
55 - Buzz
56
57 - Fizz
58
59
60 - Fizz-Buzz
61
62
63 - Fizz
64
65 - Buzz
66 - Fizz
67
68
69 - Fizz
70 - Buzz
71
72 - Fizz
73
74
75 - Fizz-Buzz
76
77
78 - Fizz
79
80 - Buzz
81 - Fizz
82
83
84 - Fizz
85 - Buzz
86
87 - Fizz
88
89
90 - Fizz-Buzz
91
92
93 - Fizz
94
95 - Buzz
96 - Fizz
97
98
99 - Fizz
100 - Buzz
```

EDIT: Thought it might be cool to not just have to do through 100. So this allows you to pick where you want it to start and end.

```(defun fizz-buzz (f B)/>
"Fizz if a multiple of 3, Buzz if multiple of 5, Fizz-Buzz if multiple of both"
(loop for x from f to b do
(cond
((and (equal (mod x 3) 0) (equal(mod x 5) 0)) (format t "~a - Fizz-Buzz~%" x))
((equal (mod x 3) 0) (format t "~a - Fizz~%" x))
((equal (mod x 5) 0) (format t "~a - Buzz~%" x))
(t (format t "~a~%" x)))))
```

```COMMON-LISP-USER> (fizz-buzz 100 125)

100 - Buzz
101
102 - Fizz
103
104
105 - Fizz-Buzz
106
107
108 - Fizz
109
110 - Buzz
111 - Fizz
112
113
114 - Fizz
115 - Buzz
116
117 - Fizz
118
119
120 - Fizz-Buzz
121
122
123 - Fizz
124
125 - Buzz
```

This post has been edited by I X Code X 1: 30 June 2011 - 12:14 PM

### #3 Raynes

• D.I.C Lover

Reputation: 614
• Posts: 2,815
• Joined: 05-January 09

Posted 30 June 2011 - 11:48 AM

Neat. It's a good example of taking big problems and factoring them into smaller problems solved by functions that are later composed to form a solution to the big problem. Functional programming is great, boys and girls.

### #4 I X Code X 1

Reputation: 47
• Posts: 66
• Joined: 28-July 10

Posted 30 June 2011 - 12:34 PM

It's a little bit like this problem. There are a lot of great exercises on this web site. Some are extremely difficult and take a lot of thinking!

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08