primes

Is there a simple algorithm that can determine if X is prime, and not confuse a mere mortal programmer?

浪子不回头ぞ 提交于 2019-11-27 06:37:58
I have been trying to work my way through Project Euler, and have noticed a handful of problems ask for you to determine a prime number as part of it. I know I can just divide x by 2, 3, 4, 5, ..., square root of X and if I get to the square root, I can (safely) assume that the number is prime. Unfortunately this solution seems quite klunky. I've looked into better algorithms on how to determine if a number is prime, but get confused fast. Is there a simple algorithm that can determine if X is prime, and not confuse a mere mortal programmer? Thanks much! The first algorithm is quite good and

Sieve of Eratosthenes algorithm in C

前提是你 提交于 2019-11-27 06:31:22
问题 Okay, so this function I created uses the Sieve of Eratosthenes algorithm to compute all the primes <= n. This function stores the prime numbers and the count of primes in the parameters. When the function exits, primes should be pointing to a chunk of dynamically allocated memory that holds all the primes <= num. *count will have the count of primes. Here is my function getPrimes : void getPrimes(int num, int* count, int** array){ (*count) = (num - 1); int sieve[num-1], primenums = 0, index,

How do i reduce the space complexity in Sieve of Eratosthenes to generate prime between a and b?

雨燕双飞 提交于 2019-11-27 06:29:38
问题 After getting through some of the SO posts, i found Sieve of Eratosthenes is the best & fastest way of generating prime numbers. I want to generate the prime numbers between two numbers, say a and b . AFAIK, in Sieve's method, the space complexity is O( b ). PS: I wrote Big-O and not Theta, because i don't know whether the space requirement can be reduced. Can we reduce the space complexity in Sieve of Eratosthenes ? 回答1: If you have enough space to store all the primes up to sqrt(b) then you

Implementing the Page Segmented Sieve of Eratosthenes in Javascript

↘锁芯ラ 提交于 2019-11-27 06:29:25
问题 I recently read about a faster implementation of Segmented Sieve of Eratosthenes for really big numbers. Following is an implementation of the same: function sieve(low, high) { var primeArray = [], ll = Math.sqrt(low), output = []; for (var i = 0; i < high; i++) { primeArray[i] = true; } for (var i = 2; i <= ll; i++) { if (primeArray[i]) { for (var j = i * i; j < high; j += i) { primeArray[j] = false; } } } for (var i = 2; i < ll; i++) { if(primeArray[i]) { var segmentStart = Math.floor(low/i

A Fast Prime Number Sieve in Python

╄→гoц情女王★ 提交于 2019-11-27 06:14:33
问题 I have been going through prime number generation in python using the sieve of Eratosthenes and the solutions which people tout as a relatively fast option such as those in a few of the answers to a question on optimising prime number generation in python are not straightforward and the simple implementation which I have here rivals them in efficiency. My implementation is given below def sieve_for_primes_to(n): size = n//2 sieve = [1]*size limit = int(n**0.5) for i in range(1,limit): if

Fastest-in term of space- way to find prime numbers with python

痞子三分冷 提交于 2019-11-27 05:33:36
Maybe it is a stupid question, but i was wondering if you could provide the shortest source to find prime numbers with Python. I was also wondering how to find prime numbers by using map() or filter() functions. Thank you (: EDIT: When i say fastest/shortest I mean the way with the less characters/words. Do not consider a competition, anyway: i was wondering if it was possible a one line source, without removing indentation always used with for cycles. EDIT 2:The problem was not thought for huge numbers. I think we can stay under a million( range(2,1000000) EDIT 3: Shortest, but still elegant.

AKS Primes algorithm in Python

不羁的心 提交于 2019-11-27 05:24:11
问题 A few years ago, it was proven that PRIMES is in P. Are there any algorithms implementing their primality test in Python? I wanted to run some benchmarks with a naive generator and see for myself how fast it is. I'd implement it myself, but I don't understand the paper enough yet to do that. 回答1: Quick answer: no, the AKS test is not the fastest way to test primality. There are much much faster primality tests that either assume the (generalized) Riemann hypothesis and/or are randomized. (E.g

How does this regex find primes? [duplicate]

冷暖自知 提交于 2019-11-27 05:04:25
问题 This question already has answers here : Closed 9 years ago . 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? 回答1: I think the article explains it rather well, but I'll try my hand at it as well. Input is in unary form. 1 is 1 , 2 is 11 , 3 is 111 , etc. Zero is an empty string. The first part of the regex matches 0

New state of the art in unlimited generation of Hamming sequence

ε祈祈猫儿з 提交于 2019-11-27 04:54:57
(this is exciting!) I know, the subject matter is well known. The state of the art (in Haskell as well as other languages) for efficient generation of unbounded increasing sequence of Hamming numbers, without duplicates and without omissions, has long been the following (AFAIK - and btw it is equivalent to the original Edsger Dijkstra's code too): hamm :: [Integer] hamm = 1 : map (2*) hamm `union` map (3*) hamm `union` map (5*) hamm where union a@(x:xs) b@(y:ys) = case compare x y of LT -> x : union xs b EQ -> x : union xs ys GT -> y : union a ys The question I'm asking is, can you find the

Reason for 5381 number in DJB hash function?

柔情痞子 提交于 2019-11-27 04:11:44
问题 Can anyone tell me why the number 5381 is used in DJB hash function ? DJB Hash function is h(0) = 5381 h(i) = 33 * h(i-1) ^ str[i] A c program: unsigned int DJBHash(char* str, unsigned int len) { unsigned int hash = 5381; unsigned int i = 0; for(i = 0; i < len; str++, i++) { hash = ((hash << 5) + hash) + (*str); } return hash; } 回答1: 5381 is just a number that, in testing, resulted in fewer collisions and better avalanching. You'll find "magic constants" in just about every hash algo. 回答2: I