📜  计数小于通过用素数替换数组和的数字直到数字形成的数字的素数

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

计数小于通过用素数替换数组和的数字直到数字形成的数字的素数

给定一个大小为N且只有正元素的数组arr[] ,任务是找到小于以下操作后形成的素数的素数:

  • 添加给定数组的所有元素,比如sum
  • 替换总和的每个数字 出现在0和该数字之间的素数的总数。

例子:

算法:这是一个简单的基于实现的问题。这个想法是像上面提到的那样一个一个地执行操作,最后计算素数的数量。

请按照以下步骤解决问题。

  • 求给定数组的总和。
  • 将 sum 转换为字符串,例如S
  • 遍历字符串并将每个字符替换为0和该字符之间的素数。
  • 将新形成的字符串转换为整数Y。
  • 计算0Y之间的素数总数并返回。

下面是上述方法的实现。

C++
// C++ code to implement the approach
 
#include 
using namespace std;
 
// Funtion to check prime
bool checkPrime(int numberToCheck)
{
    if (numberToCheck == 1
        || numberToCheck == 0) {
        return false;
    }
    for (int i = 2; i * i <= numberToCheck;
         i++) {
        if (numberToCheck % i == 0) {
            return false;
        }
    }
    return true;
}
 
// Function to calculate total prime numbers
// between 0 to r
int totalprime(int r)
{
    // Count the number of primes
    int count = 0;
    for (int i = r; i >= 0; i--) {
        count += checkPrime(i);
    }
    return count;
}
 
// Function to find required count of primes
int findNum(int arr[], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
 
    // Converting sum to string
    string s = to_string(sum);
 
    // Calculating total prime numbers:
    for (int i = 0; i < s.length(); i++) {
        s[i] = totalprime(s[i] - '0') + '0';
    }
 
    // Converting newly formed string s
    // to integer.
    int y = stoi(s);
    return totalprime(y);
}
 
// Driver's code
int main()
{
    int arr[] = { 7, 12, 9, 27, 1 };
    int N = 5, sum = 0;
 
    // Function call
    cout << findNum(arr, N);
    return 0;
}


Java
// Java code to implement the approach
import java.io.*;
import java.util.*;
 
class GFG
{
   
  // Funtion to check prime
  public static boolean checkPrime(int numberToCheck)
  {
    if (numberToCheck == 1 || numberToCheck == 0) {
      return false;
    }
    for (int i = 2; i * i <= numberToCheck; i++) {
      if (numberToCheck % i == 0) {
        return false;
      }
    }
    return true;
  }
 
  // Function to calculate total prime numbers
  // between 0 to r
  public static int totalprime(int r)
  {
     
    // Count the number of primes
    int count = 0;
    for (int i = r; i >= 0; i--) {
      if (checkPrime(i) == true)
        count += 1;
    }
    return count;
  }
 
  // Function to find required count of primes
  public static int findNum(int arr[], int n)
  {
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += arr[i];
    }
 
    // Converting sum to string
    StringBuilder s
      = new StringBuilder(Integer.toString(sum));
 
    // Calculating total prime numbers:
    for (int i = 0; i < s.length(); i++) {
      s.setCharAt(i,
                  (char)(totalprime(s.charAt(i) - '0')
                         + '0'));
    }
 
    // Converting newly formed string s
    // to integer.
    int y = Integer.parseInt(s.toString());
    return totalprime(y);
  }
  public static void main(String[] args)
  {
    int arr[] = { 7, 12, 9, 27, 1 };
    int N = 5, sum = 0;
 
    // Function call
    System.out.print(findNum(arr, N));
  }
}
 
// This code is contributed by Rohit Pradhan


Python3
# Python code to implement the approach
 
 
# Funtion to check prime
def checkPrime(numberToCheck):
    if (numberToCheck == 1 or numberToCheck == 0):
        return False
    i = 2
    while(i * i <= numberToCheck):
        if (numberToCheck % i == 0):
            return False
        i += 1
    return True
 
# Function to calculate total prime numbers
# between 0 to r
def totalprime(r):
   
    # Count the number of primes
    count = 0
    for i in range(r,-1,-1):
        count += checkPrime(i)
    return count
 
# Function to find required count of primes
def findNum(arr, n):
    sum = 0
    for i in range(n):
        sum += arr[i]
 
    # Converting sum to string
    s = str(sum)
 
    # Calculating total prime numbers:
    for i in range(len(s)):
        s = s.replace(s[i],chr(totalprime(ord(s[i]) - ord('0')) + ord('0')))
 
    # Converting newly formed string s
    # to integer.
    y = int(s)
    return totalprime(y)
 
# Driver's code
arr = [ 7, 12, 9, 27, 1 ]
N,sum = 5,0
 
# Function call
print(findNum(arr, N))
 
# This code is contributed by shinjanpatra


Javascript


输出
11

时间复杂度: 在)
辅助空间: O(1)