primes

Finding first n primes? [duplicate]

筅森魡賤 提交于 2019-12-06 12:35:22
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Fastest way to list all primes below N in python Although I already have written a function to find all primes under n ( primes(10) -> [2, 3, 5, 7] ), I am struggling to find a quick way to find the first n primes. What is the fastest way to do this? 回答1: Start with the estimate g(n) = n log n + n log log n *, which estimates the size of the nth prime for n > 5. Then run a sieve on that estimate. g(n) gives an

Optimization of Algorithm [closed]

会有一股神秘感。 提交于 2019-12-06 11:23:36
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . Here is the link to the problem. The problem asks the number of solutions to the Diophantine equation of the form 1/x + 1/y = 1/z (where z = n! ). Rearranging the given equation clearly tells that the answer is

Haskell list comprehension for finding primes

血红的双手。 提交于 2019-12-06 10:51:54
I'm trying to find all the primes less than some integer n as concisely as possible, using list comprehensions. I'm learning Haskell, and this is just an exercise. I'd like to write something like: isqrt :: Integral a => a -> a isqrt = floor . sqrt . fromIntegral primes :: Integral a => a -> [a] primes n = [i | i <- [1,3..n], mod i k /= 0 | k <- primes (isqrt i)] which of course doesn't work. Is there a way to have a list comprehension inside a list comprehension? Here is the error I'm getting: exercise-99-1.hs:138:39: Not in scope: `k' exercise-99-1.hs:138:46: Illegal parallel list

++, last & init faster than :, head & tail?

我与影子孤独终老i 提交于 2019-12-06 09:07:50
问题 Given these two ways of writing a function that finds all primes up to a specific number: primes1 = iterate (\ps -> ps ++ [([x | x <- [last ps + 1..], all (\p -> x `mod` p /= 0) ps] !! 0)]) [2] primesTo1 :: Integer -> [Integer] primesTo1 n = init $ head $ dropWhile (\p -> last p <= n) primes1 primes2 = iterate (\ps -> ([x | x <- [head ps + 1..], all (\p -> x `mod` p /= 0) ps] !! 0) : ps) [2] primesTo2 :: Integer -> [Integer] primesTo2 n = tail $ head $ dropWhile (\p -> head p <= n) primes2

Miller-Rabin Primality test in Java

落花浮王杯 提交于 2019-12-06 08:48:11
问题 I am currently working on Project Euler and thought that it might be more interesting (and a better learning experience) if don't just brute force all of the questions. On question 3 it asks for prime factors of a number and my solution will be to factor the number (using another factoring algorithm) and then test the factors for primality. I came up with this code for a Miller-Rabin Primality test (after thoroughly researching primality test) and it returns true for all the composite odd

Optimization of prime number code

陌路散爱 提交于 2019-12-06 08:42:53
问题 This is my code in python for calculation of sum of prime numbers less than a given number. What more can I do to optimize it? import math primes = [2,] #primes store the prime numbers for i in xrange(3,20000,2): #i is the test number x = math.sqrt(i) isprime = True for j in primes: #j is the devider. only primes are used as deviders if j <= x: if i%j == 0: isprime = False break if isprime: primes.append(i,) print sum (primes,) 回答1: You can use a different algorithm called the Sieve of

Checksumming large swathes of prime numbers? (for verification)

岁酱吖の 提交于 2019-12-06 06:23:05
问题 Are there any clever algorithms for computing high-quality checksums on millions or billions of prime numbers? I.e. with maximum error-detection capability and perhaps segmentable? Motivation: Small primes - up to 64 bits in size - can be sieved on demand to the tune of millions per second, by using a small bitmap for sieving potential factors (up to 2^32-1) and a second bitmap for sieving the numbers in the target range. Algorithm and implementation are reasonably simple and straightforward

Sieve of Eratosthenes has huge 'overdraw' - is Sundaram's better after all?

北城余情 提交于 2019-12-06 06:22:26
问题 The standard Sieve of Eratosthenes crosses out most composites multiple times; in fact the only ones that do not get marked more than once are those that are the product of exactly two primes. Naturally, the overdraw increases as the sieve gets bigger. For an odd sieve (i.e. without the evens) the overdraw hits 100% for n = 3,509,227, with 1,503,868 composites and 1,503,868 crossings-out of already crossed-out numbers. For n = 2^32 the overdraw rises to 134.25% (overdraw 2,610,022,328 vs. pop

Given a number X, estimate where in an ordered list of primes that number may fall

依然范特西╮ 提交于 2019-12-06 05:22:35
Given a pre-calculated ordered list of primes, and a supplied number X, I want to estimate roughly where X would fall in the list of primes, and start searching at that point. So, I have calculated and stored a list of primes from 1..2^32-1, in a binary file. I've got methods in a program that runs against that file to give me the nth prime, a random prime, how many primes exist, etc. But in order to add a function to this program to tell me where a supplied number is prime, I am having trouble coming up with a way to estimate where to begin searching. Doing it the naïve O(n) method quickly

Relatively Prime Numbers

て烟熏妆下的殇ゞ 提交于 2019-12-06 05:05:39
问题 How to make a function in c++ to determine if two entered numbers are relatively prime (no common factors)? For example "1, 3" would be valid, but "2, 4" wouldn't. 回答1: Galvanised into action by Jim Clay's incautious comment, here is Euclid's algorithm in six lines of code: bool RelativelyPrime (int a, int b) { // Assumes a, b > 0 for ( ; ; ) { if (!(a %= b)) return b == 1 ; if (!(b %= a)) return a == 1 ; } } Updated to add: I have been out-obfuscated by this answer from Omnifarious, who