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
1 answer

Why can't Clojure's async library handle the Go prime sieve?

To try out the async library in Clojure, I translated the prime sieve example from Go. Running in the REPL, it successfully printed out the prime numbers up to 227 and then stopped. I hit Ctrl-C and tried running it again but it wouldn't print out…
ijt
  • 3,505
  • 1
  • 29
  • 40
1
vote
1 answer

Sieve of Eratosthenes variant

I want to do a sieve that doesn't take advantage of the obvious math hacks. I want to brute force it. My algorithm is conceived with the notion that the sieve does a lot of checking for what are not prime numbers and just returning the result of the…
nobody
  • 7,803
  • 11
  • 56
  • 91
1
vote
0 answers

Converting a Ruby script to a PHP script

I have to make a PHP script that calculates prime numbers. I never made something in PHP before.. (only in Ruby and JavaScript) I already tried to convert the script to PHP (see below) can someone explain me why it doesn't print down the first two…
Stijn Westerhof
  • 233
  • 3
  • 13
1
vote
1 answer

Parallel functions return different results in C#

I have these codes in my windows form C# application: private void button7_Click(object sender, EventArgs e) { ThreadStart starter = delegate { thread_func(2, 1000000); }; thread1_thread = new Thread(starter); starter = delegate {…
ACE
  • 23
  • 6
1
vote
2 answers

Can't understand function behavior - isprime()

I have constructed two functions. The first (not important but related since it is called in the second one) tells whether a number is a prime: def is_prime(i): if i == 1: print("prime") if i == 2: print("not prime") for…
Navy Seal
  • 125
  • 1
  • 14
1
vote
1 answer

Prime checking using BigInteger (argument mismatch)

I'm trying to make an application to check if a specific number is a prime. Since the number is bigger than value of int or long, I had to use BigInteger, but I've slim knowledge about it so I'm hitting the wall. What I'm doing is checking if n is…
Jerry Dark
  • 11
  • 1
1
vote
1 answer

Performing Division on Individual Elements Of A List

I am trying to write a program that finds prime numbers. prime = [2] for k in range(3,100) if k%prime != 0 prime.append(k) print(prime) When I run the program I get the error: TypeError: unsupported operand type(s) for %: 'int' and…
1
vote
2 answers

How can I make this prime finder operate in parallel

I know prime finding is well studied, and there are a lot of different implementations. My question is, using the provided method (code sample), how can I go about breaking up the work? The machine it will be running on has 4 quad core…
Nick Larsen
  • 18,631
  • 6
  • 67
  • 96
1
vote
1 answer

coffeescript prime number generation

I am trying to generate prime numbers like this: generatePrimeNumbersBelowN = (n) -> for i in [2..n-1] isPrime = true for j in [2..i-1] isPrime = false if i % j == 0 break if not isPrime console.log(i, "is Prime Number.")…
1
vote
6 answers

Printing the Largest Prime Factor of a Composite Number in C

I was solving a puzzle, where im required to find the largest Prime Factor of a composite number entered by the user. I thought of something and have tried it out, but it doesn't manage to detect the largest prime factor amongst the factors of the…
Arun
  • 11
  • 1
  • 3
1
vote
1 answer

ruby - Faster prime gap search

I'm working on creating a method, gap(g, m, n). All 3 parameters are integers. gap finds the first 2 consecutive prime numbers, between m and n, that have a difference of g. For example, gap(2, 1, 10) => [3, 5] In the range from m to n, 1..10, the…
John Chen
  • 199
  • 7
1
vote
0 answers

wheel sieve - slow reconstruction from bitsets

I'm trying to get my feet wet with parallel processing by implementing a wheel sieve for primes, the order of operation roughly as follows, given some upper bound N, construct 8 different spokes of the form [p, p + 30, p+ 60, ..., p + 30n] for p…
HexedAgain
  • 1,011
  • 10
  • 21
1
vote
4 answers

Optimize Speed of Prime Number Sieve (Java)

I'm working on a method in Java which creates boolean array isPrime: boolean[] isPrime; in which prime numbers are labeled with 'true' and the rest with 'false'. While I'm at it I'd also like to count the number of Primes found: int…
Mark
  • 69
  • 1
  • 1
  • 8
1
vote
4 answers

Javascript Prime Number Function Console Issue

Apologies if this has been covered somewhere. I've searched through a lot of prime number function answers on here but I cannot seem to find the answer to my issue. I have cobbled together a function to check if a number is prime, and then I'm…
Frances
  • 11
  • 2
1
vote
1 answer

Python read from file every time or store values in list?

I am currently writing a python script to generate every prime by brute force. I currently have a >5Mb file containing prime numbers and as the script runs it appends any new prime it finds so the file will keep getting bigger. Every time the script…
DustinPianalto
  • 373
  • 1
  • 8