Given two integer x and y, the task is to find the HCF of the numbers without using recursion or Euclidean method.
Input: x = 16, y = 32
Input: x = 12, y = 15
Approach: HCF of two numbers is the greatest number which can divide both the numbers. If the smaller of the two numbers can divide the larger number then the HCF is the smaller number. Else starting from (smaller / 2) to 1 check whether the current element divides both the numbers . If yes, then it is the required HCF.
Below is the implementation of the above approach:
= 2; $i–)
// If both the numbers
// are divisible by i
if ($x % $i == 0 &&
$y % $i == 0)
// 1 divides every number
// Driver code
$x = 16; $y = 32;
// This code is contributed
// by Code_Mech.
- Find the value of ln(N!) using Recursion
- New Algorithm to Generate Prime Numbers from 1 to Nth Number
- Algorithm to generate positive rational numbers
- Pairs with same Manhattan and Euclidean distance
- Euclidean algorithms (Basic and Extended)
- C Program for Basic Euclidean algorithms
- Java Program for Basic Euclidean algorithms
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Find two numbers whose sum and GCD are given
- Given two numbers a and b find all x such that a % x = b
- Find k numbers which are powers of 2 and have sum N | Set 1
- Find XOR of numbers from the range [L, R]
- Program to find LCM of 2 numbers without using GCD
- Find two prime numbers with given sum
- Find two numbers with sum and product both same as N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.