Hướng dẫn python gcd of list
I want to calculate gcd for a list of numbers. But I don't know what's wrong with my code.
sophros 12.5k7 gold badges42 silver badges65 bronze badges asked Mar 22, 2015 at 12:51
3 here is the piece of code, that I used:
answered Jun 13, 2018 at 7:02
2 As of python 3.9, python got built-in support for calculating gcd over a list of numbers.
Output:
answered Aug 5, 2020 at 2:21
bigbountybigbounty 15.3k4 gold badges31 silver badges59 bronze badges
ideone link answered Jul 24, 2015 at 19:42
0 I used this piece of code:
Jianxin Gao 2,4572 gold badges15 silver badges31 bronze badges answered Feb 3, 2019 at 6:50
kouroshkourosh 711 silver badge4 bronze badges If you'd like to use an existing method try `np.gcd.reduce':
which returns answered Apr 23, 2020 at 7:07
uhohuhoh 3,4625 gold badges36 silver badges90 bronze badges 5 It's not clear to me why you are using 12 in your function? Do you want to test your algorithm with 12 specifically? There is built in function that provides a good solution ( If you want to develop your own approach, you could do it this way: sort the list and get the minimum number of list (call it
answered Mar 22, 2015 at 13:27
"Lambda" is an anonymous function where 'g' is assigned with GCD of two numbers whenever called. "Reduce" is a function in "functools" module which is used to perform a particular function to all of the elements in the list. Here reduce() computes the GCD of the complete list A by computing GCD of first two elements, then the GCD of 3rd element with previously computed GCD of first two elements and so on. Hope this clears your doubt. answered Oct 18, 2018 at 11:16
2
answered Mar 22, 2015 at 13:02
DanielDaniel 41.1k4 gold badges54 silver badges80 bronze badges As I see your code will simply go in infinite loop. Since you call method Greatest_Common_Divisor recursively but without base case. Align print Greatest_Common_Divisor(A) and "def" in the same column and that problem would be solved. But still what your code does for each number ai, it takes remainder of ai % 12, and then simply print 12 % (ai % 12), and there is no any connection between it and greatestCommonDivisor. Here is simple code for gcd(a,b) which you can use for the whole array:
answered Mar 22, 2015 at 21:41
MamukaMamuka 1021 silver badge4 bronze badges
I guess this one will clear your doubts. answered May 7, 2018 at 7:37
AnonymousAnonymous 3352 silver badges16 bronze badges gcd of a list input by the user which can be used for any number of input values.
answered Jul 24, 2019 at 17:24
answered Sep 23, 2019 at 17:05
Simply check gcd for minimum and maximum element in list:
answered Apr 7 at 11:27
3 Not the answer you're looking for? Browse other questions tagged python algorithm greatest-common-divisor or ask your own question. |