Reputation: 1229
I need to Write a QuickCheck property to check the Collatz conjecture for a given number greater than 0.
My challenge is that my code for Collatz conjecture is recursive , so either I get to 1 as expected or loop for ever.
I'm not sure I know how to utilize QuickCheck is this situation.
Any idea ?
Upvotes: 1
Views: 132
Reputation: 51029
I assume this is for an assignment to practice writing QuickCheck properties?
I suspect that the intention is that you write a test that succeeds if the Collatz recursion terminates with 1 (as it is conjectured to do for all positive integers) and loops forever otherwise. In other words, you're not expected to detect that the recursion is looping forever (which you could only do by way of an approximation anyway, by giving up if it recursed for "too long").
So, if you have a recursive function collatz :: Int -> Int
that returns 1
, then your QuickCheck property will ultimately boil down to collatz n == 1
. Depending on what you've learned about QuickCheck properties, you may just be expected to write: prop_collatz n = collatz n == 1
with an appropriate type signature. Or, maybe you're supposed to use the Property
type and forAll
, and maybe you're supposed to use the Positive
type to test only positive integers. It's hard to know what your instructor is expecting.
I would suggest looking at some examples from class where you tested properties on positive or non-negative or otherwise constrained sets of integers and follow that example.
Upvotes: 1