sieve-of-eratosthenes

Sieve of Eratosthenes without arrays?

泪湿孤枕 提交于 2019-12-02 07:08:49
I have to write a java code for the 'sieve of eratosthenes' algorithm to print out primes up to a given max value on the console but I'm not allowed to use arrays. Our professor told us it is possible to do only with the help of loops. So I thought a lot and googled a lot about this topic and couldn't find an answer. I dont think it's possible at all because you have store the information which digits are already crossed out somewhere. my code until now: public static void main(String[] args) { int n = 100; int mark = 2; System.out.print("Primes from 1 to "+n+": 2, "); for (int i = 2; i <= n;

prime number summing still slow after using sieve

独自空忆成欢 提交于 2019-12-01 12:57:21
I had a go at a project euler coding challenge below, the answer given by the code is correct but I do not understand why its taking nearly a minute to run. It was finishing with similar times prior to using a sieve. Others users are reporting times as low as milliseconds. I assume I am making a basic error somewhere... // The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. // Find the sum of all the primes below two million. public static long Ex010() { var sum = 0L; var sieve = new bool[2000000]; var primes = new List<int>(10000); for (int i = 2; i < sieve.Length; i++) { if (sieve[i-1])

prime number summing still slow after using sieve

陌路散爱 提交于 2019-12-01 11:04:32
问题 I had a go at a project euler coding challenge below, the answer given by the code is correct but I do not understand why its taking nearly a minute to run. It was finishing with similar times prior to using a sieve. Others users are reporting times as low as milliseconds. I assume I am making a basic error somewhere... // The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. // Find the sum of all the primes below two million. public static long Ex010() { var sum = 0L; var sieve = new bool

Sieve of Eratosthenes using precalculated primes

别等时光非礼了梦想. 提交于 2019-12-01 10:46:56
I've all prime numbers that can be stored in 32bit unsigned int and I want to use them to generate some 64bit prime numbers . using trial division is too slow even with optimizations in logic and compilation. I'm trying to modify Sieve of Eratosthenes to work with the predefined list, as follow: in array A from 2 to 4294967291 in array B from 2^32 to X inc by 1 find C which is first multiple of current prime. from C mark and jump by current prime till X. go to 1. The problem is step 3 which use modulus to find the prime multiple, such operation is the reason i didn't use trail division. Is

Sieve of Eratosthenes Issue Java

雨燕双飞 提交于 2019-12-01 08:22:28
问题 I've got an issue with an assignment that I have requiring the use of arrays. I need to create the Sieve of Eratosthenes algorithm and print out all the prime numbers. I'm quite confused because as far as I can tell, my order of operations is correct. Here is the code: //Declare the array boolean numbers [] = new boolean[1000]; int y = 0; //Declare all numbers as true to begin for(int i = 2; i < 1000;i++){ numbers[i] = true; } //Run loop that increases i and multiplies it by increasing

Program to find all primes in a very large given range of integers

限于喜欢 提交于 2019-12-01 06:10:37
i came across this following question on a programming website : 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 lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space. I came up with the following solution : import java.util.*; public class PRIME1 { static int numCases; static int left, right; static boolean[] initSieve = new boolean[32000]; static

Segmented Sieve of Atkin, possible?

社会主义新天地 提交于 2019-12-01 05:40:38
I am aware of the fact that the Sieve of Eratosthenes can be implemented so that it finds primes continuosly without an upper bound (the segmented sieve). My question is, could the Sieve of Atkin/Bernstein be implemented in the same way? Related question: C#: How to make Sieve of Atkin incremental However the related question has only 1 answer, which says "It's impossible for all sieves", which is obviously incorrect. Atkin/Bernstein give a segmented version in Section 5 of their original paper . Presumably Bernstein's primegen program uses that method. GordonBGood In fact, one can implement

Fast algorithm for finding prime numbers? [duplicate]

此生再无相见时 提交于 2019-11-30 20:10:39
This question already has an answer here: Which is the fastest algorithm to find prime numbers? 14 answers First of all - I checked a lot in this forum and I haven't found something fast enough . I try to make a function that returns me the prime numbers in a specified range. For example I did this function (in C#) using the sieve of Eratosthenes. I tried also Atkin's sieve but the Eratosthenes one runs faster (in my implementation): public static void SetPrimesSieve(int Range) { Primes = new List<uint>(); Primes.Add(2); int Half = (Range - 1) >> 1; BitArray Nums = new BitArray(Half, false);

Why is this prime test so slow?

笑着哭i 提交于 2019-11-30 18:40:45
This code was taken from the book "Haskell Road to Logic, Math and Programming". It implements sieve of eratosthenes algorithm and solves Project Euler Problem 10. sieve :: [Integer] -> [Integer] sieve (0 : xs) = sieve xs sieve (n : xs) = n : sieve (mark xs 1 n) where mark :: [Integer] -> Integer -> Integer -> [Integer] mark (y:ys) k m | k == m = 0 : (mark ys 1 m) | otherwise = y : (mark ys (k+1) m) primes :: [Integer] primes = sieve [2..] -- Project Euler #10 main = print $ sum $ takeWhile (< 2000000) primes Actually it runs even slower then the naive prime test. Can someone explain this

Sieve of Atkin explanation

蓝咒 提交于 2019-11-30 11:27:13
问题 I am doing a project at the moment and I need an efficient method for calculating prime numbers. I have used the sieve of Eratosthenes but, I have been searching around and have found that the sieve of Atkin is a more efficient method. I have found it difficult to find an explanation (that I have been able to understand!) of this method. How does it work? Example code (preferably in C or python) would be brilliant. Edit: thanks for your help, the only thing that I still do not understand is