Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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:
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 indexStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started