Question

Write a function that takes a list of integer number and returns the greatest common divisor...

Write a function that takes a list of integer number and returns the greatest common divisor of all numbers in the list.
WRITE THE CODE IN PYTHON
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python code to calculate GCD of a list of integers is given below (gcd_list). gcd_list uses for loop to iterate through the list and in turn calls gcd function (defined at the top) to get GCD of two numbers at a time. You can pass your list of integers to gcd_list and it'll return the GCD value for the complete list.

#gcd function to calculate GCD of two numbers
def gcd (a,b):
    if (b == 0):
        return a
    else:
        return gcd (b, a % b)

#gcd_list function to calculate GCD for list of numbers using for loop and gcd function
def gcd_list (listOfNums):
    gcdVal = listOfNums[0]
    for c in listOfNums[1::]:
        gcdVal = gcd (gcdVal , c)
    return gcdVal

#Sample Runs

#Define list of integers
intList1 = [6, 12, 30, 36, 60, 90, 99]
#Print final GCD value by calling gcd_list with intList as argument
print (gcd_list(intList1))
#Define list of integers
intList2 = [2, 4, 6, 8, 10]
#Print final GCD value by calling gcd_list with intList as argument
print (gcd_list(intList2))
intList3 = [10, 15, 5, 25, 20]
#Print final GCD value by calling gcd_list with intList as argument
print (gcd_list(intList3))

OUTPUT:

3                                                                                                                                                                                 

2                                                                                                                                                                                 

5                                                                                                                                                                                 

Add a comment
Know the answer?
Add Answer to:
Write a function that takes a list of integer number and returns the greatest common divisor...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Python!!! Write a recursive function gcd(m,n) that returns the greatest common divisor of a pair...

    Using Python!!! Write a recursive function gcd(m,n) that returns the greatest common divisor of a pair of numbers. The gcd of m and n is the largest number that divides both m and n. If one of the numbers is 0, then the gcd is the other number. If m is greater than or equal to n, then the gcd of m and n is the same as the gcd of n and m-n. If n is greater than m,...

  • Write Java program to take two numbers as input and find the greatest common divisor among...

    Write Java program to take two numbers as input and find the greatest common divisor among the two numbers. You will need to implement a method takes two parameters as input. Sample Input and Output: Enter first integer: 80 Enter second integer: 160 The greatest common divisor for 80 and 160 is 80 Enter first integer: 60 Enter second integer: 185 The greatest common divisor for 60 and 185 is 5

  • I want the code in C++ The greatest common divisor (GCD) of two integers is the...

    I want the code in C++ The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the numbers. Write a function called GCD that has a void return type, and accepts 3 parameters (first two by value, third by reference). The function should find the greatest common divisor of the first two numbers, and have the result as its OUTGOING value. Write a main function that asks the users for two integers, and...

  • (Recursive Greatest Common Divisor) The greatest common divisor of integers x and y is the largest...

    (Recursive Greatest Common Divisor) The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a recursive function gcd that returns the greatest common divisor of x and y. The gcd of x and y is defined recursively as follows: If y is equal to 0, then gcd(x, y) is x; otherwise gcd(x, y) is gcd(y, x % y), where % is the remainder operator. c programming need the whole...

  • PYTHON In mathematics, the Greatest Common Divisor (GCD) of two integers is the largest positive integer...

    PYTHON In mathematics, the Greatest Common Divisor (GCD) of two integers is the largest positive integer that divides the two numbers without a remainder. For example, the GCD of 8 and 12 is 4. Steps to calculate the GCD of two positive integers a,b using the Binary method is given below: Input: a, b integers If a<=0 or b<=0, then Return 0 Else, d = 0 while a and b are both even do a = a/2 b = b/2...

  • Write a function findEvens that takes an integer, n, as input and returns the list of...

    Write a function findEvens that takes an integer, n, as input and returns the list of even integers between 1 and n. Ex. Input: 10 Output: [2,4,6,8,10] Write a function sortList that takes in a list of strings and returns a list of those strings now sorted and lowercase. Ex. Input: [‘ABE’,’CAD’,’gaB’] Output: [‘abe’,’acd’,’abg’] Both done in Python

  • IN PYTHON Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd)...

    IN PYTHON Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd) of two positive integers. gcd is the largest integer that divides evenly into both of them. For example, the gcd(102, 68) = 34. You may recall learning about the greatest common divisor when you learned to reduce fractions. For example, we can simplify 68/102 to 2/3 by dividing both numerator and denominator by 34, their gcd. Finding the gcd of huge numbers is an...

  • C++ I do not understand this problem. 14THE GREATEST COMMON DENOMINATOR PROBLEM Write a function named...

    C++ I do not understand this problem. 14THE GREATEST COMMON DENOMINATOR PROBLEM Write a function named god() that accepts two integers as input parameters and returns the greatest common divisor of the two numbers. The greatest common divisor (GCD) of two integers, a and b, is the largest integer that is a factor of both a and b. The GCD of any number and 1 is 1, and the GCD of any number and o is that number. One efficient...

  • Write a java recursive program to calculate the greatest common divisor of two integer numbers. The...

    Write a java recursive program to calculate the greatest common divisor of two integer numbers. The program asks user to type two numbers a and b(suppose a>b). If b is 0, return a; else recursively call the method with two smaller parameters, one is b, the second is a mod b.

  • IN PYTHON: Write a function that takes, as an argument, a positive integer n, and returns...

    IN PYTHON: Write a function that takes, as an argument, a positive integer n, and returns a LIST consisting of all of the digits of n (as integers) in the same order. Name this function intToList(n). For example, intToList(123) should return the list [1,2,3].

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT