So I’m trying to learn Prolog and thought of doing some simple problems at SPOJ to practice. However I keep getting WA on COINS (with SWI-Prolog), even though I’m pretty sure my entry works. I solved this problem years ago in C and every test case matches, so it has to be something to do with formatting. I’ve tried to disable the prompt, account for empty lines, etc… I’m pretty much at my wits’ end here, any hints?

program :-
    prompt(_, ''),
    next_test_case.

next_test_case :-
    at_end_of_stream.

next_test_case :-
    \+ at_end_of_stream,
    readln(X),
    do_test_case(X),
    next_test_case.

do_test_case([]) :-
    next_test_case.

do_test_case([X]) :-
    solve(X,S),
    writeln(S),
    next_test_case.

solve(0,0).
solve(N,X) :-
    N > 0,
    N2 is N//2,
    N3 is N//3,
    N4 is N//4,
    solve(N2,S2),
    solve(N3,S3),
    solve(N4,S4),
    X is max(S2+S3+S4,N),
    asserta(solve(N,X)).