Questions tagged [sieve-of-eratosthenes]

Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to a specified integer.

Sieve of Eratosthenes finds primes among the natural numbers above 1 between the composites, which it finds by direct enumeration from each prime (optimization: prime's square), as an arithmetic progression with the step equal to that prime.

The first prime number is 2.

In pseudocode, the timing issues aside, it is

primes = [2, 3, ...] \ [[p*p, p*p+p, ...] for p in primes]

469 questions
-1
votes
2 answers

How to get table size 2^32 in java

I have to get in java protected final static int [] SIEVE = new int [ 1 << 32 ]; But i cant force java to that. Max sieve what i get is 2^26 i need 2^32 to end my homework. I tried with mask but i need to have SIEVE[n] = k where min{k: k|n & k…
Bla bla
  • 111
  • 2
  • 8
-1
votes
1 answer

Why program is not working for n=100000 even if time complexity of sieve of eratothenes is O(nlog(log(n)))

#include using namespace std; int main() { int n; cout<<"enter the no till which prime nos to be found :"<>n; vector prime(n+1,0);// we want n index too for(int i=2;i<=n;i++) { …
Akarsh
  • 1
-1
votes
1 answer

How many numbers have n as their smallest prime factor within 10^6?

for _ in range(int(input())): n=int(input()) least_prime = [0] * (1000001) count=0 for i in range(2, int(1000001**0.5 + 1)): if least_prime[i] == 0: least_prime[i] = i for j in range(i * i,…
Chandrachud Pati
  • 425
  • 1
  • 5
  • 12
-1
votes
2 answers

How can sieve of Eratosthenes be implemented in O(n) time complexity?

There is implementation of this algorithm of finding prime numbers upto n in O(n*log(log(n)) time complexity. How can we achieve it in O(n) time complexity?
-1
votes
1 answer

I'm trying to implement sieve of eratosthenes but it works only for numbers smaller then 33

I'm trying to write a program for the Sieve of Eratosthenes and it works but if the input number is 33 or greater I get this error: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 at…
hereToAsk
  • 11
  • 1
-1
votes
1 answer

Massive performance slowdown when changing int to unsigned long long in segmented sieve

I'm confused about the performance of my C program that implements a Segmented Sieve. I originally used only int as the datatype but to support bigger numbers I decided to switch to unsigned long long. I expected some performance penalty due to…
Chase
  • 5,315
  • 2
  • 15
  • 41
-1
votes
1 answer

Sum of primes. No matter where I cast long to int, my sum of primes results in a memory heap

I think I'm using the Eratosthenes sieve, since an array can't take long values as indexes, I get the memory error even when I use casting. Should I rather create a Map or List containing long values and compute that way ? This is the code: ** int…
-1
votes
2 answers

How to get all the prime numbers from 2 to 10^9? [sieve of eratosthenes is not working since the range is too long]

I am working on a program which prints the number of pairs of prime numbers (within a range L and R) whose difference is 6, where 2 <= L < R <= 10^9. eg., L=2 R=20 Ans: 4 Explanation: 4 pairs are there: {5, 11}, {7, 13}, {11, 17}, {13, 19} So my…
-1
votes
2 answers

Product of three primes divisible by sum of those primes

I found this problem in a cp contest which is over now so it can be answered. Three primes (p1,p2,p3) (not necessarily distinct) are called special if (p1+p2+p3) divides p1*p2*p3. We have to find the number of these special pairs if the primes…
I_Love_Islam
  • 73
  • 1
  • 3
  • 10
-1
votes
2 answers

Sieve of Eratosthenes (USING LINKEDLIST)

Im trying to figure out how i would manipulate Lists in order to find all the primes up to a number provided by the user, i have a list steps which im trying to follow which are create and fill list of possible primes Basically an arrayList that…
MaroTS
  • 35
  • 9
-1
votes
1 answer

Prime Generator PRIME1 on SPOJ

Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! Input The input begins with the number t of test cases in a single line (t<=10). In each of the next t…
Kivtas
  • 27
  • 1
  • 6
-1
votes
1 answer

Sieve of Eratosthenes (min and max)

I am using the Sieve of Eratosthenes algorithm to find the prime numbers in a range (from a min value to a max value). However, I cannot seem to get it to work if I include a min value. Here is my code in Java: protected static List
Richard
  • 8,193
  • 28
  • 107
  • 228
-1
votes
1 answer

Cuda Sieve of Eratosthenes Error when larger than 1000000

I am new to cuda and am trying to use it to carry out the Sieve of Eratosthenes. The code works for primes below 1000000. Above it i get an unknown kernal launch error. Now I understand this is because I am trying to launch a grid with too many…
brad119
  • 61
  • 1
  • 5
-1
votes
1 answer

C code for Sieve of Erastothenes

Can someone please tell me why my programme is outputting all primes up to inputted n and also just the number n+2 (whether it is prime or not)? And also sometimes n+1 if it is prime, e.g. for inputting n=10, output is 2,3,5,7,11,12. Here is my…
-1
votes
1 answer

Sieve of Eratosthenes C implementation error

So I am making next homework with input numbers to be dissolved to primes multiplication in output. I was confused when it showed up only prime number 2, so I did a proper control dump of the allocated primes array using this function: long*…
Polda18
  • 162
  • 14