{"id":7575,"date":"2021-09-30T10:40:44","date_gmt":"2021-09-30T05:10:44","guid":{"rendered":"https:\/\/python-programs.com\/?p=7575"},"modified":"2021-11-22T18:35:33","modified_gmt":"2021-11-22T13:05:33","slug":"python-program-to-calculate-the-hcf-gcd","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-calculate-the-hcf-gcd\/","title":{"rendered":"Python Program to Calculate the HCF\/GCD"},"content":{"rendered":"
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output<\/a> & achieve your goal in improving java coding skills.<\/p>\n Highest Common Factor (HCF) \/ Greatest Common Divisor (GCD) :<\/strong><\/p>\n When at least one of the integers is not zero, the greatest positive integer that evenly divides the numbers without a remainder is called the Highest Common Factor or Greatest Common Divisor.<\/p>\n The GCD of 12 and 16 is, for example, 4.<\/p>\n Given two numbers the task is to find the highest common factor of the two numbers in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example4:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n There are several ways to calculate the hcf of the two numbers in python some of them are:<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n The findGcd() function is called with two integers stored in variables number1 and number2. The function computes and returns the H.C.F. of these two integers.<\/p>\n Because the H.C.F can only be less than or equal to the lowest number, we first find the smaller of the two integers in the function. Then, to move from 1 to that number, we utilize a for loop.<\/p>\n We check if our number divides both input numbers precisely in each cycle. If this is the case, the number is saved as H.C.F. We wind up with the greatest integer that divides both numbers properly at the end of the loop.<\/p>\n The method described above is simple to learn and apply, but it is inefficient. The Euclidean algorithm is a significantly more efficient technique of calculating the H.C.F.<\/p>\n This approach is based on the fact that the H.C.F. of two numbers also divides their difference.<\/p>\n We divide the greater by the smaller and take the remainder in this algorithm. Divide the smaller number by the remainder. Repeat until the residual equals zero.<\/p>\n Facts of Euclidean Algorithm:<\/strong><\/p>\n GCD does not change when we subtract a smaller number from a larger number (we reduce a larger number). So, if we keep subtracting the largest of two numbers, we get GCD. Algorithm:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We’ll keep looping until y equals zero. In Python, the statement number1, number2 = number2, number1 % number2 swaps values.<\/p>\n In each iteration, the value of number2 is placed in number1 and the remainder (number1 % number2) is placed in number2. We have H.C.F. in number1 when number2 becomes zero.<\/p>\n This is the simplest and quick method to calculate the greatest common divisor of the given two numbers.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation :<\/strong>It just calculated the gcd in 1 line which is math.gcd() Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills. Highest Common Factor (HCF) \/ Greatest Common Divisor (GCD) : When at least one of the …<\/p>\na= 24\u00a0 \u00a0b=36<\/pre>\n
The Highest Common Factor (HCF) of the numbers 24 36 = 12<\/pre>\n
a= 18 b=72<\/pre>\n
The Highest Common Factor (HCF) of the numbers 18 72 = 18<\/pre>\n
a= 4 b=8<\/pre>\n
The Highest Common Factor (HCF) of the numbers 4 8 = 4<\/pre>\n
a= 9 b=9<\/pre>\n
The Highest Common Factor (HCF) of the numbers 9 9 = 9<\/pre>\n
The Highest common factor of two numbers is number itself if both the numbers are equal<\/pre>\n
Python Program to Calculate the HCF\/GCD<\/h2>\n
\n
Method #1:Using loops<\/h3>\n
# function which computes and returns the highest common factor of the given two numbers\r\ndef findGcd(number1, number2):\r\n\r\n # finding the smaller number\r\n if number1 > number2:\r\n smallNumber = number2\r\n else:\r\n smallNumber = number1\r\n # using for loop to traverse from 1 to smaller number\r\n for i in range(1, smallNumber+1):\r\n # if it divides the given two numbers without leaving the\r\n # remainder then assign result it with that loop iterator value\r\n if((number1 % i == 0) and (number2 % i == 0)):\r\n result = i\r\n # return the final result which is greatest common divisor(GCD)\r\n return result\r\n\r\n\r\n# given two numbers\r\n# given number a\r\nnumber1 = 24\r\n# given number b\r\nnumber2 = 36\r\n# passing the given two numbers to findGcd function which returns the greatest common factor of the given two numbers\r\nans = findGcd(number1, number2)\r\n# print the hcf of the given two numbers\r\nprint(\"The Highest Common Factor (HCF) of the numbers\", number1, number2, \"=\", ans)\r\n<\/pre>\n
The Highest Common Factor (HCF) of the numbers 24 36 = 12<\/pre>\n
Method #2:Using Euclidean algorithm<\/h3>\n
\nInstead of subtracting, we can divide the smaller integer, and the procedure will terminate when we find a remainder of zero.<\/p>\n\n
# function which computes and returns the highest common factor of the given two numbers\r\ndef findGcd(number1, number2):\r\n while(number2):\r\n number1, number2 = number2, number1 % number2\r\n return number1\r\n\r\n\r\n# given two numbers\r\n# given number a\r\nnumber1 = 24\r\n# given number b\r\nnumber2 = 36\r\n# passing the given two numbers to findGcd function which returns the greatest common factor of the given two numbers\r\nans = findGcd(number1, number2)\r\n# print the hcf of the given two numbers\r\nprint(\"The Highest Common Factor (HCF) of the numbers\", number1, number2, \"=\", ans)\r\n<\/pre>\n
The Highest Common Factor (HCF) of the numbers 24 36 = 12<\/pre>\n
Explanation:<\/h3>\n
Method #3:Using gcd() function in math module<\/h3>\n
\n
# importing math module\r\nimport math\r\n# function which computes and returns the highest common factor of the given two numbers\r\n\r\n\r\ndef findGcd(number1, number2):\r\n # calculating gcd using gcd() function\r\n resGcd = math.gcd(number1, number2)\r\n # return the gcd of the number 1 and number2\r\n return resGcd\r\n\r\n\r\n# given two numbers\r\n# given number a\r\nnumber1 = 24\r\n# given number b\r\nnumber2 = 36\r\n# passing the given two numbers to findGcd function which returns the greatest common factor of the given two numbers\r\nans = findGcd(number1, number2)\r\n# print the hcf of the given two numbers\r\nprint(\"The Highest Common Factor (HCF) of the numbers\", number1, number2, \"=\", ans)\r\n<\/pre>\n
The Highest Common Factor (HCF) of the numbers 24 36 = 12<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n