読者です 読者をやめる 読者になる 読者になる

fizzbuzz

prolog

僕のprolog力を精一杯使って解いた!

fizzbuzz(N) :- fizzbuzz_sub(1, N).
fizzbuzz_sub(Current, Max) :- Current > Max, !.
fizzbuzz_sub(Current, Max) :-
        (Current mod 15 =:= 0 -> write('fizzbuzz');
         Current mod 3 =:= 0 -> write('fizz');
         Current mod 5 =:= 0 -> write('buzz');
         write(Current)),
        nl,
        Next is Current + 1,
        fizzbuzz_sub(Next, Max).

実行結果

| ?- fizzbuzz(20).
fizzbuzz(20).
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
17
fizz
19
buzz

yes
| ?-