Question: Write a Haskell program for the following: The function collatz :: Int -> Int is defined by collatz(n) = n/2 if n is even and

Write a Haskell program for the following:

 Write a Haskell program for the following: The function collatz ::

The function collatz :: Int -> Int is defined by collatz(n) = n/2 if n is even and collatz(n) = 3n + 1 if n is odd. It is believed that if collatz is applied repeatedly, then eventually one will see the num- ber 1. For example, collatz (5) = 16, and collatz (16) = 8, and collatz (8) = 4, and collatz (4) = 2, and collatz (2) = 1. The Collatz index of a num- ber is the minimum number of times that collatz needs to be applied to obtain 1. For example, the Collatz index of 5 is 5, and the Collatz index of 7 is 16 (the sequence is [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10,5, 16,8, 4, 2, 1). Write a Haskell function collatzIndex :: Int -> SF [Int] which computes the Collatz "index" of a number by calculating the sequence ending in 1. What happens if a number has no Collatz index? The function collatz :: Int -> Int is defined by collatz(n) = n/2 if n is even and collatz(n) = 3n + 1 if n is odd. It is believed that if collatz is applied repeatedly, then eventually one will see the num- ber 1. For example, collatz (5) = 16, and collatz (16) = 8, and collatz (8) = 4, and collatz (4) = 2, and collatz (2) = 1. The Collatz index of a num- ber is the minimum number of times that collatz needs to be applied to obtain 1. For example, the Collatz index of 5 is 5, and the Collatz index of 7 is 16 (the sequence is [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10,5, 16,8, 4, 2, 1). Write a Haskell function collatzIndex :: Int -> SF [Int] which computes the Collatz "index" of a number by calculating the sequence ending in 1. What happens if a number has no Collatz index

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!