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

prime number summing still slow after using sieve

I had a go at a project euler coding challenge below, the answer given by the code is correct but I do not understand why its taking nearly a minute to run. It was finishing with similar times prior to using a sieve. Others users are reporting times…
sprocket12
  • 5,368
  • 18
  • 64
  • 133
1
vote
3 answers

Python - Project Euler #35 -- what is wrong with this approach?

Problem 35 of Project Euler is as so: The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79,…
ggordon
  • 259
  • 1
  • 3
  • 16
1
vote
2 answers

Python - Removing primes that contain an even number from a list of primes

I want to write a programme which removes all the primes which contain an even digit from a list of primes. Can anyone explain why this code returns the correct result if the limit = 200, but returns an error if the limit = 300? def…
ggordon
  • 259
  • 1
  • 3
  • 16
1
vote
2 answers

Why does this Largest Prime Factor of a given number in python algorithm work?

I am unable to understand the following block of code which I came across this site itself. It creates a function to find out the largest prime factor of a given number. It is given below: def prime_factors(n): """Returns all the prime factors of a…
Aradhye Agarwal
  • 439
  • 1
  • 4
  • 9
1
vote
2 answers

programming haskell code: List of primes

My task is to create a list of primes but only with the following information: -The list has to be infinite -I have to check up primes for n > 1 -If there is a variable 2 <= k < n-2, which divides n,then it is no prime, if it does not divide n, it…
Janet2001
  • 55
  • 7
1
vote
1 answer

Haskell Vector performance woes

I wrote the classic prime number sieve algorithm in C which pretty much instantly generates all the primes smaller than 1,000,000 on my machine (I didn't do anything to optimize it). In Haskell I use a Vector of Ints, and generate an update list…
user3692497
1
vote
1 answer

Complexity of print first n prime number

In an interview I was given this questions: Write a function to print first n prime numbers The function looks like this: Live on ideone while (true) { boolean isPrime = true; for (int divisor = 2; divisor <= (int)(Math.sqrt(number));…
giò
  • 3,402
  • 7
  • 27
  • 54
1
vote
1 answer

Numerical Error

I wrote a quick sieve to test if a number is prime or not. I have two questions: 1) I tested a 200 digit prime, and it incorrectly says it wasn't prime. I believe this is down to floating point error (or something like that). How can I make this…
1
vote
1 answer

what algorithm is used by GMP for prime field inversion (mpz_invert)?

What is the algorithm used by GMP to invert a element in a finite field?
Rick
  • 361
  • 5
  • 17
1
vote
3 answers

Why is all() slower than using for-else & break?

I've been fooling around with problem 7 from project Euler and I noticed that two of my prime finding methods are very similar but run at very different speeds. #!/usr/bin/env python3 import timeit def lazySieve (num_primes): if num_primes ==…
obivain222
  • 21
  • 4
1
vote
3 answers

Seemingly undefined behavior with realloc in C prime generator (program posted, runnable)

I wrote a program to find prime numbers and I use realloc() to change the size of the '*primes' block. When I put any number of bits greater than 6, I find that the first two values seem like undefined behavior and I cannot for the life of me…
n4alpaca
  • 13
  • 2
1
vote
1 answer

Prime-testing program not working

#include "stdafx.h" #include "math.h" #include #include using namespace std; int main () { int x; cout << "Enter a number." << endl; cin >> x; int y = 1; int i = 0; while (i == 0 && y < sqrtf(x)) { …
Mathime
  • 1,380
  • 1
  • 11
  • 11
1
vote
2 answers

if vs while loop in projecteuler-3 prime factor division

Today I practiced for my coding test using projecteulers problems. While doing the prime factor division I stumbled across something I found weird. Here is the relevant code (res is an ArrayList): for (int x = 2; x <= n; x++){ if (n % x…
1
vote
2 answers

Generate First n Primes (Haskell)

I have this function: generatePrimes :: Integral a => a -> [a] generatePrimes n = [i | i <- [2..], isPrime i] I am trying to get the first n primes. I know that I can call the function in main by using the take function (and get the first n…
user3247014
  • 11
  • 1
  • 2
1
vote
2 answers

Prime number algorithm not working

package coding; public class Prob7 { public static void main(String[] args) { long flag=0; long counter=0; long prime; for(prime=2;;prime++){ System.out.println(counter); flag=0; long check=2; …
Siddharth Venu
  • 1,318
  • 13
  • 26