General FizzBuzz: Difference between revisions
Content added Content deleted
(Added Prolog solution) |
|||
Line 2,526: | Line 2,526: | ||
A simple Prolog solution to the generalised FizzBuzz problem is as follows: |
A simple Prolog solution to the generalised FizzBuzz problem is as follows: |
||
<lang prolog>fizzbuzz( |
<lang prolog>fizzbuzz(_,[],""). |
||
factors(Fs), fizzbuzz(N,Fs,S), |
|||
⚫ | |||
fizzbuzz(_,[],""). |
|||
fizzbuzz(N,[(F,S)|Fs],Res) :- |
fizzbuzz(N,[(F,S)|Fs],Res) :- |
||
fizzbuzz(N,Fs,OldRes), |
fizzbuzz(N,Fs,OldRes), |
||
( N mod F =:= 0, string_concat(S,OldRes,Res) ; Res = OldRes ). |
( N mod F =:= 0, string_concat(S,OldRes,Res) ; Res = OldRes ). |
||
loop(B,B). |
loop(B,B,_). |
||
loop(A,B,Fs) :- |
|||
loop(A,B) :- A < B, fizzbuzz(A,Res), writeln(Res), Next is A+1, loop(Next,B). |
|||
⚫ | |||
Next is A+1, loop(Next,B,Fs). |
|||
go :- maxNumber(M), MLast is M+1, loop(1,MLast).</lang> |
go :- maxNumber(M), factors(Fs), MLast is M+1, loop(1,MLast,Fs).</lang> |
||
It is worth noting that |
It is worth noting that |
||
<lang prolog>factors([(3, "Fizz"), (5, "Buzz")]).</lang> |
<lang prolog>factors([(3, "Fizz"), (5, "Buzz")]).</lang> |
||
corresponds to basic FizzBuzz and that the proposed solution can handle an arbitrary number of factors. |
corresponds to basic FizzBuzz and that the proposed solution can handle an arbitrary number of factors. |
||
=={{header|Python}}== |
=={{header|Python}}== |