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
17
votes
2 answers

Python OverflowError: cannot fit 'long' into an index=sized integer

I want to generate two really large prime numbers using an algorithm I found online and changed slightly. I get this error on line 5: Python OverflowError: cannot fit 'long' into an index=sized integer My code: import math def atkin(end): if…
user583507
17
votes
8 answers

Prime factors in Haskell

I'm new to Haskell. How to generate a list of lists which contains prime factors of next integers? Currently, I only know how to generate prime numbers: primes = map head $ iterate (\(x:xs) -> [y | y<-xs, y `mod` x /= 0 ]) [2..]
Chris
  • 652
  • 1
  • 7
  • 22
17
votes
16 answers

Project Euler Question 3 Help

I'm trying to work through Project Euler and I'm hitting a barrier on problem 03. I have an algorithm that works for smaller numbers, but problem 3 uses a very, very large number. Problem 03: The prime factors of 13195 are 5, 7, 13 and 29. What is…
Ryan Rodemoyer
  • 5,548
  • 12
  • 44
  • 54
17
votes
7 answers

Is there a way to find the approximate value of the nth prime?

Is there a function which will return the approximate value of the n th prime? I think this would be something like an approximate inverse prime counting function. For example, if I gave this function 25 it would return a number around 100, or if I…
David Johnstone
  • 24,300
  • 14
  • 68
  • 71
16
votes
4 answers

how to generate numbers given their prime factors, but with unknown exponents?

Possible Duplicates: nth ugly number Find the Kth least number for expression (2^x)*(3^y)*(5^z) I'm wondering how to solve this problem in a fast and elegant way: We define "ugly" every number n which can be written in the form: 2^x * 3^y *…
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
16
votes
7 answers

Prime Factors In C#

I want to create a program in C# 2005 which calculates prime factors of a given input. i want to use the basic and simplest things, no need to create a method for it nor array things etc. just simple modulus. is there any code which fulfills what i…
Aliza
  • 161
  • 1
  • 1
  • 4
16
votes
22 answers

Printing prime numbers from 1 through 100

This c++ code prints out the following prime numbers: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97. But I don't think that's the way my book wants it to be written. It mentions something about square root of a number. So I…
Sahat Yalkabov
  • 32,654
  • 43
  • 110
  • 175
16
votes
7 answers

Calculating Extremely Large Powers of 2

I have made a program in Java that calculates powers of two, but it seems very inefficient. For smaller powers (2^4000, say), it does it in less than a second. However, I am looking at calculating 2^43112609, which is one greater than the largest…
antiquekid3
  • 163
  • 1
  • 1
  • 7
16
votes
1 answer

F# vs C# performance for prime number generator

I have noticed that seemingly equivalent code in F# and C# do not perform the same. The F# is slower by the order of magnitude. As an example I am providing my code which generates prime numbers/gives nth prime number in F# and C#. My F# code…
16
votes
2 answers

Improving pure Python prime sieve by recurrence formula

I am trying to optimize further the champion solution in prime number thread by taking out the complex formula for sub-list length. len() of the same subsequence is too slow as len is expensive and generating the subsequence is expensive. This looks…
Tony Veijalainen
  • 5,447
  • 23
  • 31
16
votes
18 answers

Prime number calculation fun

We're having a bit of fun here at work. It all started with one of the guys setting up a Hackintosh and we were wondering whether it was faster than a Windows Box of (nearly) same specs that we have. So we decided to write a little test for it. Just…
Feet
  • 2,567
  • 3
  • 22
  • 29
16
votes
11 answers

Prime number function in R

I am trying to create a function to test if a given integer is a prime number, I tried using the following: tpn <- function(prime.num){ if(prime.num==2){ print("PRIME") } else { if(prime.num%%(2:(prime.num-1))!=0){ …
dsmoore
  • 163
  • 1
  • 1
  • 5
16
votes
6 answers

Random prime number

How do I quickly generate a random prime number, that is for sure 1024 bit long?
oldhomemovie
  • 14,621
  • 13
  • 64
  • 99
16
votes
3 answers

Calculating prime numbers in Scala: how does this code work?

So I've spent hours trying to work out exactly how this code produces prime numbers. lazy val ps: Stream[Int] = 2 #:: Stream.from(3).filter(i => ps.takeWhile{j => j * j <= i}.forall{ k => i % k > 0}); I've used a number of printlns etc, but…
Alan Hollis
  • 1,292
  • 3
  • 14
  • 29
16
votes
7 answers

Does this prime function actually work?

Since I'm starting to get the hang of Python, I'm starting to test my newly acquired Python skills on some problems on projecteuler.net. Anyways, at some point, I ended up making a function for getting a list of all primes up until a number…
phaz
  • 872
  • 9
  • 23