primes

Efficient algorithm to get primes in large numbers.

Hi all , I'm a beginner in C#, I'm trying to write an application to get Primes between two numbers entered by the user .. The Problem is at larg no. like 1 to 10000000 getting the primes takes long time and according to the problem i'm solving, the whole operation must be carried out in a small time interval , this is the problem link f...

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 to slightly speed up the function but I could not yet take away the division, though I do...

Prime Number Formula

I am trying to write a prime number function in C# and I am wondering if the follow code will work. It "appears" to work with the first 50 numbers or so. I just want to make sure it will work no matter how big the number is: static bool IsPrime(int number) { if ((number == 2) || (number == 3) || (number == 5) || (number == 7) || (nu...

How does this regex find primes?

Possible Duplicate: How to determine if a number is a prime with regex? This page claims that this regular expression discovers non-prime numbers (and by counter-example: primes): /^1?$|^(11+?)\1+$/ How does this find primes? ...

How does this regular expression work?

From this article, /^1?$|^(11+?)\1+$/ checks whether a number(its value in unary) is prime or not. Using this, perl -l -e '(1 x $_) !~ /^1?$|^(11+?)\1+$/ && print while ++$_;' returns a list of prime numbers. I do not have enough experience with Perl, but what I understand is that the regular expression will be true for a number that ...

What are typical runtimes for Miller-Rabin primality testing?

I'm well aware that a single Miller-Rabin test runs in cubic log time. I know about Montgomery modular exponentiation and GNFS and I'm not asking about any of that fancy theory. What I am wondering is what some representative runtimes for MR (note that this is not the same as an RSA operation) on characteristic hardware (e.g., a 2.2 GHz ...

Need help optimizing solution for Project Euler problem #12.

Hey everyone, I've been having my fun with Project Euler challenges again and I've noticed that my solution for number 12 is one of my slowest at ~593.275 ms per runthrough. This is second to my solution for number 10 at ~1254.593 ms per runthrough. All of my other answers take less than 3 ms to run with most well under 1 ms. My Java s...

Project Euler #10, java

So, I'm attempting to solve Project Euler Problem 10 in Java, and I'm getting not the right answer. Here's my code: public class Problem10 { public static void main(String[] args) { long sum =0; for(int i =3;i<2000000;i+=2) { if(isPrime(i)) { sum+=i; } } System.out.println(sum); } ...

Dictionary of Primes

I was trying to create this helper function in C# that returns the first n prime numbers. I decided to store the numbers in a dictionary in the <int,bool> format. The key is the number in question and the bool represents whether the int is a prime or not. There are a ton of resources out there calculating/generating the prime numbers(SO ...

Beginner question about heap and garbage in Clojure

I have a question about Clojure: I am trying to learn the language by going through Project Euler and I don't understand what is going on under the hood: The following code is designed to use return a list of all prime numbers up to lim. I would think that it should be O(n) in heap-space because I make a list of all the numbers up to li...

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 composite number. I'm appending my code below, I'd be grateful if anyone could help me out...

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 hyperthreaded processors and 16GB of ram. I realize that there are some improvements that could be m...

Lazy List of Prime Numbers

How would one implement a list of prime numbers in Haskell so that they could be retrieved lazily? I am new to Haskell, and would like to learn about practical uses of the lazy evaluation functionality. ...

Why use a prime number in hashCode?

Hey so I'm sorry if this is really obvious but I was just wondering why is that primes are used in a class's hashCode() method? For example, when using Eclipse to generate my hashCode() method there is always the prime number 31 used: public int hashCode() { final int prime = 31; //... } Thanks in advance!! ...

Generating all factors of a number given its prime factorization

If you already have the prime factorization of a number, what is the easiest way to get the set of all factors of that number? I know I could just loop from 2 to sqrt(n) and find all divisible numbers, but that seems inefficient since we already have the prime factorization. I imagine it's basically a modified version of a combinations...

Converting prime numbers

Possible Duplicate: Help with algorithm problem from SPOJ Came across this interview question. Given two n-digit prime numbers, convert the first prime number to the second changing one digit at a time. The intermediate numbers also need to be prime. This needs to be done in the minimum number of steps (checking for primality ...

Good algorithms to determine whether one number is prime?

Possible Duplicate: Checking if an int is prime more efficiently I need to test some very large integer to see whether it is a prime. Could you provide some good algorithms or library routines? EDIT: C/C++ would be OK. Thanks. ...

Time Limit Exceeded ->Python , SPOJ, Prime Generator

Hi ,the URL of the problem is ( https://www.spoj.pl/problems/PRIME1/) yeah i did go through some thread about having time limit exceeded. I am using Python 2.6.4 . IDLE . The code is here : def prime(a): count=0 for i in range(2,int(math.sqrt(a))): if(a%i==0): count=count+1 if(count==0): return...

My Qustion 1: It's an assignmnt

The number 3797 has an interesting property...Being prime itself , it is possible to continuously remove digits from left to right,but remain prime at each stage....:3797,797,97,7....similarly we can work from right to left:3797,379,37,3. Find the of only 11 primes that are both truncatable from left to right....and right to left. Pls tr...

Confused on Miller-Rabin

As an exercise for myself, I'm implementing the Miller-Rabin test. (Working through SICP). I understand Fermat's little theorem and was able to successfully implement that. The part that I'm getting tripped up on in the Miller-Rabin test is this "1 mod n" business. Isn't 1 mod n (n being some random integer) always 1? So I'm confused at ...