2. Write a script that calculates the common factors between 18 and 54. To find
ID: 3740524 • Letter: 2
Question
2. Write a script that calculates the common factors between 18 and 54. To find a common factor, you can use the modulo operator (%) to check whether a number can be evenly divided into both numbers. Hint, test will be something like 12 % 3 = 0 and 30 % 3 = 0 , the modulo gives you the remainder,if the remainder is zero then it is a factor. ie 10 and 30 are both divisible by 3. 4 is not a common factor because it does not divide evenly into both numbers. You will need a loop to complete this easily . Then, this script should print lines that display the common factors similar to this: Common factors of 18 and 54 1 2 ….. and the remaining common factors, SQL
Explanation / Answer
Python2.7 Scipt:
#!/usr/bin/python2.7
"""
This funtion prints the comman factors between the given numbers
"""
def common_factors(Num1, Num2):
print "Common divisor btwn {0} and {1} are : ".format(Num1, Num2)
MaxNum = max(Num1, Num2)
for Num in range(1, MaxNum / 2):
##if num is common divisor.
if Num1 % Num == 0 and Num2 % Num == 0:
print Num,
common_factors(18, 54)
Output shown after running this program:
Common divisor btwn 18 and 54 are :
1 2 3 6 9 18
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.