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

Python Program to Find HCF or GCD

# Python program to find H.C.F of two numbers

# define a function
def compute_hcf(x, y):

# choose the smaller number
    if x > y:
        smaller = y
    else:
        smaller = x
    for i in range(1, smaller+1):
        if((x % i == 0) and (y % i == 0)):
            hcf = i 
    return hcf

num1 = 54 
num2 = 24

print("The H.C.F. is", compute_hcf(num1, num2))
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

PREVIOUS NEXT
Code Example
Python :: python declare variables from dictionary 
Python :: install python in dockerfile 
Python :: get definition of word python 
Python :: what is a slug 
Python :: traversing a tree in python 
Python :: convert tensor to numpy array 
Python :: python how to skip iteration 
Python :: increment python 
Python :: how to get a int from string python 
Python :: make sns heatmap colorbar larger 
Python :: mse python 
Python :: mechanize python 
Python :: python uuid 
Python :: import discord python 
Python :: save model history keras 
Python :: python get line number x in file 
Python :: read data from excel and plot in python 
Python :: how to add to the end of an array python 
Python :: pyqt5 qcombobox get selected item 
Python :: hash table in python 
Python :: how to make a clock in python 3 
Python :: select columns pandas 
Python :: how to get scrapy output file in json 
Python :: pandas rename 
Python :: python substring from end 
Python :: how to add char to string python 
Python :: modulo python 
Python :: openai gym random action 
Python :: python file write 
Python :: list get every 2nd element 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =