The Highest Common Factor (HCF) , also called gcd, can be computed in python using a single function offered by math module and hence can make tasks easier in many situations.
- Division In Python 2
- Greatest Common Divisor In Python
- Division In Python Using Int
- Divisor List In Python
- Using Recursion:
# method to compute gcd ( recursion )
def
hcfnaive(a,b):
return
a
return
hcfnaive(b,a
%
b)
a
=
60
print
(
'The gcd of 60 and 48 is : '
,end
=
')
Output: - Using Loops
# method to compute gcd ( Loops )
small
=
y
small
=
x
if
((x
%
i
=
=
0
)
and
(y
%
i
=
=
0
)):
b
=
48
# prints 12
print
(computeGCD(
60
,
48
))
Output: - Using Euclidean Algorithm
# method to compute gcd ( Euclidean algo )
x, y
=
y, x
%
y
return
x
a
=
60
print
(
'The gcd of 60 and 48 is : '
,end
=
')
Output: - Both numbers are 0, gcd is 0
- If only either number is Not a number, Type Error is raised.
Using gcd() can compute the same gcd with just one line.
# method to compute gcd import math # prints 12 print (math.gcd( 60 , 48 )) |
Output:
Some common Exceptions in this function are :
# method exceptions import math # prints 0 print (math.gcd( 0 , 0 )) # Produces error print (math.gcd( 'a' , 13 )) |
Output:
Runtime Error :
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Choose from our most advanced ships and weaponry to engage new enemies, some known, some unexpected. This game supports control re-mapping, but only dual analog game-pads and keyboard & mouse modes are officially supported. Your efforts and piloting skills will determine the outcome of your people and their future.Challenged with blazing fast dog-fights, aggressive defense systems and relentless enemy aggression, you will need razor sharp focus and pure unbridled determination if you are to succeed. This beautiful and brutal universe contains three immersive stories spanning a 15 year period, where you will be at the center of crucial defining moments in Terran history. Now you can download nad try this game for free freegamesdl. Ark pc game download.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
GCD and LCM functions in Python for several numbers
gcd_and_lcm.py
# Greatest common divisor of 1 or more numbers. |
from functools importreduce |
defgcd(*numbers): |
'' |
Return the greatest common divisor of 1 or more integers |
Examples |
-------- |
>>> gcd(5) |
5 |
>>> gcd(30, 40) |
10 |
>>> gcd(120, 40, 60) |
20 |
'' |
# Am I terrible for doing it this way? |
from math import gcd |
returnreduce(gcd, numbers) |
# Least common multiple is not in standard libraries? It's in gmpy, but this |
# is simple enough: |
deflcm(*numbers): |
'' |
Return lowest common multiple of 1 or more integers. |
Examples |
-------- |
>>> lcm(5) |
5 |
>>> lcm(30, 40) |
120 |
>>> lcm(120, 40, 60) |
120 |
'' |
deflcm(a, b): |
return (a * b) // gcd(a, b) |
returnreduce(lcm, numbers, 1) |
# Assuming numbers are positive integers.. |
commented Jun 14, 2012 • edited
edited
commented Jan 6, 2016
Python Program to Find HCF or GCD. To understand this example, you should have the knowledge of following Python programming topics: The highest common factor (H.C.F) or greatest common divisor (G.C.D) of two numbers is the largest positive integer that perfectly divides the two given numbers. For example, the H.C.F of 12 and 14 is 2.
This is the simplest lcm formula I could find. Thanks! |
commented Mar 3, 2017
good, thanks :) |
commented Mar 29, 2017 • edited
edited
For python3 support, maybe you should add (it's also compatible with python2 ) |
commented Nov 1, 2017
For python3 use from math import gcd fraction gcd has been deprecated. |
commented Dec 6, 2017
Division In Python 2
what does *numbers look like? What is the expected parameter? |
commented Jan 7, 2018
Thanks for it!! |
commented Feb 13, 2018
thank you! |
commented Mar 11, 2018
Greatest Common Divisor In Python
Excellent! How elegant it is. |
commented Jun 19, 2018
Division In Python Using Int
For case of n no's in python 2.7 |
commented Jul 18, 2018
thanks very much for the sharing |
commented Aug 19, 2018
Concatenated everything for the convenience. |
commented Jan 14, 2019 • edited
edited
commented Jan 14, 2019
@kovalevcon Why int(float division) instead of int division? |
commented Mar 25, 2019
Easy! Just do: import math ans = math.gcd(8, 10) For lcm, do: import math ans = math.lcm(8, 10) or: import math gcd_ans = math.gcd(8, 10) lcm_ans = 8 * 10 / gcd_ans I got it from this site: https://www.geeksforgeeks.org/gcd-in-python/ Check it out! |
commented May 19, 2019
I think there should be floordiv instead of truediv in your lcm |
commented Jul 21, 2019
` import math def lcm(a, b): return int(a * b / math.gcd(a, b)) print(lcm(2, 6)) ` |
commented Jul 23, 2019
Great code! Can you plz explain me how does GCD works in your program? |
Divisor List In Python
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment