{"id":18474,"date":"2021-08-26T10:39:15","date_gmt":"2021-08-26T05:09:15","guid":{"rendered":"https:\/\/python-programs.com\/?p=18474"},"modified":"2021-11-22T18:37:16","modified_gmt":"2021-11-22T13:07:16","slug":"python-program-to-find-gcd-of-elements-in-a-given-range","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-gcd-of-elements-in-a-given-range\/","title":{"rendered":"Python Program to Find GCD of Elements in a Given Range"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Sort digits of a Number in Ascending Order<\/a> 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, n, and m, and the task is to solve the equation, Find the largest integer a(gcd) that is divisible by all integers n, n + 1, n + 2,…, m.<\/p>\n We only need to look at two cases here:<\/p>\n If a = b, the segment is made up of a single number, so the answer is a. 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 Below are the ways to Find the largest integer a(gcd) that is divisible by all integers n, n + 1, n + 2,…, m.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/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 In the previous article, we have discussed Python Program to Sort digits of a Number in Ascending Order Highest Common Factor (HCF) \/ Greatest Common Divisor (GCD) : 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 …<\/p>\n
\nHighest Common Factor (HCF) \/ Greatest Common Divisor (GCD) :<\/strong><\/p>\n
\nIf a = b, then gcd(n, n + 1, n?+ 2,…, m) = gcd(n, n + 1), n + 2,…, m) = gcd(1, n + 2,…, n) = 1.<\/p>\nGiven First Number =\u00a0 125\r\nGiven Second Number = 125<\/pre>\n
The greatest number that divides both 125 and 125 = 125<\/pre>\n
Given First Number = 5\r\nGiven Second Number = 8<\/pre>\n
The greatest number that divides both 5 and 8 = 1<\/pre>\n
Program to Find GCD of Elements in a Given Range in Python.<\/h2>\n
\n
Method #1: Using If, Else conditional\u00a0 Statements (Static Input)<\/h3>\n
\n
# Give the first number as static input and store it in a variable.\r\nfst_numb = 125\r\n# Give the second number as static input and store it in another variable.\r\nsecnd_numb = 125\r\n# Check if the first number is equal to the second number using the if conditional statement.\r\nif(fst_numb == secnd_numb):\r\n # If the statement is true, then print the first number.\r\n print(\"The greatest number that divides both\",\r\n fst_numb, \"and\", secnd_numb, \"=\", fst_numb)\r\nelse:\r\n # If the statement is false, then print 1.\r\n print(\"The greatest number that divides both\",\r\n fst_numb, \"and\", secnd_numb, \"=\", 1)\r\n<\/pre>\n
The greatest number that divides both 125 and 125 = 125<\/pre>\n
Method #2: Using If, Else conditional\u00a0 Statements (User Input)<\/h3>\n
\n
# Give the first number as user input using the int(input()) function and store it in a variable.\r\nfst_numb = int(input(\"Enter some random number = \"))\r\n# Give the second number as user input using the int(input()) function and store it in another variable.\r\nsecnd_numb = int(input(\"Enter some random number = \"))\r\n# Check if the first number is equal to the second number using the if conditional statement.\r\nif(fst_numb == secnd_numb):\r\n # If the statement is true, then print the first number.\r\n print(\"The greatest number that divides both\",\r\n fst_numb, \"and\", secnd_numb, \"=\", fst_numb)\r\nelse:\r\n # If the statement is false, then print 1.\r\n print(\"The greatest number that divides both\",\r\n fst_numb, \"and\", secnd_numb, \"=\", 1)\r\n<\/pre>\n
Enter some random number = 5\r\nEnter some random number = 8\r\nThe greatest number that divides both 5 and 8 = 1<\/pre>\n
\n