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
1
vote
2 answers

Recursion in a prime generator

I'm making a prime generator, and to make it more efficient, i'm trying to only test numbers against primes that I've already found rather than all numbers < sqrt of the number being tested. I'm trying to get a to be my list of primes, but i'm not…
Thomas
  • 847
  • 4
  • 12
  • 21
1
vote
2 answers

Python finding Prime Numbers between any two numbers

I'm trying to find prime numbers between any two random numbers. Firstly, I have written the code like: m,n = map(int, raw_input().split()) for i in range(m, n+1): for j in range(2, i): if i%j == 0: break else: …
Ashvini Maurya
  • 493
  • 1
  • 7
  • 13
1
vote
1 answer

Sum of Primes using Sieve of Eratosthenes can't find bug

I'm working in JavaScript and this is a bit confusing because the code is returning the correct sum of primes. It is working with larger numbers. There is a bug where for 977 it returns the sum of primes for 976, which is 72179, instead of the sum…
Daniel Semel
  • 175
  • 1
  • 1
  • 13
1
vote
4 answers

How to keep a Swift loop running indefinitely (forever)?

I have written a function which currently displays all of the prime numbers under 1000. I can keep making 1000 bigger to generate more numbers, but I do not know how to make it keep going on forever once run. func generatePrimes() { let numbers…
Gabe Garboden
  • 33
  • 1
  • 4
1
vote
2 answers

Iterating again, prime factors loop, python

I recently made a function in python that will give me all the factors of any number you choose in python. I would like to modify it now to only show the prime factors. The function I have currently is: def prime_factors(n): L = [] i = range(1,…
SignalProcessed
  • 371
  • 4
  • 16
1
vote
0 answers

Logical Output Error in an algorithm using VHDL

i am trying to do an algorithm that verify prime numbers. To do this i have to make a circuit using RTL Design method, i am using the algorithm below to get the prime number: int prime (int x) { int i, div; div = 0; for (i = 1; i <= x; i++){ …
Mutante
  • 278
  • 5
  • 18
1
vote
4 answers

Improving trial division primality test using 6k+/-1 rule

I was going through the basics of trial division primality test, and hence, implementing it in code. The performance of the algorithm can be increased using many tricks like: 1) running the trial division only up to square-root(n) 2) trading memory…
Tanmay Garg
  • 801
  • 11
  • 20
1
vote
2 answers

Selecting parameters for string hashing

I was recently reading an article on string hashing. We can hash a string by converting a string into a polynomial. H(s1s2s3 ...sn) = (s1 + s2*p + s3*(p^2) + ··· + sn*(p^n−1)) mod M. What are the constraints on p and M so that the probability of…
Shivam Mitra
  • 1,040
  • 3
  • 17
  • 33
1
vote
1 answer

Finding Mersenne primes with Lucas–Lehmer primality test

I want to implement a Stream in Scala for finding Mersenne primes with the Lucas-Lehmer primality test. I already have: object Main { //Mersenne Numbers: def msrn():Stream[BigInt] = 7#::msrn.map(_*2+1) def s():Stream[BigInt] = 14 #:: s.map(n…
nlsmrg
  • 13
  • 5
1
vote
1 answer

Number of semi-primes between two numbers

I am doing a solution to a coding problem, and I tweaked some existing code to be able to figure out how many semi-primes exist up till and including a certain number. However, I am stuck at the part where I want to count the number of unique…
Dhruv Ghulati
  • 2,976
  • 3
  • 35
  • 51
1
vote
1 answer

Writing an algorithm to generate prime numbers in Python within a given range

I see that there are many different ways to generate prime numbers. My code is very long and redundant but I know it can definitely be condensed and made less repetitive with a few changes and I was hoping I could be pointed in the right direction.…
Chris95
  • 75
  • 1
  • 10
1
vote
1 answer

Modified version of Miller-Rabin to test deterministic primality?

The Miller-Rabin test uses k random integers to test for primality. According to CLRS, 3rd Edition, Page 971: Theorem 31.38 If n is an odd composite number, then the number of witnesses to the compositeness of n is at least (n - 1)/2. Then why…
Existent
  • 697
  • 1
  • 7
  • 14
1
vote
1 answer

pow() function in c produces truncation(or rounded) error?

Hi all:I am using pow() function in to calulate powers of primes.But some outputs don't show as expected. For example,when I feed 200 as input,I wish to print out all primes and their powers that do not exceed 200. Most of the numbers are fine,but…
vagrantlike
  • 31
  • 1
  • 5
1
vote
4 answers

Prime Number Generator in C

This is the Link to the problem: http://www.spoj.com/problems/PRIME1/ Basically we get two limits and we have to print out all the primes between them... Here is my Code (Language == C) : #include void IsPrime(int test){ for(int i=…
Sudocode
  • 69
  • 2
  • 10
1
vote
1 answer

Why does this JavaScript function work?

Much apologies for the vague title, but I need to elaborate. Here is the code in question, which I read on http://ariya.ofilabs.com/2013/07/prime-numbers-factorial-and-fibonacci-series-with-javascript-array.html: function isPrime(i) { return (i >…
pward
  • 1,049
  • 1
  • 8
  • 17