📜  计算范围 [L, R] 中单个数字和也是素数的素数

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

计算范围 [L, R] 中单个数字和也是素数的素数

给定两个整数LR 。任务是计算[L, R]范围内的素数,其单个和也是素数。
通过将数字的数字相加直到剩下一个数字来获得单个和

例子

方法:天真的方法是迭代[L, R]范围内的每个数字并检查该数字是否为素数。如果该数字是素数,则找到其数字的单个和,然后再次检查单个和是否为素数。如果单个和是素数,则递增计数器并打印范围[L, R]中的当前元素。

下面是上述方法的实现。

C++14
// C++ program for above approach
#include 
using namespace std;
 
// Function to check whether 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 find single digit sum
int SingleDigitSum(int& n)
{
    if (n <= 9)
        return n;
    return (n % 9 == 0) ? 9 : n % 9;
}
 
// Function to find single digit primes
int countSingleDigitPrimes(int l, int r)
{
    int count = 0, i;
 
    for (i = l; i <= r; i++) {
        if (isPrime(i)
            && isPrime(SingleDigitSum(i))) {
            count++;
        }
    }
    return count;
}
 
// Driver Code
int main()
{
    // Input range
    int L = 1, R = 10;
 
    // Function Call
    cout << countSingleDigitPrimes(L, R);
 
    return 0;
}


Java
// Java program to implement
// the above approach
class GFG
{
 
  // Function to check whether number
  // is prime or not
  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 find single digit sum
  static int SingleDigitSum(int n)
  {
    if (n <= 9)
      return n;
    return (n % 9 == 0) ? 9 : n % 9;
  }
 
  // Function to find single digit primes
  static int countSingleDigitPrimes(int l, int r)
  {
    int count = 0, i;
 
    for (i = l; i <= r; i++) {
      if (isPrime(i)
          && isPrime(SingleDigitSum(i))) {
        count++;
      }
    }
    return count;
  }
 
  // Driver Code
  public static void main(String args[])
  {
 
    // Input range
    int L = 1, R = 10;
 
    // Function Call
    System.out.println(countSingleDigitPrimes(L, R));
 
  }
}
 
// This code is contributed by gfgking


Python3
# Python program for above approach
import math
 
# Function to check whether 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, math.floor(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
 
    return True
 
# Function to find single digit sum
def SingleDigitSum(n):
    if n <= 9:
        return n
    return 9 if (n % 9 == 0) else n % 9
 
# Function to find single digit primes
def countSingleDigitPrimes(l, r):
    count = 0
    i = None
 
    for i in range(l, r + 1):
        if isPrime(i) and isPrime(SingleDigitSum(i)):
            count += 1
    return count
 
# Driver Code
 
# Input range
L = 1
R = 10
 
# Function Call
print(countSingleDigitPrimes(L, R))
 
# This code is contributed by gfgking


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
  // Function to check whether number
  // is prime or not
  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 find single digit sum
  static int SingleDigitSum(int n)
  {
    if (n <= 9)
      return n;
    return (n % 9 == 0) ? 9 : n % 9;
  }
 
  // Function to find single digit primes
  static int countSingleDigitPrimes(int l, int r)
  {
    int count = 0, i;
 
    for (i = l; i <= r; i++) {
      if (isPrime(i)
          && isPrime(SingleDigitSum(i))) {
        count++;
      }
    }
    return count;
  }
 
  // Driver Code
  public static void Main()
  {
 
    // Input range
    int L = 1, R = 10;
 
    // Function Call
    Console.Write(countSingleDigitPrimes(L, R));
 
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
4


时间复杂度: O((R – L)*N^(1/2)) 其中 N 是 [L, R] 范围内的素数。
辅助空间: O(1)