I have to write a program using recursion that, given n, returns the minimum value m, 1 ≤ m ≤ n, that generates the longest sequence of Collatz Steps
For example 20, the number that has to return is 18, because 18 and 19 both generate the most amount of steps (20) to 1, but 18 is smaller. We got a couple examples of what correct answers look like:
*Main> longestSequenceTo 13
9
*Main> longestSequenceTo 30
27
*Main> longestSequenceTo 88
73
*Main> longestSequenceTo 1121
871
My biggest issue is that we're not allowed to use lists, and our knowledge of Haskell is very basic still. I've managed to write the collatz conjecture as a function, and I think I know what my condition should be for the recursion on the main function:
collatz :: Integer->Integer
collatz 1 = 0
collatz n | even n = 1 + collatz (div n 2)
| otherwise = 1 + collatz (3*n+1)
i think my function should look something like:
longestSequence n | collatz n <= collatz (n-1) =
| otherwise =
my problem is I dont know how to "store" the actual n, instead of getting the number of steps a number has to take to reach 1