📌  相关文章
📜  最小整数,使得除以[2,N]范围内的任何元素时,剩下的余数为1

📅  最后修改于: 2021-04-29 08:28:42             🧑  作者: Mango

给定一个整数N ,任务是找到最小可能的整数X ,使得对于[2,N]范围内的所有MX%M = 1
例子:

方法:[2,N]范围中找到所有整数的lcm并将其存储在变量lcm中。现在我们知道lcm是可被范围[2,N]中的所有元素整除的最小数字,并使其在每个除法数上都剩下1的余数,只需将其加1 ,即lcm + 1是必需的答案。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the smallest number
// which on dividing with any element from
// the range [2, N] leaves a remainder of 1
long getMinNum(int N)
{
    // Find the LCM of the elements
    // from the range [2, N]
    int lcm = 1;
    for (int i = 2; i <= N; i++)
        lcm = ((i * lcm) / (__gcd(i, lcm)));
 
    // Return the required number
    return (lcm + 1);
}
 
// Driver code
int main()
{
    int N = 5;
 
    cout << getMinNum(N);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function to return the smallest number
    // which on dividing with any element from
    // the range [2, N] leaves a remainder of 1
    static long getMinNum(int N)
    {
        // Find the LCM of the elements
        // from the range [2, N]
        int lcm = 1;
        for (int i = 2; i <= N; i++)
            lcm = ((i * lcm) / (__gcd(i, lcm)));
 
        // Return the required number
        return (lcm + 1);
    }
 
    static int __gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return __gcd(b, a % b);
    }
 
    // Driver code
    public static void main(String args[])
    {
        int N = 5;
        System.out.println(getMinNum(N));
    }
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
from math import gcd
 
# Function to return the smallest number
# which on dividing with any element from
# the range [2, N] leaves a remainder of 1
def getMinNum(N) :
 
    # Find the LCM of the elements
    # from the range [2, N]
    lcm = 1;
    for i in range(2, N + 1) :
        lcm = ((i * lcm) // (gcd(i, lcm)));
 
    # Return the required number
    return (lcm + 1);
 
 
# Driver code
if __name__ == "__main__" :
 
    N = 5;
 
    print(getMinNum(N));
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
 
class GFG {
 
    // Function to return the smallest number
    // which on dividing with any element from
    // the range [2, N] leaves a remainder of 1
    static long getMinNum(int N)
    {
        // Find the LCM of the elements
        // from the range [2, N]
        int lcm = 1;
        for (int i = 2; i <= N; i++)
            lcm = ((i * lcm) / (__gcd(i, lcm)));
 
        // Return the required number
        return (lcm + 1);
    }
 
    static int __gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return __gcd(b, a % b);
    }
 
    // Driver code
    public static void Main()
    {
        int N = 5;
        Console.WriteLine(getMinNum(N));
    }
}
 
// This code has been contributed by anuj_67..


PHP


Javascript


输出:
61

时间复杂度: O(N * log(N))

辅助空间: O(1)