📜  [2,3,.. n]中最大的数字与[2,3,.. m]中的数字互质

📅  最后修改于: 2021-04-27 21:30:43             🧑  作者: Mango

给定两个整数n和m,任务是找到一个满足以下条件的数p:
->数字p应当小于或等于n。
->该数字应与2到p(含)之间的所有整数互质,即将两个数字相除的唯一正整数为1。
例子 :

Input :  n = 16, m = 3
Output : 13
Explanation : We need to find largest number
smaller than n and co-prime with all numbers
in set [2, 3, ... m] which is [2, 3] here. 
Note that numbers {2, 4, 6, 8, ..} are not
co-prime with 2 and numbers {3, 6, 9, .. }
are not co-prime with 3.

Input : n = 6, m = 5
Output : -1 (Number doesn't exists)
Explanation : In this example 2 will cancel
out 2, 4, 6 and 3 will cancel out 3, 6
and 5 will cancel out 5. No number is left, 
so the answer does not exists.

方法1:创建一个从2到n的数字列表。然后运行i = 2到m的循环,并标记所有为i的倍数的数字。如果我已被标记,请不要运行循环,因为其倍数已被标记。当循环终止时,请运行从n到2的循环,直到找到未标记的数字。如果没有未标记的号码而不存在数字,则第一个未标记的号码为答案。该方法占用O(n)个辅助空间,因此,如果n的值太大,则此方法将不起作用。
方法2:从n到p + 1循环,并检查每个数字是否不能被2到m之间的任何数字整除。

C++
#include 
using namespace std;
 
// Returns true if i is co-prime with numbers
// in set [2, 3, ... m]
bool isValid(long long int i, long long int m)
{
    // Running the loop till square root of n
    // to reduce the time complexity from n
    long long int sq_i = sqrt(i);
 
    // Find the minimum of square root of n
    // and m to run the loop until the smaller
    // one
    long long int sq = min(m, sq_i);
 
    // Check from 2 to min(m, sqrt(n))
    for (long long int j = 2; j <= sq; j++)
        if (i % j == 0)
            return false;
     
    return true;
}
 
// Function to find the largest number less than n
// which is Co-prime with all numbers from 2 to m
void findLargestNum(long long int n, long long int m)
{
    // Iterating from n to m+1 to find the number
    for (long long int i = n; i > m; i--) {
 
        // checking every number for the given
        // conditions
        if (isValid(i, m)) {
         
            // The first number which satisfy the
            // conditions is the answer
            cout << i << '\n';
            return;
        }
    }
 
    // If there is no number which satisfy the
    // conditions, then print number does not exist.
    cout << "Number Doesn't Exists\n";
}
 
// Driver Program
int main()
{
    long long int n = 16, m = 3;
    findLargestNum(n, m);
    return 0;
}


Java
// Java Largest number in [2, 3, .. n]
// which is co-prime with numbers
// in [2, 3, .. m]
import java.io.*;
 
class GFG
{
    // Returns true if i is co-prime with numbers
    // in set [2, 3, ... m]
    static boolean isValid(long i, long m)
    {
        // Running the loop till square root of n
        // to reduce the time complexity from n
        long sq_i = (long)Math.sqrt(i);
     
        // Find the minimum of square root of n
        // and m to run the loop until the smaller
        // one
        long sq = Math.min(m, sq_i);
     
        // Check from 2 to min(m, sqrt(n))
        for (long j = 2; j <= sq; j++)
            if (i % j == 0)
                return false;
         
        return true;
    }
     
    // Function to find the largest number less than n
    // which is Co-prime with all numbers from 2 to m
    static void findLargestNum(long n, long m)
    {
        // Iterating from n to m+1 to find the number
        for (long i = n; i > m; i--) {
     
            // checking every number for the given
            // conditions
            if (isValid(i, m)) {
             
                // The first number which satisfy the
                // conditions is the answer
                System.out.println (i);
                return;
            }
        }
     
        // If there is no number which satisfy the
        // conditions, then print number does not exist.
        System.out.println("Number Doesn't Exists");
    }
     
    // Driver Program
    public static void main (String[] args)
    {
        long n = 16, m = 3;
        findLargestNum(n, m);
             
    }
}
         
// This code is contributed by vt_m.


Python3
# Python3 code to find
# Largest number in
# [2, 3, .. n] which is
# co-prime with
# numbers in [2, 3, .. m]
import math
 
# Returns true if i is
# co-prime with numbers
# in set [2, 3, ... m]
def isValid(i,m) :
     
    # Running the loop
    # till square root of n
    # to reduce the time
    # complexity from n
    sq_i = math.sqrt(i)
 
    # Find the minimum of
    # square root of n
    # and m to run the loop
    # until the smaller
    # one
    sq = min(m, sq_i)
 
    # Check from 2 to
    # min(m, sqrt(n))
    for j in range(2, sq + 1) :
        if (i % j == 0) :
            return False
     
    return True
 
# def to find the
# largest number less
# than n which is Co-prime 
# with all numbers from
# 2 to m
def findLargestNum(n, m) :
     
    # Iterating from n to m+1
    # to find the number
    for i in range(n, m, -1) :
 
        # checking every number for
        # the given conditions
        if (isValid(i, m)) :
         
            # The first number
            # which satisfy the
            # conditions is the
            # answer
            print ("{}\n".format(i));
            return
 
    # If there is no number
    # which satisfy the
    # conditions, then print
    # number does not exist.
    print ("Number Doesn't Exists\n")
 
# Driver Code
n = 16
m = 3
findLargestNum(n, m)
     
# This code is contributed by
# Manish Shaw(manishshaw1)


C#
// C# Largest number in [2, 3, .. n]
// which is co-prime with numbers
// in [2, 3, .. m]
using System;
 
class GFG
{
    // Returns true if i is co-prime
    // with numbers in set [2, 3, ... m]
    static bool isValid(long i, long m)
    {
        // Running the loop till square root
        // of n to reduce the time complexity
        // from n
        long sq_i = (long)Math.Sqrt(i);
     
        // Find the minimum of square root
        // of n and m to run the loop until
        // the smaller one
        long sq = Math.Min(m, sq_i);
     
        // Check from 2 to min(m, sqrt(n))
        for (long j = 2; j <= sq; j++)
            if (i % j == 0)
                return false;
         
        return true;
    }
     
    // Function to find the largest number
    // less than n which is Co-prime with
    // all numbers from 2 to m
    static void findLargestNum(long n, long m)
    {
        // Iterating from n to m+1 to find the
        // number
        for (long i = n; i > m; i--) {
     
            // checking every number for the given
            // conditions
            if (isValid(i, m)) {
             
                // The first number which satisfy the
                // conditions is the answer
                Console.WriteLine(i);
                return;
            }
        }
     
        // If there is no number which satisfy
        // the conditions, then print number does
        // not exist.
        Console.WriteLine("Number Doesn't Exists");
    }
     
    // Driver Program
    public static void Main ()
    {
        long n = 55, m = 25;
        findLargestNum(n, m);
             
    }
}
         
// This code is contributed by vt_m.


PHP
 $m; $i--)
    {
 
        // checking every number for
        // the given conditions
        if (isValid($i, $m))
        {
         
            // The first number
            // which satisfy the
            // conditions is the
            // answer
            echo $i , "\n";
            return;
        }
    }
 
    // If there is no number
    // which satisfy the
    // conditions, then print
    // number does not exist.
    echo "Number Doesn't Exists\n";
}
 
    // Driver Code
    $n = 16; $m = 3;
    findLargestNum($n, $m);
     
// This code is contributed by anuj_67
?>


Javascript


输出 :

13