SET UP • Program factoid.py is concerned with shades of primality • Module primal.py provides three functions to assist
PYTHON FILES Program Program factoid.py factoid.py import primal a1, b1 = 6, 2 a2, b2 = 7, 3 a3, b3 = 4, 5 b1 = primal.is_factor( a1, b1 ) b2 = primal.is_factor( a2, b2 ) b3 = primal.is_factor( a3, b3 ) print( b1 ) print( b2 ) print( b3 )
PYTHON FILES Program Program factoid.py factoid.py Module primal.py Module primal.py import primal def is_factor( x, y ) : a1, b1 = 6, 2 rem = x % y a2, b2 = 7, 3 if ( rem == 0 ) : a3, b3 = 4, 5 result = True else: b1 = primal.is_factor( a1, b1 ) result = False b2 = primal.is_factor( a2, b2 ) b3 = primal.is_factor( a3, b3 ) return result print( b1 ) def are_relative_primes( x, y ) : print( b2 ) ... print( b3 )
Program Trace – begins with the program code, traditionally called the main Program Program factoid.py factoid.py main main import primal a1, b1 = 6, 2 a2, b2 = 7, 3 a3, b3 = 4, 5 r1 = primal.is_factor( a1, b1 ) r2 = primal.is_factor( a2, b2 ) r3 = primal.is_factor( a3, b3 ) print( r1 ) print( r2 ) print( r3 )
Program Trace – execution steps through program Program Program factoid.py factoid.py main main Module primal.py Module primal.py import primal import primal def is_factor( x, y ) : a1, b1 = 6, 2 rem = x % y a2, b2 = 7, 3 if ( rem == 0 ) : a3, b3 = 4, 5 result = True else: r1 = primal.is_factor( a1, b1 ) result = False r2 = primal.is_factor( a2, b2 ) r3 = primal.is_factor( a3, b3 ) return result print( r1 ) def are_relative_primes( x, y ) : print( r2 ) ... print( r3 )
Program Trace – execution steps through program Program factoid.py Program factoid.py main main Module Module primal.py primal.py a1 6 import primal def is_factor( x, y ) : b1 2 a1, b1 = 6, 2 a1, b1 = 6, 2 rem = x % y a2, b2 = 7, 3 if ( rem == 0 ) : a3, b3 = 4, 5 result = True else: r1 = primal.is_factor( a1, b1 ) result = False r2 = primal.is_factor( a2, b2 ) r3 = primal.is_factor( a3, b3 ) return result print( r1 ) def are_relative_primes( x, y ) : print( r2 ) ... print( r3 )
Program Trace – execution steps through program Program factoid.py Program factoid.py main main Module Module primal.py primal.py a1 6 import primal def is_factor( x, y ) : b1 2 a2 7 a1, b1 = 6, 2 rem = x % y b2 3 a2, b2 = 7, 3 a2, b2 = 7, 3 if ( rem == 0 ) : a3, b3 = 4, 5 result = True else: r1 = primal.is_factor( a1, b1 ) result = False r2 = primal.is_factor( a2, b2 ) r3 = primal.is_factor( a3, b3 ) return result print( r1 ) def are_relative_primes( x, y ) : print( r2 ) ... print( r3 )
Program Trace – execution steps through program Program factoid.py Program factoid.py main main Module primal.py Module primal.py a1 6 import primal def is_factor( x, y ) : b1 2 a2 7 a1, b1 = 6, 2 rem = x % y b2 3 a2, b2 = 7, 3 if ( rem == 0 ) : a3 4 a3, b3 = 4, 5 a3, b3 = 4, 5 result = True b3 5 else: r1 = primal.is_factor( a1, b1 ) result = False r2 = primal.is_factor( a2, b2 ) r3 = primal.is_factor( a3, b3 ) return result print( r1 ) def are_relative_primes( x, y ) : print( r2 ) ... print( r3 )
Program Trace – function invocation causes transfer of control Program factoid.py Program factoid.py main main Module Module primal.py primal.py a1 6 import primal def is_factor( x, y ) : b1 2 a2 7 a1, b1 = 6, 2 rem = x % y b2 3 a2, b2 = 7, 3 if ( rem == 0 ) : a3 4 a3, b3 = 4, 5 result = True b3 5 else: r1 = 1 = primal primal.is_factor .is_factor( a1, b1 ) ( a1, b1 ) result = False r2 = primal.is_factor( a2, b2 ) r3 = primal.is_factor( a3, b3 ) return result print( r1 ) def are_relative_primes( x, y ) : print( r2 ) ... print( r3 )
Program Trace – copies of arguments are passed (assigned) to parameters Program factoid.py Program factoid.py main main Module Module primal.py primal.py a1 6 import primal def is_factor( x, y ) : b1 2 a2 7 a1, b1 = 6, 2 rem = x % y b2 3 is_factor is_factor() () a2, b2 = 7, 3 if ( rem == 0 ) : a3 4 x a3, b3 = 4, 5 result = True b3 5 y else: r1 ??? r1 = 1 = primal primal.is_factor .is_factor( a1, b1 ) ( a1, b1 ) result = False r2 = primal.is_factor( a2, b2 ) r3 = primal.is_factor( a3, b3 ) return result print( r1 ) def are_relative_primes( x, y ) : print( r2 ) ... print( r3 )
Program Trace – copies of arguments are passed (assigned) to parameters Program factoid.py Program factoid.py main main Module Module primal.py primal.py a1 6 import primal def is_factor( x, y ) : b1 2 a2 7 a1, b1 = 6, 2 rem = x % y b2 3 is_factor() is_factor () a2, b2 = 7, 3 if ( rem == 0 ) : a3 4 x 6 a3, b3 = 4, 5 result = True b3 5 y else: r1 ??? r1 = 1 = primal primal.is_factor .is_factor( a1, b1 ) ( a1, b1 ) result = False r2 = primal.is_factor( a2, b2 ) r3 = primal.is_factor( a3, b3 ) return result print( r1 ) def are_relative_primes( x, y ) : print( r2 ) ... print( r3 )
Program Trace – copies of arguments are passed (assigned) to parameters Program factoid.py Program factoid.py main main Module Module primal.py primal.py a1 6 import primal def is_factor( x, y ) : b1 2 a2 7 a1, b1 = 6, 2 rem = x % y b2 3 is_factor() is_factor () a2, b2 = 7, 3 if ( rem == 0 ) : a3 4 x 6 a3, b3 = 4, 5 result = True b3 5 y else: r1 ??? r1 = 1 = primal primal.is_factor .is_factor( a1, b1 ) ( a1, b1 ) result = False r2 = primal.is_factor( a2, b2 ) r3 = primal.is_factor( a3, b3 ) return result print( r1 ) def are_relative_primes( x, y ) : print( r2 ) ... print( r3 )
Program Trace – copies of arguments are passed (assigned) to parameters Program factoid.py Program factoid.py main main Module Module primal.py primal.py a1 6 import primal def is_factor( x, y ) : b1 2 a2 7 a1, b1 = 6, 2 rem = x % y b2 3 is_factor is_factor() () a2, b2 = 7, 3 if ( rem == 0 ) : a3 4 x 6 a3, b3 = 4, 5 result = True b3 5 y 2 else: r1 ??? r1 = 1 = primal primal.is_factor .is_factor( a1, b1 ) ( a1, b1 ) result = False r2 = primal.is_factor( a2, b2 ) r3 = primal.is_factor( a3, b3 ) return result print( r1 ) def are_relative_primes( x, y ) : print( r2 ) ... print( r3 )
Program Trace – cpu control is passed to function Program factoid.py Program factoid.py main main Module Module primal.py primal.py a1 6 import primal def is_factor( x, y ) : b1 2 a2 7 a1, b1 = 6, 2 rem = x % y b2 3 is_factor is_factor() () a2, b2 = 7, 3 if ( rem == 0 ) : a3 4 x 6 a3, b3 = 4, 5 result = True b3 5 y 2 else: r1 ??? r1 = 1 = primal primal.is_factor .is_factor( a1, b1 ) ( a1, b1 ) result = False r2 = primal.is_factor( a2, b2 ) r3 = primal.is_factor( a3, b3 ) return result print( r1 ) def are_relative_primes( x, y ) : print( r2 ) ... print( r3 )
Program Trace – cpu control is passed to function Program factoid.py Program factoid.py main main Module Module primal.py primal.py a1 6 import primal def is_factor def is_factor( x, y ) : ( x, y ) : b1 2 a2 7 is_factor is_factor() () a1, b1 = 6, 2 rem = x % y b2 3 x 6 a2, b2 = 7, 3 if ( rem == 0 ) : a3 4 y 2 a3, b3 = 4, 5 result = True b3 5 else: r1 ??? r1 = 1 = primal primal.is_factor .is_factor( a1, b1 ) ( a1, b1 ) result = False r2 = primal.is_factor( a2, b2 ) r3 = primal.is_factor( a3, b3 ) return result print( r1 ) def are_relative_primes( x, y ) : print( r2 ) ... print( r3 )
Program Trace – function executes Program factoid.py Program factoid.py main main Module primal.py Module primal.py a1 6 import primal def is_factor( x, y ) : b1 2 a2 7 is_factor() is_factor () a1, b1 = 6, 2 rem = x % y rem = x % y b2 3 x 6 a2, b2 = 7, 3 if ( rem == 0 ) : a3 4 y 2 a3, b3 = 4, 5 result = True b3 5 rem 0 else: r1 ??? r1 = primal.is_factor( a1, b1 ) result = False r2 = primal.is_factor( a2, b2 ) r3 = primal.is_factor( a3, b3 ) return result print( r1 ) def are_relative_primes( x, y ) : print( r2 ) ... print( r3 )
Program Trace – function executes Program Program factoid.py factoid.py main main Module primal.py Module primal.py a1 6 import primal def is_factor( x, y ) : b1 2 a2 7 is_factor is_factor() () a1, b1 = 6, 2 rem = x % y b2 3 x 6 a2, b2 = 7, 3 if ( rem == 0 ) : if ( rem == 0 ) : a3 4 y 2 a3, b3 = 4, 5 result = True b3 5 rem 0 else: r1 ??? r1 = primal.is_factor( a1, b1 ) result = False r2 = primal.is_factor( a2, b2 ) r3 = primal.is_factor( a3, b3 ) return result print( r1 ) def are_relative_primes( x, y ) : print( r2 ) ... print( r3 )
Program Trace – function executes Program factoid.py Program factoid.py main main Module Module primal.py primal.py a1 6 import primal def is_factor( x, y ) : b1 2 a2 7 is_factor() is_factor () a1, b1 = 6, 2 rem = x % y b2 3 x 6 a2, b2 = 7, 3 if ( rem == 0 ) : a3 4 y 2 a3, b3 = 4, 5 result = True result = True b3 5 rem 0 else: r1 ??? result True r1 = primal.is_factor( a1, b1 ) result = False r2 = primal.is_factor( a2, b2 ) r3 = primal.is_factor( a3, b3 ) return result print( r1 ) def are_relative_primes( x, y ) : print( r2 ) ... print( r3 )
Recommend
More recommend