Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,19 @@
:- module hailstone.
:- interface.
:- import_module int, list.
:- func hailstone(int) = list(int).
:- pred hailstone(int::in, list(int)::out) is det.
:- implementation.
hailstone(N) = S :- hailstone(N, S).
hailstone(N, [N|S]) :-
( N = 1 -> S = []
; N mod 2 = 0 -> hailstone(N/2, S)
; hailstone(3 * N + 1, S) ).
:- end_module hailstone.

View file

@ -0,0 +1,36 @@
:- module test_hailstone.
:- interface.
:- import_module io.
:- pred main(io.state::di, io.state::uo) is det.
:- implementation.
:- import_module int, list.
:- import_module hailstone.
:- pred longest(int::in, int::out, int::out) is det.
:- pred longest(int::in, int::in, int::in, int::out, int::out) is det.
longest(M, N, L) :- longest(M, 0, 0, N, L).
longest(N, CN, CL, MN, ML) :-
( N > 1 ->
L = list.length(hailstone(N)),
( L > CL -> longest(N - 1, N, L, MN, ML)
; longest(N - 1, CN, CL, MN, ML) )
; MN = CN, ML = CL ).
main(!IO) :-
S = hailstone(27),
( list.length(S) = 112,
list.append([27, 82, 41, 124], _, S),
list.remove_suffix(S, [8, 4, 2, 1], _),
longest(100000, 77031, 351) ->
io.write_string("All tests succeeded.\n", !IO)
; io.write_string("At least one test failed.\n", !IO) ).
:- end_module test_hailstone.

View file

@ -0,0 +1 @@
:- pred longest(int::in, int::out, int::out) is det.