primes

Generator function for prime numbers [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-06 04:08:04
问题 This question already has answers here : Simple Prime Generator in Python (23 answers) Closed 5 years ago . I'm trying to write a generator function for printing prime numbers as follows def getPrimes(n): prime=True i=2 while(i<n): for a in range(2,i): if(i%a==0): prime=False break if(prime): yield i However I'm not getting the desired results p=getPrimes(100) should give me a generator function that will iterate primes from 2 through 100 but the result I'm getting is [2,3]. What am I doing

Number of distinct prime partitions [duplicate]

风格不统一 提交于 2019-12-06 03:17:53
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: A number as it’s prime number parts I have this homework assignment of mine, hard as hell, where I have to get all the distinct prime partitions of a given number. For example, number 7 has five different prime partitions (or five different ways of representing the 2 prime partitions it has): 5 + 2 2 + 5 3 + 2 + 2 2 + 3 + 2 2 + 2 + 3 As you can see, the number itself is excluded in the case it's a prime. I don't

Modulus warning in R- Lehmann Primality Test

匆匆过客 提交于 2019-12-06 02:29:53
I spent a little time hacking an R implementation of the lehmann primality test. The function design I borrowed from http://davidkendal.net/articles/2011/12/lehmann-primality-test Here is my code: primeTest <- function(n, iter){ a <- sample(1:(n-1), 1) lehmannTest <- function(y, tries){ x <- ((y^((n-1)/2)) %% n) if (tries == 0) { return(TRUE) }else{ if ((x == 1) | (x == (-1 %% n))){ lehmannTest(sample(1:(n-1), 1), (tries-1)) }else{ return(FALSE) } } } lehmannTest(a, iter) } primeTest(4, 50) # false primeTest(3, 50) # true primeTest(10, 50)# false primeTest(97, 50) # gives false # SHOULD BE

Common Lisp: is delete-if the same as setf + remove-if?

女生的网名这么多〃 提交于 2019-12-06 01:45:49
The following code generates prime from 1 to n: (defun prime-list(n) (let ((a)(b)(x (floor (sqrt n)))) (loop for i from (floor n 6) downto 1 do (push (1+ (* 6 i)) a) (push (1- (* 6 i)) a)) (loop while (<= (car a) x) do (push (car a) b) (setf a (remove-if #'(lambda(m)(or (= 0 (mod m (car a))) (> m n))) a))) (append '(2 3) (reverse b) a))) It seems to me the part (setf a (remove-if #'XXX a)) can be replaced by (delete-if #'XXX a) And I hoped this would make it faster. However when I made that change the function now get into an infinite loop and never returns. Why? As mentioned in the comments,

Fastest prime test for small-ish numbers

浪尽此生 提交于 2019-12-06 00:30:19
问题 I'm playing through project Euler in my spare time, and it's come to the point where I need to do some refactoring. I've implemented Miller-Rabin, as well as a few sieves. I've heard before that sieves are actually faster for small-ish numbers, as in under a few million. Does anybody have any information on this? Google wasn't very helpful. 回答1: Yes, you'll find with most algorithms that you can trade space for time. In other words, by allowing the use of more memory, the speed is greatly

Of Ways to Count the Limitless Primes [closed]

折月煮酒 提交于 2019-12-05 21:52:32
Alright, so maybe I shouldn't have shrunk this question sooo much... I have seen the post on the most efficient way to find the first 10000 primes . I'm looking for all possible ways . The goal is to have a one stop shop for primality tests. Any and all tests people know for finding prime numbers are welcome. And so: What are all the different ways of finding primes? Some prime tests only work with certain numbers, for instance, the Lucas–Lehmer test only works for Mersenne numbers. Most prime tests used for big numbers can only tell you that a certain number is "probably prime" (or, if the

How to generate the 1000th prime in python?

跟風遠走 提交于 2019-12-05 19:02:20
问题 count = 0 i = 11 while count <= 1000 and i <= 10000: if i%2 != 0: if (i%3 == 0 or i%4 == 0 or i%5 == 0 or i%6 == 0 or i%7 == 0 or i%9 == 0): continue else: print i,'is prime.' count += 1 i+=1 I'm trying to generate the 1000th prime number only through the use of loops. I generate the primes correctly but the last prime i get is not the 1000th prime. How can i modify my code to do so. Thank in advance for the help. EDIT: I understand how to do this problem now. But can someone please explain

Learning Haskell: Seemingly Circular Program - Please help explain

为君一笑 提交于 2019-12-05 18:23:39
问题 I'm currently going through the book "The Haskell Road to Logic, Math, and Programming" by Doets and Van Eijck. I've never been exposed to any functional programming language until this book, so keep that in mind. Still early in the book, it gives the following code for a primality test: ldp :: Integer -> Integer ldp n = ldpf primes1 n ldpf :: [Integer] -> Integer -> Integer ldpf (p:ps) n | rem n p == 0 = p | p^2 > n = n | otherwise = ldpf ps n primes1 :: [Integer] primes1 = 2 : filter prime

Clojure: Avoiding stack overflow in Sieve of Erathosthene?

南笙酒味 提交于 2019-12-05 18:19:12
问题 Here's my implementation of Sieve of Erathosthene in Clojure (based on SICP lesson on streams): (defn nats-from [n] (iterate inc n)) (defn divide? [p q] (zero? (rem q p))) (defn sieve [stream] (lazy-seq (cons (first stream) (sieve (remove #(divide? (first stream) %) (rest stream)))))) (def primes (sieve (nats-from 2))) Now, it's all OK when i take first 100 primes: (take 100 primes) But, if i try to take first 1000 primes , program breaks because of stack overflow. I'm wondering if is it

Quickly determine if a number is prime in Python for numbers < 1 billion

廉价感情. 提交于 2019-12-05 16:48:13
问题 My current algorithm to check the primality of numbers in python is way to slow for numbers between 10 million and 1 billion. I want it to be improved knowing that I will never get numbers bigger than 1 billion. The context is that I can't get an implementation that is quick enough for solving problem 60 of project Euler: I'm getting the answer to the problem in 75 seconds where I need it in 60 seconds. http://projecteuler.net/index.php?section=problems&id=60 I have very few memory at my