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
44
votes
11 answers

Speed up bitstring/bit operations in Python?

I wrote a prime number generator using Sieve of Eratosthenes and Python 3.1. The code runs correctly and gracefully at 0.32 seconds on ideone.com to generate prime numbers up to 1,000,000. # from bitstring import BitString def…
43
votes
8 answers

Finding a prime number after a given number

How can I find the least prime number greater than a given number? For example, given 4, I need 5; given 7, I need 11. I would like to know some ideas on best algorithms to do this. One method that I thought of was generate primes numbers through…
avd
  • 13,993
  • 32
  • 78
  • 99
41
votes
6 answers

Segmented Sieve of Eratosthenes?

It's easy enough to make a simple sieve: for (int i=2; i<=N; i++){ if (sieve[i]==0){ cout << i << " is prime" << endl; for (int j = i; j<=N; j+=i){ sieve[j]=1; } } cout << i << " has " << sieve[i] << "…
John Smith
  • 11,678
  • 17
  • 46
  • 51
39
votes
3 answers

AKS Primes algorithm in Python

A few years ago, it was proven that PRIMES is in P. Are there any algorithms implementing their primality test in Python? I wanted to run some benchmarks with a naive generator and see for myself how fast it is. I'd implement it myself, but I don't…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
39
votes
2 answers

Recursive function causing a stack overflow

I am trying to write a simple sieve function to calculate prime numbers in clojure. I've seen this question about writing an efficient sieve function, but I am not to that point yet. Right now I am just trying to write a very simple (and slow)…
dbyrne
  • 59,111
  • 13
  • 86
  • 103
35
votes
2 answers

Is Swift really slow at dealing with numbers?

As I was playing around with a swift tutorial, I started to write a custom isPrime method to check if a given Int is prime or not. After writing it I realized it was working properly but found it a bit slow to perform isPrime on some quite large…
apouche
  • 9,703
  • 6
  • 40
  • 45
35
votes
36 answers

Print series of prime numbers in python

I was having issues in printing a series of prime numbers from one to hundred. I can't figure our what's wrong with my code. Here's what I wrote; it prints all the odd numbers instead of primes: for num in range(1, 101): for i in range(2, num): …
user1546721
  • 363
  • 1
  • 4
  • 6
34
votes
10 answers

Fastest algorithm for primality test

I need to test primality on intervals between numbers which are really big (in the range of long long), so i need some fast algorithm for checking if a number is prime or not. Please suggest your ideas.
dada
  • 1,095
  • 4
  • 13
  • 14
33
votes
14 answers

Prime number check acts strange

I have been trying to write a program that will take an imputed number, and check and see if it is a prime number. The code that I have made so far works perfectly if the number is in fact a prime number. If the number is not a prime number it…
Chris
  • 401
  • 2
  • 7
  • 9
33
votes
28 answers

Program to find prime numbers

I want to find the prime number between 0 and a long variable but I am not able to get any output. The program is using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication16 { class…
sandy101
  • 3,395
  • 3
  • 23
  • 19
32
votes
1 answer

Recursion with Func

Is it possible to do recursion with an Func delegate? I have the following, which doesn't compile because the name of the Func isn't in scope... Func, IEnumerable> GeneratePrimesRecursively = (number, upperBound,…
t3rse
  • 10,024
  • 11
  • 57
  • 84
32
votes
1 answer

What does "e is 65537 (0x10001)" mean?

I want to know what the output e is 65537 (0x10001) means. It happens during the RSA Key Generation using openssl genrsa. I know that the dots mean that the number has passed a probe division and the plus is printed out after it passed a miller…
jsbeckr
  • 1,183
  • 1
  • 10
  • 24
29
votes
16 answers

Is there a simple algorithm that can determine if X is prime?

I have been trying to work my way through Project Euler, and have noticed a handful of problems ask for you to determine a prime number as part of it. I know I can just divide x by 2, 3, 4, 5, ..., square root of X and if I get to the square root,…
Pulsehead
  • 5,050
  • 9
  • 33
  • 37
28
votes
10 answers

Algorithm to find Lucky Numbers

I came across this question.A number is called lucky if the sum of its digits, as well as the sum of the squares of its digits is a prime number. How many numbers between A and B are lucky? 1 <= A <= B <= 1018. I tried this. First I generated all…
vgeta
  • 507
  • 1
  • 9
  • 15
28
votes
9 answers

How to calculate the number of coprime subsets of the set {1,2,3,..,n}

I am solving this task (problem I). The statement is: How many subsets of the set {1, 2, 3, ..., n} are coprime? A set of integers is called coprime if every two of its elements are coprime. Two integers are coprime if their greatest common divisor…
Love Paper
  • 471
  • 1
  • 4
  • 15