📌  相关文章
📜  通过减去其最高除数将 N 减少为素数的最小操作

📅  最后修改于: 2022-05-13 01:56:10.775000             🧑  作者: Mango

通过减去其最高除数将 N 减少为素数的最小操作

给定一个正整数N 。在一个操作中,用除N1之外的最高除数减去N。任务是找到将N精确地减少到素数所需的最小操作。

例子:

方法:这个问题可以通过使用简单的数学概念来解决。请按照以下步骤解决给定的问题。

  • 首先,检查N是否已经是素数。
    • 如果N已经是素数,则返回0
    • 否则,初始化一个变量说count = 0以存储所需的操作数。
    • 初始化一个变量说 i=2 并运行一个 while 循环直到 N!=i
      • 运行 while 循环并在每次迭代中减去 N 的当前值及其最大除数。
      • 计算步骤并将计数增加1
    • 返回计数

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function check whether a number
// is prime or not
bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;
 
    // Check from 2 to square root of n
    for (int i = 2; i <= sqrt(n); i++)
        if (n % i == 0)
            return false;
 
    return true;
}
 
// Function to minimum operations required
// to reduce the number to a prime number
int minOperation(int N)
{
    // Because 1 cannot be converted to prime
    if (N == 1)
        return -1;
 
    // If given number is already prime
    // return 0
    if (isPrime(N) == true) {
        return 0;
    }
 
    // If number is not prime
    else {
        // Variable for total count
        int count = 0;
        int i = 2;
 
        // If number is not equal to i
        while (N != i) {
            // If N is completely divisible by i
            while (N % i == 0) {
                // Temporary variable to store
                // current number
                int temp = N;
 
                // Update the number by decrementing
                // with highest divisor
                N -= (temp / i);
 
                // Increment count by 1
                count++;
                if (isPrime(N))
                    return count;
            }
            i++;
        }
 
        // Return the count
        return count;
    }
}
 
// Driver Code
int main()
{
    int N = 38;
 
    cout << minOperation(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.Arrays;
 
class GFG {
    // Function check whether a number
    // is prime or not
    public static boolean isPrime(int n) {
        // Corner case
        if (n <= 1)
            return false;
 
        // Check from 2 to square root of n
        for (int i = 2; i <= Math.sqrt(n); i++)
            if (n % i == 0)
                return false;
 
        return true;
    }
 
    // Function to minimum operations required
    // to reduce the number to a prime number
    public static int minOperation(int N) {
        // Because 1 cannot be converted to prime
        if (N == 1)
            return -1;
 
        // If given number is already prime
        // return 0
        if (isPrime(N) == true) {
            return 0;
        }
 
        // If number is not prime
        else {
            // Variable for total count
            int count = 0;
            int i = 2;
 
            // If number is not equal to i
            while (N != i) {
                // If N is completely divisible by i
                while (N % i == 0) {
                    // Temporary variable to store
                    // current number
                    int temp = N;
 
                    // Update the number by decrementing
                    // with highest divisor
                    N -= (temp / i);
 
                    // Increment count by 1
                    count++;
                    if (isPrime(N))
                        return count;
                }
                i++;
            }
 
            // Return the count
            return count;
        }
    }
 
    // Driver Code
    public static void main(String args[]) {
        int N = 38;
 
        System.out.println(minOperation(N));
    }
}
 
// This code is contributed by gfgking.


Python3
# python program for the above approach
import math
 
# Function check whether a number
# is prime or not
def isPrime(n):
 
    # Corner case
    if (n <= 1):
        return False
 
    # Check from 2 to square root of n
    for i in range(2, int(math.sqrt(n)) + 1):
        if (n % i == 0):
            return False
 
    return True
 
# Function to minimum operations required
# to reduce the number to a prime number
def minOperation(N):
 
    # Because 1 cannot be converted to prime
    if (N == 1):
        return -1
 
    # If given number is already prime
    # return 0
    if (isPrime(N) == True):
        return 0
 
    # If number is not prime
    else:
        # Variable for total count
        count = 0
        i = 2
 
        # If number is not equal to i
        while (N != i):
           
            # If N is completely divisible by i
            while (N % i == 0):
               
                # Temporary variable to store
                # current number
                temp = N
 
                # Update the number by decrementing
                # with highest divisor
                N -= (temp // i)
 
                # Increment count by 1
                count += 1
                if (isPrime(N)):
                    return count
            i += 1
 
        # Return the count
        return count
 
# Driver Code
if __name__ == "__main__":
    N = 38
    print(minOperation(N))
 
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
 
public class GFG {
     
    // Function check whether a number
    // is prime or not
    public static bool isPrime(int n)
    {
       
        // Corner case
        if (n <= 1)
            return false;
 
        // Check from 2 to square root of n
        for (int i = 2; i <= Math.Sqrt(n); i++)
            if (n % i == 0)
                return false;
 
        return true;
    }
 
    // Function to minimum operations required
    // to reduce the number to a prime number
    public static int minOperation(int N) {
        // Because 1 cannot be converted to prime
        if (N == 1)
            return -1;
 
        // If given number is already prime
        // return 0
        if (isPrime(N) == true) {
            return 0;
        }
 
        // If number is not prime
        else {
            // Variable for total count
            int count = 0;
            int i = 2;
 
            // If number is not equal to i
            while (N != i) {
                 
                // If N is completely divisible by i
                while (N % i == 0) {
                     
                    // Temporary variable to store
                    // current number
                    int temp = N;
 
                    // Update the number by decrementing
                    // with highest divisor
                    N -= (temp / i);
 
                    // Increment count by 1
                    count++;
                     
                    if (isPrime(N))
                        return count;
                }
                i++;
            }
 
            // Return the count
            return count;
        }
    }
 
    // Driver Code
    public static void Main(string []args) {
        int N = 38;
 
        Console.WriteLine(minOperation(N));
    }
}
 
// This code is contributed by AnkThon


Javascript



输出
1

时间复杂度: O(sqrtN)

辅助空间: O(1)