Largest number less than or equal to N/2 which is coprime to N
Given a number N, the task is to find the largest positive integer less than or equal to N/2 which is coprime to N.
Note: Two number A and B are considered to coprime if gcd(A, B) = 1. It is also given that 2 < N < 10^18.
Examples:
Input: N = 50
Output: 23
GCD(50, 23) = 1
Input: N = 100
Output: 49
Naive Approach: Start from N/2 and find the number smaller than or equal to N/2 which is coprime to N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
bool coPrime(ll n1, ll n2)
{
if (gcd(n1, n2) == 1)
return true ;
else
return false ;
}
ll largestCoprime(ll N)
{
ll half = floor (N / 2);
while (coPrime(N, half) == false )
half--;
return half;
}
int main()
{
ll n = 50;
cout << largestCoprime(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
else
return gcd(b, a % b);
}
static boolean coPrime( int n1, int n2)
{
if (gcd(n1, n2) == 1 )
return true ;
else
return false ;
}
static int largestCoprime( int N)
{
int half = ( int )(N / 2 );
while (coPrime(N, half) == false )
half--;
return half;
}
public static void main(String args[])
{
int n = 50 ;
System.out.println(largestCoprime(n));
}
}
|
Python3
import math as mt
def gcd( a, b):
if (b = = 0 ):
return a
else :
return gcd(b, a % b)
def coPrime( n1, n2):
if (gcd(n1, n2) = = 1 ):
return True
else :
return False
def largestCoprime( N):
half = mt.floor(N / 2 )
while (coPrime(N, half) = = False ):
half - = 1
return half
n = 50
print ( largestCoprime(n))
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
static bool coPrime( int n1, int n2)
{
if (gcd(n1, n2) == 1)
return true ;
else
return false ;
}
static int largestCoprime( int N)
{
int half = ( int )(N / 2);
while (coPrime(N, half) == false )
half--;
return half;
}
static void Main()
{
int n = 50;
Console.WriteLine(largestCoprime(n));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $b == 0)
return $a ;
else
return gcd( $b , $a % $b );
}
function coPrime( $n1 , $n2 )
{
if (gcd( $n1 , $n2 ) == 1)
return true;
else
return false;
}
function largestCoprime( $N )
{
$half = floor ( $N / 2);
while (coPrime( $N , $half ) == false)
$half --;
return $half ;
}
$n = 50;
echo largestCoprime( $n );
|
Javascript
function gcd(a, b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
function coPrime(n1, n2)
{
if (gcd(n1, n2) == 1)
return true ;
else
return false ;
}
function largestCoprime(N)
{
let half = Math.floor(N / 2);
while (coPrime(N, half) == false )
half--;
return half;
}
let n = 50;
document.write(largestCoprime(n));
|
Time Complexity : O(nlogn)
Auxiliary Space: O(logn)
Efficient Approach: To observe the pattern:
- If the given number is odd, the largest coprime number will be (N-1)/2.
- If the given number is divisible by 4, the largest coprime number will be (N)/2 – 1.
- If the given number is divisible by 2, the largest coprime number will be (N)/2 – 2.
Note: There is a special case 6, for which greatest coprime number less than N / 2 will be 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long largestCoprime( long long N)
{
if (N == 6)
return 1;
else if (N % 4 == 0)
return (N / 2) - 1;
else if (N % 2 == 0)
return (N / 2) - 2;
else
return ((N - 1) / 2);
}
int main()
{
long long int n = 50;
cout << largestCoprime(n) << endl;
return 0;
}
|
Java
class GfG
{
static int largestCoprime( int N)
{
if (N == 6 )
return 1 ;
else if (N % 4 == 0 )
return (N / 2 ) - 1 ;
else if (N % 2 == 0 )
return (N / 2 ) - 2 ;
else
return ((N - 1 ) / 2 );
}
public static void main(String []args)
{
int n = 50 ;
System.out.println(largestCoprime(n));
}
}
|
Python3
def largestCoprime(N):
if N = = 6 :
return 1
elif N % 4 = = 0 :
return N / / 2 - 1
elif N % 2 = = 0 :
return N / / 2 - 2
else :
return (N - 1 ) / / 2
if __name__ = = "__main__" :
n = 50
print (largestCoprime(n))
|
C#
using System;
class GfG
{
static int largestCoprime( int N)
{
if (N == 6)
return 1;
else if (N % 4 == 0)
return (N / 2) - 1;
else if (N % 2 == 0)
return (N / 2) - 2;
else
return ((N - 1) / 2);
}
public static void Main()
{
int n = 50;
Console.WriteLine(largestCoprime(n));
}
}
|
PHP
<?php
function largestCoprime( $N )
{
if ( $N == 6)
return 1;
else if ( $N % 4 == 0)
return ( $N / 2) - 1;
else if ( $N % 2 == 0)
return ( $N / 2) - 2;
else
return (( $N - 1) / 2);
}
$n = 50;
echo largestCoprime( $n );
?>
|
Javascript
<script>
function largestCoprime(N)
{
if (N == 6)
return 1;
else if (N % 4 == 0)
return (N / 2) - 1;
else if (N % 2 == 0)
return (N / 2) - 2;
else
return ((N - 1) / 2);
}
var n = 50;
document.write(largestCoprime(n));
</script>
|
Time Complexity : O(1), since there are only basic arithmetic operations that take constant time.
Auxiliary Space: O(1), since no extra space has been required.
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...