User:Kithira/Course Pages/CSCI 12/Assignment 2/Group 2/Homework 2
def syracuse(n):
if n%2 == 0: return n/2 else: return 3*n+1
def _test():
assert(syr(10)==5) assert(syr(7)==11)
if __name__ == '__main__':
_test()
Given an integer input, this function outputs the next number in the Syracuse sequence. In the second line, the program checks whether the input is even or odd. n%2==0 means that the integer n, in mod 2, has no remainder (ie, it is an even number). For even numbers, the function outputs the input multiplied by one-half. If the input was an odd number (in the fourth line, the "else" refers to all numbers that are not even), the function outputs the sum of one and input multiplied by three. The program then checks that the function is working correctly, by asserting that the output for an input of 10 should be 5, and the output for an input of 7 should be 11. By checking both an even and an odd number, this test should assure that the function is producing the desired output. The last two lines mean that the program will test the function if this file is being used. It will not test the function if the function has been imported and is being used in another file, for example.