📜  平衡素数

📅  最后修改于: 2021-04-27 06:05:52             🧑  作者: Mango

在数论中,平衡素数是在其上下具有相等大小素数间隙的素数,因此它等于上下最接近素数的算术平均值。或代数地表示,给定素数p n ,其中n是素数在有序数集中的索引,

 \Huge p_n=  \frac{p_{n-1}+p_{n+1}}{2}

前几个平衡质数是5、53、157、173……

给定正整数N。任务是打印第N个平衡素数。
例子:

Input : n = 2
Output : 53

Input : n = 3
Output : 157

这个想法是使用Eratosthenes的Sieve生成质数并将其存储在数组中。现在遍历数组以检查它是否是平衡素数,并继续计算平衡素数。一旦达到第n个素数,就将其返回。

以下是此方法的实现:

C++
// CPP Program to find Nth Balanced Prime
#include
#define MAX 501
using namespace std;
  
// Return the Nth balanced prime.
int balancedprime(int n)
{
    // Sieve of Eratosthenes
    bool prime[MAX+1];
    memset(prime, true, sizeof(prime));
  
    for (int p = 2; p*p <= MAX; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true)
        {
            // Update all multiples of p
            for (int i = p*2; i <= MAX; i += p)
                prime[i] = false;
        }
    }
  
    // storing all primes
    vector v;
    for (int p = 3; p <= MAX; p += 2)
       if (prime[p])
          v.push_back(p);
            
    int count = 0;
      
    // Finding the Nth balanced Prime
    for (int i = 1; i < v.size(); i++)
    {
        if (v[i] == (v[i+1] + v[i - 1])/2)
          count++;
            
        if (count == n)
          return v[i];
    }
}
  
// Driven Program
int main()
{
  int n = 4;  
  cout << balancedprime(n) << endl;
  return 0;
}


Java
// Java Program to find Nth Balanced Prime
import java.util.*;
  
public class GFG
{
    static int MAX = 501;
  
    // Return the Nth balanced prime.
    public static int balancedprime(int n)
    {
          
        // Sieve of Eratosthenes
        boolean[] prime = new boolean[MAX+1];
          
        for(int k = 0 ; k < MAX+1; k++)
            prime[k] = true;
  
        for (int p = 2; p*p <= MAX; p++)
        {
              
            // If prime[p] is not changed,
            // then it is a prime
            if (prime[p] == true)
            {
                  
                // Update all multiples of p
                for (int i = p*2; i <= MAX;
                                     i += p)
                    prime[i] = false;
            }
        }
  
        // storing all primes
        Vector v = 
                     new Vector();
        for (int p = 3; p <= MAX; p += 2)
            if (prime[p])
                v.add(p);
  
        int count = 0;
  
        // Finding the Nth balanced Prime
        for (int i = 1; i < v.size(); i++)
        {
            if ((int)v.get(i) == ((int)v.get(i+1)
                           + (int)v.get(i-1))/2)
                count++;
              
            if (count == n)
                return (int) v.get(i);
        }
  
        return 1;
    }
      
    // Driven Program
    public static void main(String[] args)
    {
        int n = 4;
        System.out.print(balancedprime(n));
    }
}
  
// This code is contributed by Prasad Kshirsagar


Python3
# Python3 code to find Nth Balanced Prime
  
MAX = 501
  
# Return the Nth balanced prime.
def balancedprime( n ):
  
    # Sieve of Eratosthenes
    prime = [True]*(MAX+1)
    p=2
    while p * p <= MAX:
  
        # If prime[p] is not changed, 
        # then it is a prime
        if prime[p] == True:
          
            # Update all multiples of p
            i = p * 2
            while i <= MAX:
                prime[i] = False
                i = i + p
        p = p +1
          
    # storing all primes
    v = list()
    p = 3
    while p <= MAX:
        if prime[p]:
            v.append(p)
        p = p + 2
          
    count = 0
      
    # Finding the Nth balanced Prime
    i=1
    for i in range(len(v)):
        if v[i] == (v[i+1] + v[i - 1])/2:
            count += 1
              
        if count == n:
            return v[i] 
  
# Driven Program
n = 4
print(balancedprime(n)) 
  
# This code is contributed by "Sharad_Bhardwaj".


PHP


C#
// C# Program to find Nth Balanced Prime
using System;
using System.Collections.Generic;
public class GFG
{
    static int MAX = 501;
  
    // Return the Nth balanced prime.
    public static int balancedprime(int n)
    {
          
        // Sieve of Eratosthenes
        bool[] prime = new bool[MAX+1];
          
        for(int k = 0 ; k < MAX+1; k++)
            prime[k] = true;
  
        for (int p = 2; p*p <= MAX; p++)
        {
              
            // If prime[p] is not changed,
            // then it is a prime
            if (prime[p] == true)
            {
                  
                // Update all multiples of p
                for (int i = p*2; i <= MAX;
                                    i += p)
                    prime[i] = false;
            }
        }
  
        // storing all primes
        List v = new List();
        for (int p = 3; p <= MAX; p += 2)
            if (prime[p])
                v.Add(p);
  
        int c = 0;
        // Finding the Nth balanced Prime
        for (int i = 1; i < v.Count-1; i++)
        {
            if ((int)v[i]==(int)(v[i+1]+v[i-1])/2)
            c++;
            if (c == n)
                return (int) v[i];
        }
  
        return 1;
    }
      
    // Driven Program
    public static void Main()
    {
        int n = 4;
        Console.WriteLine(balancedprime(n));
    }
}
  
// This code is contributed by mits


输出:

173