I found this to be an interesting task from a regional math contest.
Given:
,
even
,
odd
Find .
Starting with a_6, the sequence repeats with for numbers of the form
(see proof below). 2018 is of the form 3k + 2, thus the answer is 1.
Checking it with Idris:
testseq : Integer -> Integer
testseq 0 = 6
testseq k = let previous = testseq (k - 1) in
if (mod previous 2 == 0) then
(div previous 2) else
3 * previous + 1
infiniteSeq : Stream Nat
infiniteSeq = map (\x => fromIntegerNat (testseq x)) [0..]
-- *test> take 20 infiniteSeq
-- [6, 3, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1, 4, 2, 1, 4, 2, 1, 4, 2] : List Integer
getItemFromSeq : Nat -> Nat
getItemFromSeq n = index n infiniteSeq
-- *test> getItemFromSeq 2018
-- 1 : Integer
Proof for repeating sequence. Given:
,
even
,
odd
Prove that the sequence repeats , i.e. for any triplet
we have that it equals to some of
.
Base case:
Inductive step:
Case 1: Assume that
Since , by definition we have that
.
Thus , which is what we needed to show.
Similarly cases 2 and 3 are proven.
Thus the sequence repeats .