primes

Finding prime numbers with the Sieve of Eratosthenes (Originally: Is there a better way to prepare this array?)

Note: Version 2, below, uses the Sieve of Eratosthenes. There are several answers that helped with what I originally asked. I have chosen the Sieve of Eratosthenes method, implemented it, and changed the question title and tags appropriately. Thanks to everyone who helped! Introduction I wrote this fancy little method that generates...

How can I test for primality?

I am writing a little library with some prime number related methods. As I've done the groundwork (aka working methods) and now I'm looking for some optimization. Ofcourse the internet is an excellent place to do so. I've, however, stumbled upon a rounding problem and I was wondering how to solve this. In the loop I use to test a numbe...

fixnum and prime numbers in ruby

Before I set about to writing this myself, has anyone seen a ruby implementation of the following behavior? puts 7.nextprime(); #=> 11 puts 7.previousprime(); #=> 5 puts 7.isprime(); #=> true Obviously this kind of thing would be ugly for large numbers but for integers never exceeding a few thousand (the common instance fo...

Shortest-code for prime-calculation

The newspaper for the Computer Science-line at my school (called readme, is's norwegian, page 19) had a fun competition: Write the shortest possible Java-code for this problem: Take in an integer (as a string in the first entry of a string array, since the Java main method only takes a string array) as an argument, and write out fir...

prime numbers c#

Hi I'm new to C#. And I would like to program something like, displaying the prime numbers in a listbox if user will input any integer in the textbox. (that means, if they write 10, it will display the prime numbers from 0-10, or 20 from 0-20, etc). What should I consider first, before I do the programming? I know there are many example...

How can I find prime numbers through bit operations in C++?

How can I find prime numbers through bit operations in C++? ...

n-th prime number problem, need to speed it up a bit

There is simple cipher that translates number to series of . ( ) In order to encrypt a number (0 .. 2147483647) to this representation, I (think I) need: prime factorization for given p (p is Prime), order sequence of p (ie. PrimeOrd(2) == 0, PrimeOrd(227) == 49) Some examples 0 . 6 (()()) 1 () ...

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 efficient way to do this in a Clojure-y way. When I fist did this, I brute-forced it. That was ea...

C# Prime Generator, Maxxing out Bit Array

(C#, prime generator) Heres some code a friend and I were poking around on: public List<int> GetListToTop(int top) { top++; List<int> result = new List<int>(); BitArray primes = new BitArray(top / 2); int root = (int)Math.Sqrt(top); for (int i = 3, count = 3; i <= root; i += 2, count++) { int ...

Sieve of Atkin explanation

I am doing a project at the moment and I need an efficient method for calculating prime numbers. I have used the sieve of Eratosthenes but, I have been searching around and have found that the sieve of Atkin is a more efficient method. I have found it difficult to find an explanation (that I have been able to understand!) of this method....

Calculating phi(k) for 1<k<N

Given a large N, I need to iterate through all phi(k) such that 1 < k < N quickly. Since the values of N will be around 1012, it is important that the memory complexity is sub O(n). Is it possible? If so, how? ...

Efficient storage of prime numbers

For a library, I need to store the first primes numbers up to a limit L. This collection must have a O(1) lookup time (to check whether a number is prime or not) and it must be easy, given a number, to find the next prime number (assuming it is smaller than L). Given that L is fixed, an Eratostene sieve to generate the list is fine. Rig...

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 gave this function 1000 it would return a number around 8000. I don't care if the number r...

Most elegant way to generate prime numbers

What is the most elegant way to implement this function: ArrayList generatePrimes(int n) This function generates the first n primes (edit: where n>1), so generatePrimes(5) will return an ArrayList with {2, 3, 5, 7, 11}. (I'm doing this in C#, but I'm happy with a Java implementation - or any other similar language for that matter (so ...

How do I break a for loop in PHP if conditions are met?

Hi, I'm diligently plugging away at some code that checks for divisibility (yes, it's to generate primes) and I want to know how to stop a for... loop if the condition is met once. Code like this: $delete = array(); foreach ($testarray as $v) { for ($b=2; $b<$v; $b++) { if ($v%$b == 0) { $delete []= $v; } } So $testarray is int...

F# using sequence cache correctly

I'm trying to use Seq.cache with a function that I made that returns a sequence of primes up to a number N excluding the number 1. I'm having trouble figuring out how to keep the cached sequence in scope but still use it in my definition. let rec primesNot1 n = {2 .. n} |> Seq.filter (fun i -> (primesNot1 (i / 2) |> S...

Working with Java's BigInteger probable primes

I want to print all the prime numbers between two numbers. This is my code: package sphere; import java.math.BigInteger; import java.io.*; class PrimeTest2 { public static void main(String args[]) throws java.lang.Exception { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); String s = r.read...

Iterating over a data structure with 51 million primes quickly.

What's the best data structure (in java) for the task of loading 51 million primes and then iterating over them? I need to know, for example, the primes that are between 1000000000 and that same number minus 100000. ...

Learning F# - printing prime numbers

Yesterday I started looking at F# during some spare time. I thought I would start with the standard problem of printing out all the prime numbers up to 100. Heres what I came up with... #light open System let mutable divisable = false let mutable j = 2 for i = 2 to 100 do j <- 2 while j < i do if i % j = 0 then divisab...

Generating REALLY big primes

I'm playing around and trying to write an implementation of RSA. The problem is that I'm stuck on generating the massive prime numbers that are involved in generating a key pair. Could someone point me to a fast way to generate huge primes/probable primes? ...