第N个Ramanujan素数是为其的最小整数R n
其中π(x)是素数计数函数
请注意,整数Rn一定是质数: π(x)–π(x / 2) ,因此, π(x)必须通过在x = R n处获得另一个质数来增加。由于π(x)–π(x / 2)最多可以增加1,
R n的范围是(2n log(2n),4n log(4n)) 。
拉马努詹素数:
2, 11, 17, 29, 41, 47, 59, 67, 71, 97
对于给定的N ,任务是打印第N个Ramanujan素数
例子:
Input : N = 5
Output : 2, 11, 17, 29, 41
Input : N = 10
Output : 2, 11, 17, 29, 41, 47, 59, 67, 71, 97
方法:
让我们将解决方案分为几部分,
首先,我们将使用Eratosthenes筛子使所有质数小于10 ^ 6
现在我们必须找到π(x)的值, π(x)是小于或等于x的素数的计数。素数按升序存储。因此,我们可以执行二进制搜索来找到所有小于x的素数,这可以在O(log n)中完成。
现在我们得到的范围R n介于: 2n log(2n)
下面是上述方法的实现:
C++
// CPP program to find Ramanujan numbers
#include
using namespace std;
#define MAX 1000000
// FUnction to return a vector of primes
vector addPrimes()
{
int n = MAX;
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed, then it is a prime
if (prime[p] == true)
{
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
vector ans;
// Print all prime numbers
for (int p = 2; p <= n; p++)
if (prime[p])
ans.push_back(p);
return ans;
}
// Function to find number of primes
// less than or equal to x
int pi(int x, vector v)
{
int l = 0, r = v.size() - 1, m, in = -1;
// Binary search to find out number of
// primes less than or equal to x
while (l <= r) {
m = (l + r) / 2;
if (v[m] <= x) {
in = m;
l = m + 1;
}
else {
r = m - 1;
}
}
return in + 1;
}
// Function to find the nth ramanujan prime
int Ramanujan(int n, vector v)
{
// For n>=1, a(n)<4*n*log(4n)
int upperbound = 4 * n * (log(4 * n) / log(2));
// We start from upperbound and find where
// pi(i)-pi(i/2) v = addPrimes();
for (int i = 1; i <= n; i++) {
cout << Ramanujan(i, v);
if(i!=n)
cout << ", ";
}
}
// Driver code
int main()
{
int n = 10;
Ramanujan_Numbers(n);
return 0;
}
Java
// Java program to find Ramanujan numbers
import java.util.*;
class GFG
{
static int MAX = 1000000;
// FUnction to return a vector of primes
static Vector addPrimes()
{
int n = MAX;
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true.
// A value in prime[i] will finally be false
// if i is Not a prime, else true.
boolean []prime= new boolean[n + 1];
Arrays.fill(prime, true);
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true)
{
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
Vector ans = new Vector();
// Print all prime numbers
for (int p = 2; p <= n; p++)
if (prime[p])
ans.add(p);
return ans;
}
// Function to find number of primes
// less than or equal to x
static int pi(int x, Vector v)
{
int l = 0, r = v.size() - 1, m, in = -1;
// Binary search to find out number of
// primes less than or equal to x
while (l <= r)
{
m = (l + r) / 2;
if (v.get(m) <= x)
{
in = m;
l = m + 1;
}
else
{
r = m - 1;
}
}
return in + 1;
}
// Function to find the nth ramanujan prime
static int Ramanujan(int n, Vector v)
{
// For n>=1, a(n)<4*n*log(4n)
int upperbound = (int) (4 * n * (Math.log(4 * n) /
Math.log(2)));
// We start from upperbound and find where
// pi(i)-pi(i/2) v = addPrimes();
for (int i = 1; i <= n; i++)
{
System.out.print(Ramanujan(i, v));
if(i != n)
System.out.print(", ");
}
}
// Driver code
public static void main(String[] args)
{
int n = 10;
Ramanujan_Numbers(n);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find Ramanujan numbers
from math import log, ceil
MAX = 1000000
# FUnction to return a vector of primes
def addPrimes():
n = MAX
# Create a boolean array "prime[0..n]"
# and initialize all entries it as true.
# A value in prime[i] will finally be
# false if i is Not a prime, else true.
prime = [True for i in range(n + 1)]
for p in range(2, n + 1):
if p * p > n:
break
# If prime[p] is not changed,
# then it is a prime
if (prime[p] == True):
# Update all multiples of p
# greater than or equal to the
# square of it. numbers which are
# multiple of p and are less than p^2
# are already been marked.
for i in range(2 * p, n + 1, p):
prime[i] = False
ans = []
# Print all prime numbers
for p in range(2, n + 1):
if (prime[p]):
ans.append(p)
return ans
# Function to find number of primes
# less than or equal to x
def pi(x, v):
l, r = 0, len(v) - 1
# Binary search to find out number of
# primes less than or equal to x
m, i = 0, -1
while (l <= r):
m = (l + r) // 2
if (v[m] <= x):
i = m
l = m + 1
else:
r = m - 1
return i + 1
# Function to find the nth ramanujan prime
def Ramanujan(n, v):
# For n>=1, a(n)<4*n*log(4n)
upperbound = ceil(4 * n * (log(4 * n) / log(2)))
# We start from upperbound and find where
# pi(i)-pi(i/2)
C#
// C# program to find Ramanujan numbers
using System;
using System.Collections.Generic;
class GFG
{
static int MAX = 1000000;
// FUnction to return a vector of primes
static List addPrimes()
{
int n = MAX;
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true.
// A value in prime[i] will finally be false
// if i is Not a prime, else true.
Boolean []prime = new Boolean[n + 1];
for(int i = 0; i < n + 1; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true)
{
// Update all multiples of p greater than
// or equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
List ans = new List();
// Print all prime numbers
for (int p = 2; p <= n; p++)
if (prime[p])
ans.Add(p);
return ans;
}
// Function to find number of primes
// less than or equal to x
static int pi(int x, List v)
{
int l = 0, r = v.Count - 1, m, i = -1;
// Binary search to find out number of
// primes less than or equal to x
while (l <= r)
{
m = (l + r) / 2;
if (v[m] <= x)
{
i = m;
l = m + 1;
}
else
{
r = m - 1;
}
}
return i + 1;
}
// Function to find the nth ramanujan prime
static int Ramanujan(int n, List v)
{
// For n>=1, a(n)<4*n*log(4n)
int upperbound = (int) (4 * n * (Math.Log(4 * n) /
Math.Log(2)));
// We start from upperbound and find where
// pi(i)-pi(i/2) v = addPrimes();
for (int i = 1; i <= n; i++)
{
Console.Write(Ramanujan(i, v));
if(i != n)
Console.Write(", ");
}
}
// Driver code
public static void Main(String[] args)
{
int n = 10;
Ramanujan_Numbers(n);
}
}
// This code is contributed by 29AjayKumar
输出:
2, 11, 17, 29, 41, 47, 59, 67, 71, 97