Questions tagged [primes]

Primes or prime numbers are integers greater than 1 which are divisible only by themselves and 1, i.e.: 2, 3, 5, 7, 11, ... .

See Wikipedia on primes.

If your program for checking or computing prime numbers doesn't work, please check the following points before posting a question:

  • If it is too slow, try testing divisibility only up to and including the square root. If you want to compute all primes in a range better use the sieve of Eratosthenes, implemented properly it will be much faster.

  • If your algorithm says square numbers are prime you neglected the and including part above.

  • Check for overflow. n * n overflows a 32 bit signed integer for n > 46340. The sum of the first two million primes for Project Euler problem 10 will also overflow a 32 bit integer.

3346 questions
22
votes
13 answers

Finding prime numbers with the Sieve of Eratosthenes (Originally: Is there a better way to prepare this array?)

Note: Version 2, below, uses the Sieve of Eratosthenes. There are several answers that helped with what I originally asked. I have chosen the Sieve of Eratosthenes method, implemented it, and changed the question title and tags appropriately. …
eleven81
  • 6,301
  • 11
  • 37
  • 48
22
votes
5 answers

Sieve of Atkin explanation

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…
marc lincoln
  • 1,551
  • 4
  • 21
  • 25
21
votes
3 answers

What is the fastest deterministic primality test for numbers in the range 2^1024 to 2^4096?

I am writing an implementation of a cryptography protocol. So far I've been having a difficult time finding the fastest deterministic primality test for 1024-bit to 4096-bit integers (308- to 1233-digit numbers). I am aware of several options but…
jnm2
  • 7,960
  • 5
  • 61
  • 99
21
votes
9 answers

How do I generate Primes Using 6*k +- 1 rule

We know that all primes above 3 can be generated using: 6 * k + 1 6 * k - 1 However we all numbers generated from the above formulas are not prime. For Example: 6 * 6 - 1 = 35 which is clearly divisible by 5. To Eliminate such conditions, I…
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
21
votes
4 answers

Using LINQ to generate prime numbers

Following is an interview question: The following one-liner generates and displays the list of first 500 prime numbers. How would you optimize it using parallel LINQ while still keeping it a SINGLE C# STATEMENT: MessageBox.Show(string.Join(",", …
dotNET
  • 33,414
  • 24
  • 162
  • 251
21
votes
2 answers

The Sieve of Atkin

I have been trying to learn algorithms for generating prime numbers and I came across Sieve of Atkin on Wikipedia. I understand almost all parts of the algorithm, except a few. Here are the questions: How are the three quadratic equations below…
Shiv
  • 471
  • 4
  • 8
21
votes
5 answers

Explain this chunk of haskell code that outputs a stream of primes

I have trouble understanding this chunk of code: let sieve (p:xs) = p : sieve (filter (\ x -> x `mod` p /= 0) xs) in sieve [2 .. ] Can someone break it down for me? I understand there is recursion in it, but thats the problem I can't understand…
nubela
  • 1
  • 24
  • 75
  • 123
21
votes
3 answers

Write the biggest prime

I'm trying to solve the biggest prime programming praxis problem in C#. The problem is simple, print out or write to file the number: 257,885,161 − 1 (which has 17,425,170 digits) I have managed to solve it using the amazing GNU Multiple Precision…
marcob
  • 1,003
  • 2
  • 11
  • 23
20
votes
8 answers

How many iterations of Rabin-Miller should I use for cryptographic safe primes?

I am generating a 2048-bit safe prime for a Diffie-Hellman-type key, p such that p and (p-1)/2 are both prime. How few iterations of Rabin-Miller can I use on both p and (p-1)/2 and still be confident of a cryptographically strong key? In the…
jnm2
  • 7,960
  • 5
  • 61
  • 99
19
votes
11 answers

Generate a list of primes up to a certain number

I'm trying to generate a list of primes below 1 billion. I'm trying this, but this kind of structure is pretty shitty. Any suggestions? a <- 1:1000000000 d <- 0 b <- for (i in a) {for (j in 1:i) {if (i %% j !=0) {d <- c(d,i)}}}
Thomas
  • 847
  • 4
  • 12
  • 21
19
votes
4 answers

Enumerate factors of a number directly in ascending order without sorting?

Is there an efficient algorithm to enumerate the factors of a number n, in ascending order, without sorting? By “efficient” I mean: The algorithm avoids a brute-force search for divisors by starting with the prime-power factorization of n. The…
Todd Lehman
  • 2,880
  • 1
  • 26
  • 32
18
votes
6 answers

Determining if a given number is a prime in haskell

So I have devised the following function for seeing if a given number is a prime in Haskell (it assumes the first prime is 2): isPrime k = length [ x | x <- [2..k], k `mod` x == 0] == 1 it has the obvious pitfall of continuing the evaluation even…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
18
votes
4 answers

How to optimize this Haskell code summing up the primes in sublinear time?

Problem 10 from Project Euler is to find the sum of all the primes below given n. I solved it simply by summing up the primes generated by the sieve of Eratosthenes. Then I came across much more efficient solution by Lucy_Hedgehog (sub-linear!). For…
Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
18
votes
21 answers

How can I find the prime factors of an integer in JavaScript?

I was trying to find the prime factors of a number, recorded below as 'integer' using a for loop in javascript. I can't seem to get it working and I'm not sure whether it's my JavaScript or my calculation logic. //integer is the value for which we…
John the User
  • 620
  • 2
  • 5
  • 13
17
votes
3 answers

Why is this Haskell code snippet not infinitely recursive?

To help me learn Haskell, I am working through the problems on Project Euler. After solving each problem, I check my solution against the Haskell wiki in an attempt to learn better coding practices. Here is the solution to problem 3: primes = 2 :…
Matthew
  • 28,056
  • 26
  • 104
  • 170