Co-prime pair with given sum minimum difference
Co-prime or mutually prime pairs are those pairs of numbers whose GCD is 1. Given a number n represent the number as the sum of a Co-prime pair (A, B) such that A – B is minimum.
Examples :
Input : 12
Output : 5 7
Possible co-prime pairs are (5, 7), (1, 11)
but difference between 5 and 7 is minimum
Input : 13
Output : 6 7
Possible co-prime pairs are (6, 7), (5, 8),
(4, 9), (3, 10), (2, 11) and (1, 12)
but difference between 6 and 7 is minimum
A simple solution is to iterate through all numbers from 1 to n-1. For every number x, check if n – x and x are co-primes. If yes, then update the result if the difference between these two is less than the minimum difference so far.
An efficient solution is based on the fact that the numbers with a minimum difference should be close to n/2. We loop from n/2 to 1. Check every possible pair and when the first possible Co-prime pair is found display it and stop the loop.
C++
#include <bits/stdc++.h>
using namespace std;
bool coprime( int a, int b)
{
return (__gcd(a, b) == 1);
}
void pairSum( int n){
int mid = n / 2;
for ( int i = mid; i >= 1; i--) {
if (coprime(i, n - i) == 1) {
cout << i << " " << n - i;
break ;
}
}
}
int main()
{
int n = 11;
pairSum(n);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static int __gcd( int a, int b)
{
return b == 0 ? a :
__gcd(b, a % b);
}
static boolean coprime( int a, int b)
{
return (__gcd(a, b) == 1 );
}
static void pairSum( int n)
{
int mid = n / 2 ;
for ( int i = mid; i >= 1 ; i--)
{
if (coprime(i, n - i) == true )
{
System.out.print( i + " " +
(n - i));
break ;
}
}
}
public static void main(String args[])
{
int n = 11 ;
pairSum(n);
}
}
|
Python3
import math
def coprime(a, b):
return 1 if (math.gcd(a, b) = = 1 ) else 0 ;
def pairSum(n):
mid = int (n / 2 );
i = mid;
while (i > = 1 ):
if (coprime(i, n - i) = = 1 ):
print (i, n - i);
break ;
i = i - 1 ;
n = 11 ;
pairSum(n);
|
C#
using System;
class GFG {
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
static bool coprime( int a, int b)
{
return (__gcd(a, b) == 1);
}
static void pairSum( int n)
{
int mid = n / 2;
for ( int i = mid; i >= 1; i--)
{
if (coprime(i, n - i) == true )
{
Console.Write( i + " "
+ (n - i));
break ;
}
}
}
public static void Main()
{
int n = 11;
pairSum(n);
}
}
|
PHP
<?php
function gcd( $num1 , $num2 )
{
while ( $num2 != 0)
{
$t = $num1 % $num2 ;
$num1 = $num2 ;
$num2 = $t ;
}
return $num1 ;
}
function coprime( $a , $b )
{
if (gcd( $a , $b ) == 1)
return 1;
else
return 0;
}
function pairSum( $n )
{
$mid = (int)(( $n / 2));
for ( $i = $mid ; $i >= 1; $i --)
{
if (coprime( $i , $n - $i ) == 1)
{
echo $i . " " . ( $n - $i );
break ;
}
}
}
$n = 11;
pairSum( $n );
?>
|
Javascript
<script>
function __gcd(a, b)
{
return b == 0 ? a :
__gcd(b, a % b);
}
function coprime(a, b)
{
return (__gcd(a, b) == 1);
}
function pairSum(n)
{
let mid = Math.floor(n / 2);
for (let i = mid; i >= 1; i--)
{
if (coprime(i, n - i) == true )
{
document.write( i + " " +
(n - i));
break ;
}
}
}
let n = 11;
pairSum(n);
</script>
|
Time Complexity: O(n*log(n))
Auxiliary Space: O(1)
Last Updated :
17 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...