📌  相关文章
📜  与给定范围内的所有整数互质的数字

📅  最后修改于: 2021-10-26 06:45:21             🧑  作者: Mango

给定两个正整数LR ,任务是找到一个大于1的整数X ,使得X[L, R]范围内的所有整数互质。

例子:

方法:解决给定问题的最简单方法是找到一个大于R的素数,因为这个整数不能整除[L, R]范围内的任何整数。因此,想法是从值(R + 1)进行迭代,如果存在任何质数整数,则打印该整数并跳出循环。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check whether the
// given number N is prime or not
bool isPrime(int N)
{
    // Base Case
    if (N == 1)
        return false;
 
    for (int i = 2; i * i <= N; i++) {
 
        // If N has more than one
        // factor, then return false
        if (N % i == 0)
            return false;
    }
 
    // Otherwise, return true
    return true;
}
 
// Function to find X which is co-prime
// with the integers from the range [L, R]
int findCoPrime(int L, int R)
{
    // Store the resultant number
    int coPrime;
 
    // Check for prime integers
    // greater than R
    for (int i = R + 1;; i++) {
 
        // If the current number is
        // prime, then update coPrime
        // and break out of loop
        if (isPrime(i)) {
            coPrime = i;
            break;
        }
    }
 
    // Print the resultant number
    return coPrime;
}
 
// Driver Code
int main()
{
    int L = 16, R = 17;
    cout << findCoPrime(L, R);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to check whether the
// given number N is prime or not
static boolean isPrime(int N)
{
     
    // Base Case
    if (N == 1)
        return false;
 
    for(int i = 2; i * i <= N; i++)
    {
         
        // If N has more than one
        // factor, then return false
        if (N % i == 0)
            return false;
    }
 
    // Otherwise, return true
    return true;
}
 
// Function to find X which is co-prime
// with the integers from the range [L, R]
static int findCoPrime(int L, int R)
{
     
    // Store the resultant number
    int coPrime;
 
    // Check for prime integers
    // greater than R
    for(int i = R + 1;; i++)
    {
         
        // If the current number is
        // prime, then update coPrime
        // and break out of loop
        if (isPrime(i))
        {
            coPrime = i;
            break;
        }
    }
 
    // Print the resultant number
    return coPrime;
}
 
// Driver Code
public static void main(String[] args)
{
    int L = 16, R = 17;
     
    System.out.println(findCoPrime(L, R));
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
 
# Function to check whether the
# given number N is prime or not
def isPrime(N):
    # Base Case
    if (N == 1):
        return False
 
    for i in range(2, N + 1):
        if i*i > N:
            break
             
        # If N has more than one
        # factor, then return false
        if (N % i == 0):
            return False
 
    # Otherwise, return true
    return True
 
# Function to find X which is co-prime
# with the integers from the range [L, R]
def findCoPrime(L, R):
   
    # Store the resultant number
    coPrime, i = 0, R + 1
 
    # Check for prime integers
    # greater than R
    while True:
 
        # If the current number is
        # prime, then update coPrime
        # and break out of loop
        if (isPrime(i)):
            coPrime = i
            break
        i += 1
 
    # Print the resultant number
    return coPrime
 
# Driver Code
if __name__ == '__main__':
    L,R = 16, 17
    print (findCoPrime(L, R))
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check whether the
// given number N is prime or not
static bool isPrime(int N)
{
     
    // Base Case
    if (N == 1)
        return false;
 
    for(int i = 2; i * i <= N; i++)
    {
         
        // If N has more than one
        // factor, then return false
        if (N % i == 0)
            return false;
    }
 
    // Otherwise, return true
    return true;
}
 
// Function to find X which is co-prime
// with the integers from the range [L, R]
static int findCoPrime(int L, int R)
{
 
    // Store the resultant number
    int coPrime;
 
    // Check for prime integers
    // greater than R
    for(int i = R + 1;; i++)
    {
         
        // If the current number is
        // prime, then update coPrime
        // and break out of loop
        if (isPrime(i))
        {
            coPrime = i;
            break;
        }
    }
 
    // Print the resultant number
    return coPrime;
}
 
// Driver Code
public static void Main(string[] args)
{
    int L = 16, R = 17;
 
    Console.WriteLine(findCoPrime(L, R));
}
}
 
// This code is contributed by ukasp


Javascript


输出:
19

时间复杂度: O(L * R 1/2 )
辅助空间: O(1)