📜  小于或等于N / 2的最大数,它是N的互质数

📅  最后修改于: 2021-05-31 23:44:17             🧑  作者: Mango

给定数字N,任务是找到小于或等于N / 2且与N互质的最大正整数。
注意:如果gcd(A,B)= 1,则两个数字A和B被认为互质。
例子:

Input: N = 50
Output: 23
GCD(50, 23) = 1 

Input: N = 100
Output: 49

天真的方法:从N / 2开始并找到小于或等于N / 2的数字,它是N的互质数。
下面是上述方法的实现:

C++
// C++ implementation of the above approacdh
#include 
#define ll long long int
using namespace std;
 
// Function to calculate gcd of two number
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
 
// Function to check if two numbers are coprime or not
bool coPrime(ll n1, ll n2)
{
    // two numbers are coprime if their gcd is 1
    if (gcd(n1, n2) == 1)
        return true;
    else
        return false;
}
 
// Function to find largest integer less
// than or equal to N/2 and coprime with N
ll largestCoprime(ll N)
{
    ll half = floor(N / 2);
 
    // Check one by one all numbers
    // less than or equal to N/2
    while (coPrime(N, half) == false)
        half--;
 
    return half;
}
 
// Driver code
int main()
{
 
    ll n = 50;
    cout << largestCoprime(n);
 
    return 0;
}


Java
// Java implementation of the above approacdh
import java.util.*;
 
class GFG
{
 
// Function to calculate gcd of two number
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
 
// Function to check if two
// numbers are coprime or not
static boolean coPrime(int n1, int n2)
{
    // two numbers are coprime
    // if their gcd is 1
    if (gcd(n1, n2) == 1)
        return true;
    else
        return false;
}
 
// Function to find largest integer less
// than or equal to N/2 and coprime with N
static int largestCoprime(int N)
{
    int half = (int)(N / 2);
 
    // Check one by one all numbers
    // less than or equal to N/2
    while (coPrime(N, half) == false)
        half--;
 
    return half;
}
 
// Driver code
public static void main(String args[])
{
    int n = 50;
    System.out.println(largestCoprime(n));
}
}
 
// This code is contributed by
// Surendra_Gangwar


Python3
# Python3 implementation of the above approacdh
import math as mt
 
# Function to calculate gcd of two number
def gcd( a, b):
 
    if (b == 0):
        return a
    else:
        return gcd(b, a % b)
 
 
# Function to check if two numbers are coprime or not
def coPrime( n1, n2):
 
    # two numbers are coprime if their gcd is 1
    if (gcd(n1, n2) == 1):
        return True
    else:
        return False
 
 
# Function to find largest integer less
# than or equal to N/2 and coprime with N
def largestCoprime( N):
 
    half = mt.floor(N / 2)
 
    # Check one by one a numbers
    # less than or equal to N/2
    while (coPrime(N, half) == False):
        half -= 1
 
    return half
 
 
# Driver code
 
n = 50
print( largestCoprime(n))
 
#This code is contributed by Mohit kumar 29


C#
// C# implementation of the above approacdh
using System;
 
class GFG
{
 
// Function to calculate gcd of two number
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
 
// Function to check if two
// numbers are coprime or not
static bool coPrime(int n1, int n2)
{
    // two numbers are coprime
    // if their gcd is 1
    if (gcd(n1, n2) == 1)
        return true;
    else
        return false;
}
 
// Function to find largest integer less
// than or equal to N/2 and coprime with N
static int largestCoprime(int N)
{
    int half = (int)(N / 2);
 
    // Check one by one all numbers
    // less than or equal to N/2
    while (coPrime(N, half) == false)
        half--;
 
    return half;
}
 
// Driver code
static void Main()
{
    int n = 50;
    Console.WriteLine(largestCoprime(n));
}
}
 
// This code is contributed by chandan_jnu


PHP


C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to find largest integer less than
// or equal to N/2 and is coprime with N
long long largestCoprime(long long N)
{
    // Handle the case for N = 6
    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);
}
 
// Driver code
int main()
{
 
    long long int n = 50;
    cout << largestCoprime(n) << endl;
 
    return 0;
}


Java
// Java implementation of the above approach
class GfG
{
 
    // Function to find largest integer less than
    // or equal to N/2 and is coprime with N
    static int largestCoprime(int N)
    {
         
        // Handle the case for N = 6
        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);
    }
 
    // Driver code
    public static void main(String []args)
    {
        int n = 50;
        System.out.println(largestCoprime(n));
    }
}
     
// This code is contributed by Rituraj Jain


Python3
# Python3 implementation of the above approach
 
# Function to find largest integer less than
# or equal to N/2 and is coprime with N
def largestCoprime(N):
 
    # Handle the case for N = 6
    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
 
# Driver code
if __name__ == "__main__":
   
    n = 50
    print(largestCoprime(n))
   
# This code is contributed by Rituraj Jain


C#
// C# implementation of the above approach
using System;
 
class GfG
{
 
    // Function to find largest
    // integer less than or equal
    // to N/2 and is coprime with N
    static int largestCoprime(int N)
    {
         
        // Handle the case for N = 6
        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);
    }
 
    // Driver code
    public static void Main()
    {
        int n = 50;
        Console.WriteLine(largestCoprime(n));
    }
}
     
// This code is contributed by Ryuga


PHP


Javascript


输出:
23

高效方法:观察模式:

  • 如果给定数字为奇数,则最大互质数将为(N-1)/ 2
  • 如果给定数可被4整除,则最大互质数将为(N)/ 2 – 1
  • 如果给定数可被2整除,则最大互质数将为(N)/ 2 – 2

注意:在特殊情况6下,小于N / 2的最大互质数为1。
下面是上述方法的实现:

C++

// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to find largest integer less than
// or equal to N/2 and is coprime with N
long long largestCoprime(long long N)
{
    // Handle the case for N = 6
    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);
}
 
// Driver code
int main()
{
 
    long long int n = 50;
    cout << largestCoprime(n) << endl;
 
    return 0;
}

Java

// Java implementation of the above approach
class GfG
{
 
    // Function to find largest integer less than
    // or equal to N/2 and is coprime with N
    static int largestCoprime(int N)
    {
         
        // Handle the case for N = 6
        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);
    }
 
    // Driver code
    public static void main(String []args)
    {
        int n = 50;
        System.out.println(largestCoprime(n));
    }
}
     
// This code is contributed by Rituraj Jain

Python3

# Python3 implementation of the above approach
 
# Function to find largest integer less than
# or equal to N/2 and is coprime with N
def largestCoprime(N):
 
    # Handle the case for N = 6
    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
 
# Driver code
if __name__ == "__main__":
   
    n = 50
    print(largestCoprime(n))
   
# This code is contributed by Rituraj Jain

C#

// C# implementation of the above approach
using System;
 
class GfG
{
 
    // Function to find largest
    // integer less than or equal
    // to N/2 and is coprime with N
    static int largestCoprime(int N)
    {
         
        // Handle the case for N = 6
        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);
    }
 
    // Driver code
    public static void Main()
    {
        int n = 50;
        Console.WriteLine(largestCoprime(n));
    }
}
     
// This code is contributed by Ryuga

的PHP


Java脚本


输出:
23