PYTHON 3.0 Create one file named program105.py containing a main function and a
ID: 670786 • Letter: P
Question
PYTHON 3.0
Create one file named program105.py containing a main function and a recursive function named findmod. The findmod function should compute a mod b but without using the % operator.
In main:
prompt the user to enter two positive integers.
call the findmod function with these integers as arguments.
print its returned value.
The findmod function, findmod(integer1, integer2), must recursively calculate the remainder and then return it.
SAMPLE OUTPUT
Enter a positive integer 17
Enter another positive integer 5
The remainder is 2
SAMPLE OUTPUT
Enter a positive integer 30
Enter another positive integer 8
The remainder is 6
Explanation / Answer
total = 0; # This is global variable.
# Function definition is here
def sum( arg1, arg2 ):
# Add both the parameters and return them."
total = arg1 + arg2; # Here total is local variable.
print "Inside the function local total : ", total
return total;
# Now you can call sum function
sum( 10, 20 );
print "Outside the function global total : ", total
import itertools
import ent # Download from: http://wstein.org/ent/ent_py
def parity(n)
"""Assume Goldbach's Conjecture: all even numbers greater than 2 can
be expressed as the sum of two primes.
Not guaranteed to be efficient, or even succeed, for large n.
"""
# A few quick checks
if n in (-2, 0, 2): return "Even"
elif n in (-1, 1): return "Odd"
if n < 0: n = -n # a bit faster than abs(n)
# The primes generator uses the Sieve of Eratosthenes
# and thus modulo, so this is a little bit cheating
primes_to_n = ent.primes(n)
# Still one more easy way out
if primes_to_n[-1] == n: return "Odd"
# Brutish!
elif n in (p1+p2 for (p1, p2) in itertools.product(primes_to_n, primes_to_n)):
return "Even"
else:
return "Odd"
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.