primes

Haskell prime test

与世无争的帅哥 提交于 2019-11-30 13:44:33
I'm new to Haskell, and I'm trying a bit: isPrime :: Integer->Bool isPrime x = ([] == [y | y<-[2..floor (sqrt x)], mod x y == 0]) I have a few questions. Why when I try to load the .hs, WinHugs say: Instances of (Floating Integer, RealFrac Integer) required for definition of isPrime ? When the interpreter finds one element in the right set, it immediately stops or it computes all the set? I think you know what I mean. Sorry about my english. 1) The problem is that sqrt has the type (Floating a) => a -> a , but you try to use an Integer as argument. So you have to convert your Integer first to

Sieve of Atkin explanation

蓝咒 提交于 2019-11-30 11:27:13
问题 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. How does it work? Example code (preferably in C or python) would be brilliant. Edit: thanks for your help, the only thing that I still do not understand is

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

不羁岁月 提交于 2019-11-30 10:50:18
问题 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 nothings making it clearer. This is what I think the code does: /** * [2,3] * * takeWhile 2*2 <= 3 * takeWhile 2*2 <= 4 found match * (4 % [2,3] > 1) return false. * takeWhile 2*2 <= 5 found match * (5 % [2,3] > 1) return true * Add 5 to the list *

Miller Rabin Primality test accuracy

馋奶兔 提交于 2019-11-30 10:27:31
I know the Miller–Rabin primality test is probabilistic. However I want to use it for a programming task that leaves no room for error. Can we assume that it is correct with very high probability if the input numbers are 64-bit integers (i.e. long long in C)? Miller–Rabin is indeed probabilistic, but you can trade accuracy for computation time arbitrarily. If the number you test is prime, it will always give the correct answer. The problematic case is when a number is composite, but is reported to be prime. We can bound the probability of this error using the formula on Wikipedia : If you

Find out 20th, 30th, nth prime number. (I'm getting 20th but not 30th?) [Python]

自闭症网瘾萝莉.ら 提交于 2019-11-30 09:49:36
The question is to find the 1000th prime number. I wrote the following python code for this. The problem is, I get the right answer for the 10th , 20th prime but after that each increment of 10 leaves me one off the mark. I can't catch the bug here :( count=1 #to keep count of prime numbers primes=() #tuple to hold primes candidate=3 #variable to test for primes while count<20: for x in range(2,candidate): if candidate%x==0: candidate=candidate+2 else : pass primes=primes+(candidate,) candidate=candidate+2 count=count+1 print primes print "20th prime is ", primes[-1] In case you're wondering,

Why do two algorithms for finding primes differ in speed so much even though they seem to do the same number of iterations?

北城余情 提交于 2019-11-30 09:17:34
问题 I have two algorithms of finding primes, in Python. The inner loop of each one seems to be executed the same number of times, and is equally simple. However, one of them takes 10 times as much as the other. My question is: Why? Is this some quirk of Python that can be optimized away (how?), or am I missing something else? The problem I am solving is essentially from http://www.spoj.pl/problems/PRIME1/. In my case, I have N = 10 ** 9, delta = 10 ** 5, and I want to find all primes between N

Finding the exponent of n = 2**x using bitwise operations [logarithm in base 2 of n]

家住魔仙堡 提交于 2019-11-30 08:44:24
Is there a straightforward way to extracting the exponent from a power of 2 using bitwise operations only? EDIT: Although the question was originally about bitwise operations, the thread is a good read also if you are wondering "What's the fastest way to find X given Y = 2 X in Python ?"** I am currently trying to optimize a routine ( Rabin-Miller primality test ) that reduces an even number N in the forms 2**s * d . I can get the 2**s part by: two_power_s = N & -N but I can't find a way to extract just " s " with a bitwise operation. Workarounds I am currently testing without too much

How to pick prime numbers to calculate the hash code?

牧云@^-^@ 提交于 2019-11-30 07:36:56
This question follows on the answer given by Jon Skeet on the question: " What is the best algorithm for an overridden System.Object.GetHashCode? ". To calculate the hash code the following algorithm is used: public override int GetHashCode() { unchecked // Overflow is fine, just wrap { int hash = 17; // Suitable nullity checks etc, of course :) hash = hash * 23 + field1.GetHashCode(); hash = hash * 23 + field2.GetHashCode(); hash = hash * 23 + field3.GetHashCode(); return hash; } } I don't understand why the numbers 17 and 23 are chosen. Why don't we pick 3 and 5? That are prime numbers as

Random prime number

你。 提交于 2019-11-30 06:40:04
How do I quickly generate a random prime number, that is for sure 1024 bit long? Generate 1024 random bits. Use a random source that is strong enough for your intended purpose. Set the highest and lowest bits to 1. This makes sure there are no leading zeros (the prime candidate is big enough) and it is not an even number (definitely not prime). Test for primality . If it's not a prime, go back to 1. Alternatively, use a library function that generates primes for you. Use a library function, such as OpenSSL. There's no need to write this yourself. Example: http://ardoino.com/7-maths-openssl

Racket Programming. Where am I going wrong?

假装没事ソ 提交于 2019-11-30 06:04:58
问题 The question i'm trying to answer: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? Where am I going wrong? my prime? test seems to be the issue, but it works fine on relatively small numbers. However the prime? test gives a wrong answer with larger numbers. Is there an easier way to go about this? (define b 3) (define z 0) (define divides? (lambda (a b) (= (remainder a b) 0))) (define (prime? n) (cond ((or (= n 1) (= n 0)) false) (