:- use_module(balance).
:- use_module(library(aggregate)).

prime(N) :-
    M is floor(sqrt(N)),
    between(2, M, K),
    N mod K =:= 0, !, fail.
prime(_).

/* sequential */
count(N) :-
   aggregate_all(sum(M), (between(1, 1000, Y), slice(Y, M)), N).

/* parallel */
count2(N, C) :-
   aggregate_all(sum(M),
                 balance(between(1, 1000, Y),
                         slice(Y, M),
                         [threads(C)]),
                 N).

slice(Y, M) :-
   aggregate_all(count, (H is Y*1000, L is H-999, between(L, H, X), prime(X)), M).
