Last active
December 20, 2015 04:29
-
-
Save muupan/6070968 to your computer and use it in GitHub Desktop.
FizzBuzz in Prolog
It would be better to use between/3.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fizzbuzz(_n) :- | |
fizzbuzz_sequence(1, _n). | |
fizzbuzz_sequence(_max, _max) :- | |
fizzbuzz_number(_max), !. | |
fizzbuzz_sequence(_min, _max) :- | |
fizzbuzz_number(_min), | |
_next is _min + 1, | |
fizzbuzz_sequence(_next, _max). | |
fizzbuzz_number(_n) :- | |
_n mod 3 =:= 0, | |
_n mod 5 =:= 0, | |
write('FizzBuzz'), nl, !. | |
fizzbuzz_number(_n) :- | |
_n mod 3 =:= 0, | |
write('Fizz'), nl, !. | |
fizzbuzz_number(_n) :- | |
_n mod 5 =:= 0, | |
write('Buzz'), nl, !. | |
fizzbuzz_number(_n) :- | |
write(_n), nl. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment