{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

a8q3 - #CS 116 Assignment 8 Question 3 Ru Wang r46wang...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
##CS 116 Assignment 8 Question 3 ## Ru Wang r46wang 20319247 ## collatz: int[>=1] nat -> boolean ## purpose: this function consumes a positive integer and a natural number indicating the maximum iteration it can go through and produces true if the integer reaches 1 with the collatz conjecture within the maximum iterations indicated and false otherwise. ## examples: ## collatz(1, 0) => True ## collatz(2, 0) => False ## collatz(2, 1) => True ## collatz(3, 1) => False ## collatz(17, 11) => False ## collatz(17, 12) => True def collatz(n, max_itn): a = 0 while a < max_itn: if (n == 1): break if (n % 2) == 1: n = (3 * n) + 1 else: n = n / 2 a += 1 return (n == 1) ## Testings: ## collatz(1,0) => True print "part a Test 1" expect = True ans = collatz(1,0) print expect == ans assert expect == ans ## collatz(2, 0) => False print "part a Test 2" expect = False ans = collatz(2, 0) print expect == ans assert expect == ans ## collatz(2, 1) => True print "part a Test 3" expect = True ans = collatz(2,1) print expect == ans assert expect == ans ## collatz(3, 1) => False print "part a Test 4" expect = False ans = collatz(3,1) print expect == ans assert expect == ans ## collatz(17, 11) => False print "part a Test 5" expect = False ans = collatz(17, 11)
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}