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
1 answer

I am getting a segmentation error in PRIME1 on spoj. How should I eradicate it?

My answer to problem PRIME1 please explain me where am I wrong. I am receiving a segmentation error. here it is: #include #include using namespace std; int main(int argc, char** argv) { int…
ANiK3T
  • 47
  • 1
  • 8
-1
votes
1 answer

Sieve of Eratosthenes returning incorrect answers

I'm a beginner trying to create a function to determine whether or not a value is prime or not. def isPrime(number): marked = [] ## create empty list for i in xrange(2, number+1): if i not in marked: ## begin loop to remove…
jhub1
  • 611
  • 3
  • 7
  • 19
-1
votes
2 answers

sieve of eratosthenes error java

I am just learning java and have an issue writing a program that uses the sieve of Eratosthenes. The compilation error is below the code. Any help with this problem is greatly appreciated. Thanks much. public class Main { public static void…
garserdt216
  • 163
  • 6
-1
votes
1 answer

Sieve of Eratosthenes Given with Range

I'm trying to generate a sequence of prime numbers starting from N to N_Max in C++. My approach is to use Sieve of Eratosthenes to generate these prime numbers: void runEratosthenesSieve(int upperBound) { int upperBoundSquareRoot =…
-1
votes
1 answer

How is this code working for finding the number of divisors of a number?

http://www.spoj.com/problems/NDIV/ This is the problem statement. Since i'm new to programming, this particular problem ripped me off, I found this particular code on the internet which when I tried submitting got AC. I want to know how this code…
dumb_iam
  • 31
  • 1
  • 8
-1
votes
1 answer

Impelementation error in finding prime no

Problem-To generate prime numbers. Approach used-Sieve of erathones without using optimisation . Problem-Code is generating only 2 and 3. #include int main() { int i,j; int primes[100]; for(i=0;i<=20;i++) { …
Beginner
  • 721
  • 11
  • 27
-1
votes
1 answer

Runtime error in finding prime numbers

Spoj problem- http://www.spoj.com/problems/PRIME1/ There is a logical error in the code and is not running. IDE-Codeblocks int main() { int t,*prime; long long int m,n,i,j; //t for number of test cases scanf("%d",&t); …
Beginner
  • 721
  • 11
  • 27
-1
votes
1 answer

Calculation error while using ceil function in C++

I m new to C++. I m trying to implement a segmented sieve to find prime numbers, between given numbers m and n. My logic might be also wrong. Below is the code i have written, long long m; long long n; std::cin >> m; std::cin >> n; vector
m_amber
  • 747
  • 3
  • 13
  • 23
-1
votes
2 answers

Segmented Sieve of Erastothenes C++ SPOJ

I'm aware this has been asked before, but I cannot fully understand how to implement Segmented Sieve of Eratosthenes. Problem The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two…
user3125772
  • 73
  • 1
  • 10
-1
votes
1 answer

Sieve of Eratosthenes C++ code

I'm fairly new to programming, and I just started C++ I found this problem, which involved generation all prime numbers upto "n". This is my code, where I've assumed "n" to be 10. I've tried my best. I'd really appreciate it if you guys could tell…
-1
votes
2 answers

BSP Sieve Of Erastothenes C implementation gives Segmentation fault (core dumped)

EDIT EDIT: This is my problem after using the program in the second comment, original post is below. > ./bspsieve 8 10 processors to use: 8 upper bound: 10 It took : 0.000076 seconds for proc 0 out of 8. *** glibc…
-1
votes
2 answers

C++ sieve of Eratosthenes with array

I'd like to code the famous Sieve of Eratosthenes in C++ using just array as it would be a set where I can delete some elements on the way to find out primes numbers. I don't want to use STL (vector, set)... Just array! How can I realize it? I try…
theskunk
  • 17
  • 2
  • 5
-1
votes
1 answer

Sieve of Eratosthenes algorithm using arrays in C

I have to follow the sieve of Eratosthenes algorithm, which is to: Initialize the array is_prime so that all the values of the elements will be true. Then, set the value of is_prime[1] to be false (since 1 is NOT prime.) For I=2 until sqrt(N) set…
-1
votes
3 answers

C++ Sieve of Eratosthenes finding 3 too many primes

I have a programming assignment to write a program in C++ that finds all primes less than n (user input). One half of the assignment involves the Sieve of Eratosthenes. My code is working (read: assignment is complete), but before I edited the…
-2
votes
1 answer

I am having a problem with my code on Sieve of Eratosthenes

See my code below. I keep getting an error code, and I don't understand what it means too. Where in the code can I look? def list_true(n): return [False for x in range(2)] + [x for x in range (2, n+1)] assert len(list_true(20)) == 21 assert…