/*** collatz-conjecture ***/

/*
    The Collatz conjecture is that no matter what value of N, the sequence will always reach 1.

    For any positive integer N, to get next integer, do:
        - if N is even, divide by 2
        - if N is odd, multiply by 3 and add 1, i.e 3xN + 1

    Repeat indefinitely to obtain the hailstone sequence N_0, N_1, N_2, ...

    https://en.wikipedia.org/wiki/Collatz_conjecture
*/

:- use_module(library(clpfd)).

hailstone(N, N).

hailstone(N_0, N) :-
    N_0 #= 2 * N_1,
    hailstone(N_1, N).

hailstone(N_0, N) :-
    N_0 #= 2 * _ + 1,
    N_1 #= 3 * N_0 + 1,
    hailstone(N_1, N).

%:- hailstone(3, N), print(N), nl, N = 1.

:- initialization main.

main :- 
    hailstone(3, N), print(N), nl, N = 1,

    halt.

% N = 3
% N = 10
% N = 5
% N = 16
% N = 8
% N = 4
% N = 2
% N = 1

/*
    Michael Sjoeberg
    2019-08-28
    https://github.com/michaelsjoeberg/prolog-playground/blob/master/applications/collatz-conjecture.pl
*/