@prefix math: <http://www.w3.org/2000/10/swap/math#>.
@prefix list: <http://www.w3.org/2000/10/swap/list#>.
@prefix log: <http://www.w3.org/2000/10/swap/log#>.
@prefix : <https://eyereasoner.github.io/ns#>.

{ ?N :collatz (?N (?N)) } <= {
    true log:callWithCut true.
}.

{ ?N0 :collatz (?N ?M)} <= {
    (   { (?N0 2) math:remainder 0 }
        { (?N0 2) math:integerQuotient ?N1 }
        { ((3 ?N0)!math:product 1) math:sum ?N1 }
    ) log:ifThenElseIn ?SCOPE.
    ?N1 :collatz (?N ?J).
    ?M list:firstRest (?N0 ?J).
}.
