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

hackerrank Ramanujan’s Prime Substrings 1 problem

The question is: Ramanujan is so fond of playing number games. One day Ramanujan and Anish played a game. Ramanujan gave Anish a number string and asked him to find all the distinct substrings of size at most six that are prime. Anish being good at…
Ga25
  • 1
  • 1
-2
votes
1 answer

Prime number generator doesn't work for small numbers

Since 2 days I'm struggling with PRIME1 problem from SPOJ. I managed to write a program that works fine for large numbers, but I can't figure out why does it display such messed numbers in the beginning range (usually 1-11). I'm using Segmented…
-2
votes
1 answer

Python find sum of prime factors without loop

I'm trying to produce the sum of all prime factors of a number without using loop. However if the result of prime_factor(m, k) bigger than 2, when go to main(n) after factor=prime_factor(m, k), factor will be None def prime_factor(m, k): if…
-2
votes
1 answer

Sum of prime numbers below 10^12

How do I find the sum of all primes below 10^12? I use sieve of Eratosthenes with O(n * log(log(n))) but I want an algorithm to calculate this faster. My code runs in 4 sec for 10^8, but it takes many hours to calculate 10^12. This is my code:…
Iman.Ejt
  • 11
  • 10
-2
votes
1 answer

Project Euler #10 How to pre-calculate sum of primes?

Here is the question: The sum of the primes below 10 is 2+3+5+7=17. Find the sum of all the primes not greater than given N. Input Format : The first line contains an integer T i.e. number of the test cases. The next T lines will contains an…
Aneesh Dandime
  • 123
  • 1
  • 11
-2
votes
1 answer

python - sieve of Eratosthenes when number is prime print "1" when not print "0"

I must create a function f(n) which value is 1 when the number is prime or value is 0 when isn't prime. Code is working but prints in revers order. For example: f(6)= 0 0 1 1 0 1 0 def prime(n): if n<2: return False for i in range(2,n): …
Martin
  • 1
  • 4
-2
votes
2 answers

need help about sieve of eratosthenes in free pascal

my teacher gave me this : n<=10^6; an array of n integer :ai..an(ai<=10^9); find all prime numbers . he said something about sieve of eratosthenes,and I read about it,also the wheel factorization too,but I still couldn't figure it out how to get the…
-2
votes
1 answer

Sieve of Eratosthenes in AT&T Assemby

I have written a simple program in C to find all prime numbers within a given range using Sieve of Eratosthenes. In school, we are currently learning assembly in a class, but I can't figure out how to write the three loops. I have somewhat figured…
erikst
  • 23
  • 5
-2
votes
1 answer

Sieve of Eratosthenes algorithm not working for large limits

I have programmed a sieve of Eratosthenes algorithm in C++, and it works fine for smaller numbers that I have tested it with. However, when I use large numbers, i.e. 2 000 000 as the upper limit, the program begins giving wrong answers. Can anyone…
-3
votes
1 answer

How can I complete my code about the Sieve of Erathostenes?

It's about finding the prime numbers from 2 to 1000 using this method but I can't get the solution and I've been thinking and trying to solve this issue for three days. I'm desperate for help so if anyone can help me I would really appreciate it I…
LGV
  • 11
  • 2
-3
votes
1 answer

Is it a valid code for Sieve of Eratosthenes?

I recently learned Sieve of Eratosthenes to find prime numbers. After knowing the method i wrote this code for it. Would it be a valid C++ code for Sieve of Eratosthenes? #include using namespace std; int main() { int n; cin>>n; bool…
Suyash
  • 39
  • 5
-3
votes
2 answers

sieve or eratosthenes calculator -- running into memory issues and crashing with numbers >=1,000,000

I'm not exactly sure why this is. I tried changing the variables to long long, and I even tried doing a few other things -- but its either about the inefficiency of my code (it literally does the whole process of finding all primes up to the number,…
spiroth10
  • 31
  • 4
-3
votes
2 answers

Prime numbers in c language

I want to find prime numbers with multithreading and using Sieve of E. function.I write some piece of codes. If the program will run, the user enter a max number and thread number. The program should create threads that given thread number. The…
esrtr
  • 99
  • 2
  • 12
-3
votes
1 answer

Sieve for 8 digit primes

I am not able to find prime numbers of 8 digits in spite of increasing size of array a. It is working for smaller numbers: #include #include int main() { int n,a[100000],i,m,k; scanf("%d",&n); for (i=2;i<=n;i++) …
-3
votes
1 answer

Implementing sieve of eratosthenes and then getting highest prime factor

I am stuck on trying to get the sieve to work. When I debug it, it tells me that stuff like 9 and 15 still evaluate to true when they go through the sieve. What causes this? Also, am I using the vector properly to get the highest prime…
Tetramputechture
  • 2,911
  • 2
  • 33
  • 48
1 2 3
31
32