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
27
votes
5 answers

Lazy List of Prime Numbers

How would one implement a list of prime numbers in Haskell so that they could be retrieved lazily? I am new to Haskell, and would like to learn about practical uses of the lazy evaluation functionality.
Mantas Vidutis
  • 16,376
  • 20
  • 76
  • 92
26
votes
11 answers

How TDD works when there can be millions of test cases for a production functionality?

In TDD, you pick a test case and implement that test case then you write enough production code so that the test passes, refactor the codes and again you pick a new test case and the cycle continues. The problem I have with this process is that TDD…
The Light
  • 26,341
  • 62
  • 176
  • 258
26
votes
3 answers

Why setting HashTable's length to a Prime Number is a good practice?

I was going through Eric Lippert's latest Blog post for Guidelines and rules for GetHashCode when i hit this para: We could be even more clever here; just as a List resizes itself when it gets full, the bucket set could resize itself as well, to…
Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
26
votes
3 answers

Fastest primality test

Could you suggest a fast, deterministic method that is usable in practice, for testing if a large number is prime or not? Also, I would like to know how to use non-deterministic primality tests correctly. For example, if I'm using such a method,…
user500944
26
votes
2 answers

Generating all factors of a number given its prime factorization

If you already have the prime factorization of a number, what is the easiest way to get the set of all factors of that number? I know I could just loop from 2 to sqrt(n) and find all divisible numbers, but that seems inefficient since we already…
dimo414
  • 47,227
  • 18
  • 148
  • 244
26
votes
15 answers

How do I generate the first n prime numbers?

I am learning Ruby and doing some math stuff. One of the things I want to do is generate prime numbers. I want to generate the first ten prime numbers and the first ten only. I have no problem testing a number to see if it is a prime number or…
Tony Petley
  • 359
  • 1
  • 3
  • 13
25
votes
4 answers

How can I convert an absolutely massive number to a string in a reasonable amount of time?

This is quite an odd problem I know, but I'm trying to get a copy of the current largest prime number in a file. Getting the number in integer form is fairly easy. I just run this. prime = 2**74207281 - 1 It takes about half a second and it works…
Daffy
  • 841
  • 9
  • 23
25
votes
1 answer

Why do programming contests want answers modulo some large prime?

I have been testing the waters of competitive programming and I have already seen this statement mentioned a lot of times: Print the result modulo 109 + 7 Now I can figure out that this is some way of preventing overflow of digits when dealing…
Pawan
  • 1,480
  • 2
  • 18
  • 27
25
votes
8 answers

Sieve of Eratosthenes algorithm in JavaScript running endless for large number

I have been trying to write Sieve of Eratosthenes algorithm in JavaScript. Basically I just literally followed the steps below: Create a list of consecutive integers from 2 to (n-1) Let first prime number p equal 2 Starting from p, count up in…
Bao
  • 617
  • 2
  • 10
  • 17
24
votes
11 answers

Is there an upper limit on .txt file size?

As a Christmas gift I have written a small program in Java to calculate primes. My intention was to leave it on all night, calculating the next prime and writing it to a .txt file. In the morning I would kill the program and take the .txt file to my…
Ziggy
  • 21,845
  • 28
  • 75
  • 104
24
votes
7 answers

Ruby isPrime Method

('1' * N) !~ /^1?$|^(11+?)\1+$/ On the net, I found this piece of Ruby code that works for N >= 0 that determines whether or not N is a prime. From what I can tell, it looks like play with regex but I have no idea how it works. Could someone tell…
Felix F
23
votes
2 answers

Proving the primality of strong probable primes

Using the probabilistic version of the Miller-Rabin test, I have generated a list of medium-large (200-300 digit) probable primes. But probable ain't good enough! I need to know these numbers are prime. Is there a library -- preferably wrapped or…
senderle
  • 145,869
  • 36
  • 209
  • 233
23
votes
3 answers

Confused on Miller-Rabin

As an exercise for myself, I'm implementing the Miller-Rabin test. (Working through SICP). I understand Fermat's little theorem and was able to successfully implement that. The part that I'm getting tripped up on in the Miller-Rabin test is this "1…
afkbowflexin
  • 4,029
  • 2
  • 37
  • 61
23
votes
6 answers

Does a library for prime-related functions exist for Python?

I've just implemented the Miller-Rabin-Test and a simple function for factorizing numbers. Both could be done better and at least the Miller-Rabin-Test is well-known. So could you please tell me if a Python-Library, that implements such common…
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
23
votes
8 answers

Efficient storage of prime numbers

For a library, I need to store the first primes numbers up to a limit L. This collection must have a O(1) lookup time (to check whether a number is prime or not) and it must be easy, given a number, to find the next prime number (assuming it is…
Samuel Tardieu
  • 2,071
  • 1
  • 14
  • 17