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
70
votes
6 answers

What is a sensible prime for hashcode calculation?

Eclipse 3.5 has a very nice feature to generate Java hashCode() functions. It would generate for example (slightly shortened:) class HashTest { int i; int j; public int hashCode() { final int prime = 31; int…
Dr. Hans-Peter Störr
  • 25,298
  • 30
  • 102
  • 139
68
votes
5 answers

Is a list (potentially) divisible by another?

Problem Say you have two lists A = [a_1, a_2, ..., a_n] and B = [b_1, b_2, ..., b_n] of integers. We say A is potentially-divisible by B if there is a permutation of B that makes a_i divisible by b_i for all i. The problem is then: is it possible to…
McGuire
  • 1,192
  • 2
  • 11
  • 20
67
votes
30 answers

isPrime Function for Python Language

So I was able to solve this problem with a little bit of help from the internet and this is what I got: def isPrime(n): for i in range(2,int(n**0.5)+1): if n%i==0: return False return True But my question really…
66
votes
0 answers

Checking if a number is prime in Python

I have written the following code, which should check if the entered number is a prime number or not, but there is an issue I couldn't get through: def main(): n = input("Please enter a number:") is_prime(n) def is_prime(a): x = True …
steve
  • 685
  • 1
  • 6
  • 4
66
votes
31 answers

Check if number is prime number

I would just like to ask if this is a correct way of checking if number is prime or not? because I read that 0 and 1 are NOT a prime number. int num1; Console.WriteLine("Accept number:"); num1 = Convert.ToInt32(Console.ReadLine()); if (num1 ==…
user1954418
  • 963
  • 7
  • 21
  • 29
65
votes
28 answers

Simple prime number generator in Python

Could someone please tell me what I'm doing wrong with this code? It is just printing 'count' anyway. I just want a very simple prime generator (nothing fancy). import math def main(): count = 3 one = 1 while one == 1: for x…
marc lincoln
  • 1,551
  • 4
  • 21
  • 25
64
votes
31 answers

Most efficient code for the first 10000 prime numbers?

I want to print the first 10000 prime numbers. Can anyone give me the most efficient code for this? Clarifications: It does not matter if your code is inefficient for n >10000. The size of the code does not matter. You cannot just hard code the…
Niyaz
  • 53,943
  • 55
  • 151
  • 182
63
votes
3 answers

Reason for the number 5381 in the DJB hash function?

Can anyone tell me why the number 5381 is used in the DJB hash function? The DJB hash function is defined as: h 0 = 5381 h i = 33h i - 1 + s i Here's a C implementation: unsigned int DJBHash(char* str, unsigned int len) { unsigned int hash =…
viji
  • 2,706
  • 5
  • 28
  • 34
61
votes
40 answers

How to find prime numbers between 0 - 100?

In Javascript how would i find prime numbers between 0 - 100? i have thought about it, and i am not sure how to find them. i thought about doing x % x but i found the obvious problem with that. this is what i have so far: but unfortunately it is the…
user1599757
  • 653
  • 1
  • 6
  • 3
56
votes
9 answers

Why is the size 127 (prime) better than 128 for a hash-table?

Supposing simple uniform hashing, that being, any given value is equally like to hash into any of the slots of the hash. Why is it better to use a table of size 127 and not 128? I really don't understand what's the problem with the power of 2…
Clash
  • 4,896
  • 11
  • 47
  • 67
55
votes
15 answers

What would be the fastest method to test for primality in Java?

I am trying to find the fastest way to check whether a given number is prime or not (in Java). Below are several primality testing methods I came up with. Is there any better way than the second implementation(isPrime2)? public class Prime { …
Anantha Kumaran
  • 10,101
  • 8
  • 33
  • 36
51
votes
2 answers

How many prime numbers are there (available for RSA encryption)?

Am I mistaken in thinking that the security of RSA encryption, in general, is limited by the amount of known prime numbers? To crack (or create) a private key, one has to combine the right pair of prime numbers. Is it impossible to publish a list of…
pinhead
  • 1,017
  • 2
  • 12
  • 16
49
votes
16 answers

Fast Prime Number Generation in Clojure

I've been working on solving Project Euler problems in Clojure to get better, and I've already run into prime number generation a couple of times. My problem is that it is just taking way too long. I was hoping someone could help me find an…
MBCook
  • 14,424
  • 7
  • 37
  • 41
47
votes
11 answers

Calculating and printing the nth prime number

I am trying to calculate prime numbers, which I've already done. But I want to calculate and print ONLY the nth prime number (User input), while calculating the rest (They won't be printed) only the nth prime number will be printed. Here's what I've…
Erick
  • 2,488
  • 6
  • 29
  • 43
45
votes
13 answers

nᵗʰ ugly number

Numbers whose only prime factors are 2, 3, or 5 are called ugly numbers. Example: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... 1 can be considered as 2^0. I am working on finding nth ugly number. Note that these numbers are extremely sparsely…
Anil Katti
  • 1,313
  • 1
  • 14
  • 26