Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Program to find GCD or HCF of two numbers python

# function to return gcd of a and b
 
# Taking the matrix as globally
dp = [[-1 for i in range(1001)] for j in range(1001)]
 
def gcd(a,b):
     
    # Everything divides 0
    if (a == 0):
        return b
    if (b == 0):
        return a
 
    # base case
    if (a == b):
        return a
     
    if(dp[a][b] != -1):
        return dp[a][b]
         
    # a is greater
    if (a > b):
        dp[a][b] = gcd(a-b, b)
    else:
        dp[a][b] = gcd(a, b-a)
         
    return dp[a][b]
 
# Driver program to test above function
a = 98
b = 56
if(gcd(a, b)):
    print('GCD of', a, 'and', b, 'is', gcd(a, b))
else:
    print('not found')
 
# This code is contributed by Samim Hossain Mondal.
Comment

Program to find GCD or HCF of two numbers python

// Java program to find GCD of two numbers
import java.util.*;
public class GFG
{
    static int [][]dp = new int[1001][1001];
   
    // Recursive function to return gcd of a and b
    static int gcd(int a, int b)
    {
       
        // Everything divides 0
        if (a == 0)
          return b;
        if (b == 0)
          return a;
      
        // base case
        if (a == b)
            return a;
      
        // if a value is already
    // present in dp
    if(dp[a][b] != -1)
        return dp[a][b];
 
    // a is greater
    if (a > b)
        dp[a][b] = gcd(a-b, b);
     
    // b is greater
    else
        dp[a][b] = gcd(a, b-a);
     
    // return dp
    return dp[a][b];
    }
     
    // Driver method
    public static void main(String[] args)
    {
        for(int i = 0; i < 1001; i++) {
            for(int j = 0; j < 1001; j++) {
                dp[i][j] = -1;
            }
        }
        int a = 98, b = 56;
        System.out.println("GCD of " + a +" and " + b + " is " + gcd(a, b));
    }
}
 
// This code is contributed by Samim Hossain Mondal.
Comment

how to find gcd of two numbers in python

import math
# Using the built-in gcd method
gcd_built_in = math.gcd(10, 5)
print("GCD:", gcd_built_in)  # GCD: 5
# Creating a new gcd method
# implementing Euclid's algorithm

def gcd_euclid(num1, num2):
    if(num1 == 0):
        return num2
    if(num2 == 0):
        return num1
    while(num1 != num2):
        if(num1 > num2):
            num1 = num1-num2
        else:
            num2 = num2 - num1
    return num1

gcd_euclid = gcd_euclid(3, 7)
print("GCD:", gcd_euclid)  # GCD: 1
Comment

how to find gcd of n numbers in python

#My Python Lectures: https://cutt.ly/python-full-playlist
#Codechef Problem Solution Video: https://cutt.ly/codeChefContestProblemSolution
import math
num = [2,3,4,5,6,7]
res = num[0]
for i in range(1, len(num), 1):
    res = math.gcd(res, num[i])
print(res)
Comment

PREVIOUS NEXT
Code Example
Python :: convert birth date column to age pandas 
Python :: runtime errors in python 
Python :: how to remove trailing zeros in python 
Python :: re python 
Python :: python default dictionary 
Python :: python elif 
Python :: average of a list in function with python 
Python :: python squared 
Python :: mad libs generator python tutorial 
Python :: find last element in list python 
Python :: List Nested Lists 
Python :: how to create multiple dictionaries in python 
Python :: add to list python 
Python :: python check if variable has value 
Python :: get category discord.py 
Python :: python discord bot embed 
Python :: py string find regex pos 
Python :: parse receipt python 
Python :: pandas dataframe apply function with multiple arguments 
Python :: python suppress print output from function 
Python :: skeppy python 
Python :: number pattern program in python using for loop 
Python :: Display vowels in a string using for loop 
Python :: inverse matrix gauss python 
Python :: print(shahzaib) 
Python :: light fm cold start problem 
Python :: python prime number 
Python :: pip upgrade command 
Shell :: ubuntu uninstall chrome 
Shell :: grep ip address 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =