Largest number less than or equal to N / 2 which is coprime to N | Java implementation of the above approacdh ; Function to calculate gcd of two number ; Function to check if two numbers are coprime or not ; two numbers are coprime if their gcd is 1 ; Function to find largest integer less than or equal to N / 2 and coprime with N ; Check one by one all numbers less than or equal to N / 2 ; Driver code
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 ) ) ; } }