📌  相关文章
📜  将N表示为质数和平方的两倍的方式的计数

📅  最后修改于: 2021-05-04 19:32:48             🧑  作者: Mango

给定整数N ,任务是计算路数,以使N可以写为素数和平方的两倍的总和,即

N = 2*A^{2} + P

,其中P可以是任何质数,而A是任何正整数。
笔记:

N <= 10^{6}

例子:

方法:想法是使用Eratosthenes的Seive查找所有素数,然后针对每个素数检查从1开始的每个可能的数。如果任何素数和平方的两倍等于给定数,则增加方式数1。
下面是上述方法的实现:

C++
// C++ implementation to count the
// number of ways a number can be
// written as sum of prime number
// and twice a square
 
#include 
 
using namespace std;
long long int n = 500000 - 2;
vector v;
 
// Function to mark all the
// prime numbers using sieve
void sieveoferanthones()
{
    bool prime[n + 1];
 
    // Intially all the numbers
    // are marked as prime
    memset(prime, true,
           sizeof(prime));
 
    // Loop to mark the prime numbers
    // upto the Square root of N
    for (long long int i = 2; i <= sqrt(n);
         i++) {
        if (prime[i])
            for (long long int j = i * i;
                 j <= n; j += i) {
                prime[j] = false;
            }
    }
 
    // Loop to store the prime
    // numbers in an array
    for (long long int i = 2; i < n; i++) {
        if (prime[i])
            v.push_back(i);
    }
}
 
// Function to find the number
// ways to represent a number
// as the sum of prime number and
// square of a number
void numberOfWays(long long int n)
{
    long long int count = 0;
 
    // Loop to iterate over all the
    // possible prime numbers
    for (long long int j = 1;
         2 * (pow(j, 2)) < n; j++) {
        for (long long int i = 1;
             v[i] + 2 <= n; i++) {
 
            // Incrment the count if
            // the given number is a
            // valid number
            if (n == v[i]
+ (2 * (pow(j, 2))))
                count++;
        }
    }
    cout << count << endl;
}
 
// Driver Code
int main()
{
    sieveoferanthones();
    long long int n = 9;
 
    // Function Call
    numberOfWays(n);
    return 0;
}


Java
// Java implementation to count the
// number of ways a number can be
// written as sum of prime number
// and twice a square
import java.util.*;
class GFG{
 
static int n = 500000 - 2;
static Vector v =
              new Vector<>();
 
// Function to mark all the
// prime numbers using sieve
static void sieveoferanthones()
{
  boolean []prime = new boolean[n + 1];
 
  // Intially all the numbers
  // are marked as prime
  Arrays.fill(prime, true);
 
  // Loop to mark the prime numbers
  // upto the Square root of N
  for (int i = 2;
           i <= Math.sqrt(n); i++)
  {
    if (prime[i])
      for (int j = i * i;
               j <= n; j += i)
      {
        prime[j] = false;
      }
  }
 
  // Loop to store the prime
  // numbers in an array
  for (int i = 2; i < n; i++)
  {
    if (prime[i])
      v.add(i);
  }
}
 
// Function to find the number
// ways to represent a number
// as the sum of prime number and
// square of a number
static void numberOfWays(int n)
{
  int count = 0;
 
  // Loop to iterate over all the
  // possible prime numbers
  for (int j = 1; 2 *
      (Math.pow(j, 2)) < n; j++)
  {
    for (int i = 1; v.get(i) +
             2 <= n; i++)
    {
      // Incrment the count if
      // the given number is a
      // valid number
      if (n == v.get(i) +
         (2 * (Math.pow(j, 2))))
        count++;
    }
  }
  System.out.print(count + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
  sieveoferanthones();
  int n = 9;
 
  // Function Call
  numberOfWays(n);
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 implementation to count the
# number of ways a number can be
# written as sum of prime number
# and twice a square
import math
 
n = 500000 - 2
v = []
 
# Function to mark all the
# prime numbers using sieve
def sieveoferanthones():
     
    prime = [1] * (n + 1)
 
    # Loop to mark the prime numbers
    # upto the Square root of N
    for i in range(2, int(math.sqrt(n)) + 1):
        if (prime[i] != 0):
             
            for j in range(i * i, n + 1, i):
                prime[j] = False
             
    # Loop to store the prime
    # numbers in an array
    for i in range(2, n):
        if (prime[i] != 0):
            v.append(i)
     
# Function to find the number
# ways to represent a number
# as the sum of prime number and
# square of a number
def numberOfWays(n):
     
    count = 0
 
    # Loop to iterate over all the
    # possible prime numbers
    j = 1
    while (2 * (pow(j, 2)) < n):
        i = 1
        while (v[i] + 2 <= n):
 
            # Incrment the count if
            # the given number is a
            # valid number
            if (n == v[i] +
               (2 * (math.pow(j, 2)))):
                count += 1
                 
            i += 1
             
        j += 1
         
    print(count)
 
# Driver Code
sieveoferanthones()
n = 9
 
# Function call
numberOfWays(n)
 
# This code is contributed by sanjoy_62


C#
// C# implementation to count the
// number of ways a number can be
// written as sum of prime number
// and twice a square        
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{        
             
static int n = 500000 - 2;
 
static ArrayList v = new ArrayList();
 
// Function to mark all the
// prime numbers using sieve
static void sieveoferanthones()
{
    bool []prime = new bool[n + 1];
 
    // Intially all the numbers
    // are marked as prime
    Array.Fill(prime, true);
 
    // Loop to mark the prime numbers
    // upto the Square root of N
    for(int i = 2;
            i <= (int)Math.Sqrt(n); i++)
    {
        if (prime[i])
        {
            for(int j = i * i;
                    j <= n; j += i)
            {
                prime[j] = false;
            }
        }
    }
 
    // Loop to store the prime
    // numbers in an array
    for(int i = 2; i < n; i++)
    {
        if (prime[i])
            v.Add(i);
    }
}
 
// Function to find the number
// ways to represent a number
// as the sum of prime number and
// square of a number
static void numberOfWays(int n)
{
    int count = 0;
 
    // Loop to iterate over all the
    // possible prime numbers
    for(int j = 1;
            2 * (Math.Pow(j, 2)) < n; j++)
    {
        for(int i = 1;
           (int)v[i] + 2 <= n; i++)
        {
             
            // Incrment the count if
            // the given number is a
            // valid number
            if (n == (int)v[i] +
                     (2 * (Math.Pow(j, 2))))
                count++;
        }
    }
    Console.Write(count);
}        
         
// Driver Code        
public static void Main (string[] args)
{        
    sieveoferanthones();
    int n = 9;
 
    // Function call
    numberOfWays(n);
}        
}
 
// This code is contributed by rutvik_56


输出:
1