GCD of two numbers formed by n repeating x and y times
Last Updated :
11 Sep, 2023
Given three positive integer n, x, y. The task is to print Greatest Common Divisor of numbers formed by n repeating x times and number formed by n repeating y times.
0 <= n, x, y <= 1000000000.
Examples :
Input : n = 123, x = 2, y = 3.
Output : 123
Number formed are 123123 and 123123123.
Greatest Common Divisor of 123123 and
123123123 is 123.
Input : n = 4, x = 4, y = 6.
Output : 44
The idea is based on Euclidean algorithm to compute GCD of two number.
Let f(n, x) be a function which gives a number n repeated x times. So, we need to find GCD(f(n, x), f(n, y)).
Let n = 123, x = 3, y = 2.
So, first number A is f(123, 3) = 123123123 and second number B is f(123, 2) = 123123. We know, GCD(A,B) = GCD(A - B, B), using this property we can subtract any multiple of B, say B' from first A as long as B' is smaller than A.
So, A = 123123123 and B' can be 123123000. On subtracting A will became 123 and B remains same.
Therefore, A = A - B' = f(n, x - y).
So, GCD(f(n, x), f(n, y)) = GCD(f(n, x - y), f(n, y))
We can conclude following,
GCD(f(n, x), f(n, y)) = f(n, GCD(x, y)).
Below is the implementation based on this approach:
CPP
// C++ program to print Greatest Common Divisor
// of number formed by N repeating x times and
// y times.
#include<bits/stdc++.h>
using namespace std;
// Return the Greatest common Divisor of two numbers.
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
// Prints Greatest Common Divisor of number formed
// by n repeating x times and y times.
void findgcd(int n, int x, int y)
{
// Finding GCD of x and y.
int g = gcd(x,y);
// Print n, g times.
for (int i = 0; i < g; i++)
cout << n;
}
// Driven Program
int main()
{
int n = 123, x = 5, y = 2;
findgcd(n, x, y);
return 0;
}
Java
// Java program to print Greatest Common Divisor
// of number formed by N repeating x times and
// y times
class GFG {
// Return the Greatest common Divisor
// of two numbers.
static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
// Prints Greatest Common Divisor of
// number formed by n repeating x
// times and y times.
static void findgcd(int n, int x, int y) {
// Finding GCD of x and y.
int g = gcd(x, y);
// Print n, g times.
for (int i = 0; i < g; i++)
System.out.print(n);
}
// Driver code
public static void main(String[] args) {
int n = 123, x = 5, y = 2;
findgcd(n, x, y);
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program to print Greatest
# Common Divisor of number formed
# by N repeating x times and y times
# Return the Greatest common Divisor
# of two numbers.
def gcd(a, b):
if (a == 0):
return b
return gcd(b % a, a)
# Prints Greatest Common Divisor of
# number formed by n repeating x times
# and y times.
def findgcd(n, x, y):
# Finding GCD of x and y.
g = gcd(x, y)
# Print n, g times.
for i in range(g):
print(n)
# Driver code
n = 123
x = 5
y = 2
findgcd(n, x, y)
# This code is contributed by Anant Agarwal.
C#
// C# program to print Greatest Common
// Divisor of number formed by N
// repeating x times and y times
using System;
class GFG {
// Return the Greatest common
// Divisor of two numbers.
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Prints Greatest Common
// Divisor of number formed
// by n repeating x times
// and y times.
static void findgcd(int n,
int x, int y)
{
// Finding GCD of x and y.
int g = gcd(x, y);
// Print n, g times.
for (int i = 0; i < g; i++)
Console.Write(n);
}
// Driver code
public static void Main() {
int n = 123, x = 5, y = 2;
findgcd(n, x, y);
}
}
// This code is contributed by
// nitin mittal.
PHP
<?php
// PHP program to print
// Greatest Common Divisor
// of number formed by N
// repeating x times and y times.
// Return the Greatest common
// Divisor of two numbers.
function gcd($a, $b)
{
if ($a == 0)
return $b;
return gcd($b % $a, $a);
}
// Prints Greatest Common Divisor
// of number formed by n repeating
// x times and y times.
function findgcd($n, $x, $y)
{
// Finding GCD of x and y.
$g = gcd($x, $y);
// Print n, g times.
for ($i = 0; $i < $g; $i++)
echo($n);
}
// Driver Code
$n = 123; $x = 5; $y = 2;
findgcd($n, $x, $y);
// This code is contributed by Ajit.
?>
JavaScript
<script>
// Javascript program to print Greatest Common Divisor
// of number formed by N repeating x times and
// y times.
// Return the Greatest common Divisor of two numbers.
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
// Prints Greatest Common Divisor of number formed
// by n repeating x times and y times.
function findgcd(n, x, y)
{
// Finding GCD of x and y.
let g = gcd(x,y);
// Print n, g times.
for (let i = 0; i < g; i++)
document.write(n);
}
// Driven Program
let n = 123, x = 5, y = 2;
findgcd(n, x, y);
// This is code is contributed by Mayank Tyagi
</script>
Output :
123
Time Complexity: O(log(min(n)) )
Auxiliary Space: O(log(min(n))
Similar Reads
GCD (Greatest Common Divisor) Practice Problems for Competitive Programming GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest positive integer that divides both of the numbers.GCD of Two NumbersFastest Way to Compute GCDThe fastest way to find the Greatest Common Divisor (GCD) of two numbers is by using the Euclidean algorithm. The E
4 min read
Program to Find GCD or HCF of Two Numbers Given two positive integers a and b, the task is to find the GCD of the two numbers.Note: The GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. Examples:Input: a = 20, b = 28Output: 4Explanation: The factors of 20 are 1, 2, 4
12 min read
Check if two numbers are co-prime or not Two numbers A and B are said to be Co-Prime or mutually prime if the Greatest Common Divisor of them is 1. You have been given two numbers A and B, find if they are Co-prime or not.Examples : Input : 2 3Output : Co-PrimeInput : 4 8Output : Not Co-PrimeThe idea is simple, we find GCD of two numbers a
5 min read
GCD of more than two (or array) numbers Given an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr
11 min read
Program to find LCM of two numbers Given two positive integers a and b. Find the Least Common Multiple (LCM) of a and b.LCM of two numbers is the smallest number which can be divided by both numbers. Input : a = 10, b = 5Output : 10Explanation : 10 is the smallest number divisible by both 10 and 5Input : a = 5, b = 11Output : 55Expla
5 min read
LCM of given array elements In this article, we will learn how to find the LCM of given array elements.Given an array of n numbers, find the LCM of it. Example:Input : {1, 2, 8, 3}Output : 24LCM of 1, 2, 8 and 3 is 24Input : {2, 7, 3, 9, 4}Output : 252Table of Content[Naive Approach] Iterative LCM Calculation - O(n * log(min(a
14 min read
Find the other number when LCM and HCF given Given a number A and L.C.M and H.C.F. The task is to determine the other number B. Examples: Input: A = 10, Lcm = 10, Hcf = 50. Output: B = 50 Input: A = 5, Lcm = 25, Hcf = 4. Output: B = 20 Formula: A * B = LCM * HCF B = (LCM * HCF)/AExample : A = 15, B = 12 HCF = 3, LCM = 60 We can see that 3 * 60
4 min read
Minimum insertions to make a Co-prime array Given an array of N elements, find the minimum number of insertions to convert the given array into a co-prime array. Print the resultant array also.Co-prime Array : An array in which every pair of adjacent elements are co-primes. i.e, gcd(a, b) = 1 . Examples : Input : A[] = {2, 7, 28}Output : 1Exp
6 min read
Find the minimum possible health of the winning player Given an array health[] where health[i] is the health of the ith player in a game, any player can attack any other player in the game. The health of the player being attacked will be reduced by the amount of health the attacking player has. The task is to find the minimum possible health of the winn
4 min read
Minimum squares to evenly cut a rectangle Given a rectangular sheet of length l and width w. we need to divide this sheet into square sheets such that the number of square sheets should be as minimum as possible.Examples: Input :l= 4 w=6 Output :6 We can form squares with side of 1 unit, But the number of squares will be 24, this is not min
4 min read