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

PREVIOUS NEXT
Code Example
Python :: Python Import all names 
Python :: use rectangular signal in python 
Python :: pandas group by decending 
Python :: pandas.describe per group 
Python :: split column and rename them 
Python :: python 3d list 
Python :: python remove vowels from string 
Python :: how to get ping from computer IN PYTHON 
Python :: find rules of decision tree python 
Python :: #Check if list1 contains all elements of list2 using all() 
Python :: CMake Error at pybind11/tools/FindPythonLibsNew.cmake:131 (message): Python config failure: 
Python :: pythonhashseed 
Python :: python Python Program to Catch Multiple Exceptions in One Line 
Python :: django search pagination 
Python :: print index and value on each iteration of the for loop in Python 
Python :: Python .on event triggers 
Python :: pandas assign value to row based on condition 
Python :: pandas and operator 
Python :: NumPy fliplr Syntax 
Python :: pyqt5 spin box change value trigger 
Python :: how to send image to template thats not in static flask 
Python :: get every second elemnt of array matlab 
Python :: how to python string up 
Python :: BST_Deleting 
Python :: How to plot Feature importance of any model in python 
Python :: deepcopy python 
Python :: pyqt5 app styles 
Python :: explode multiple columns pandas 
Python :: df shape 
Python :: list vs dictionary python 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =