(defun fizz-buzz (n)
(do ((i 1 (+ i 1)))
((> i n))
(let
((fizz (= 0 (mod i 3)))
(buzz (= 0 (mod i 5))))
(if fizz (format t "Fizz"))
(if buzz (format t "Buzz"))
(format t "~A~%"
(if (or fizz buzz) "" i)))))
(fizz-buzz 100)
Another friend of mine commenting on the same FizzBuzz thread supplied
the following Python code. It certainly is concise:
for i in xrange(1,101):
print(str(i), "Fizz", "Buzz", "FizzBuzz")[(i%3==0)|(i%5==0)<<1]
Brian wrote:
(defun fizz-buzz (n)
(do ((i 1 (+ i 1)))
((> i n))
(let
((fizz (= 0 (mod i 3)))
(buzz (= 0 (mod i 5))))
(if fizz (format t "Fizz"))
(if buzz (format t "Buzz"))
(format t "~A~%"
(if (or fizz buzz) "" i)))))
(fizz-buzz 100)
Another friend of mine commenting on the same FizzBuzz thread supplied
the following Python code. It certainly is concise:
for i in xrange(1,101):
print(str(i), "Fizz", "Buzz", "FizzBuzz")[(i%3==0)|(i%5==0)<<1]
newLISP
(define (fizz)
(define (any _test) (exists _test $args))
(define (divis n) (= 0 (mod i n)))
(for (i 1 21)
(if (divis 3) (print 'fizz))
(if (divis 5) (print 'buzz))
(if (divis 7) (print 'zoom))
(unless (any divis 3 5 7) (print i))
(println)))
(fizz)
1
2
fizz
4
buzz
fizz
zoom
8
fizz
buzz
11
fizz
13
zoom
fizzbuzz
16
17
fizz
19
buzz
fizzzoom
Another friend of mine commenting on the same FizzBuzz thread supplied
the following Python code. It certainly is concise:
for i in xrange(1,101):
print(str(i), "Fizz", "Buzz", "FizzBuzz")[(i%3==0)|(i%5==0)<<1]
Another friend of mine commenting on the same FizzBuzz thread supplied
the following Python code. It certainly is concise:
for i in xrange(1,101):
print(str(i), "Fizz", "Buzz", "FizzBuzz")[(i%3==0)|(i%5==0)<<1]
Gauche Scheme
(use srfi-42) ;; do-ec looping macro.
(define (z? n) (if (= 0 n) 1 0))
(define % mod)
(do-ec (: i 1 101)
(print (~ `(,i Fizz Buzz FizzBuzz)
(+ (z? (% i 3)) (* (z? (% i 5)) 2)))))
After removing unnecessary blanks, it's shorter than the
Python version.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 714 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 140:30:07 |
| Calls: | 12,087 |
| Files: | 14,998 |
| Messages: | 6,517,424 |