General FizzBuzz: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed whitespace and changed some comments, added templates for the output sections.)
(Added Prolog solution)
Line 2,518: Line 2,518:
Buzz
Buzz
PS></pre>
PS></pre>

=={{header|Prolog}}==
Assuming the user specifies as input two facts of the form:
<lang prolog>maxNumber(105).
factors([(3, "Fizz"), (5, "Buzz"), (7, "Baxx")]).</lang>

A simple Prolog solution to the generalised FizzBuzz problem is as follows:

<lang prolog>fizzbuzz(N,Res) :-
factors(Fs), fizzbuzz(N,Fs,S),
( (S = "", Res is N) ; Res = S ).

fizzbuzz(_,[],"").
fizzbuzz(N,[(F,S)|Fs],Res) :-
fizzbuzz(N,Fs,OldRes),
( N mod F =:= 0, string_concat(S,OldRes,Res) ; Res = OldRes ).

loop(B,B).
loop(A,B) :- A < B, fizzbuzz(A,Res), writeln(Res), Next is A+1, loop(Next,B).

go :- maxNumber(M), MLast is M+1, loop(1,MLast).</lang>

It is worth noting that
<lang prolog>factors([(3, "Fizz"), (5, "Buzz")]).</lang>
corresponds to basic FizzBuzz and that the proposed solution can handle an arbitrary number of factors.



=={{header|Python}}==
=={{header|Python}}==