primes

Fastest algorithm for primality test [closed]

扶醉桌前 提交于 2019-11-27 03:44:32
I need to test primality on intervals between numbers which are really big (in the range of long long), so i need some fast algorithm for checking if a number is prime or not. Please suggest your ideas. One good method is the Miller-Rabin test. It should be noted however, that this is only a probabilistic test. A Miller-Rabin test to the seven bases 2, 325, 9375, 28178, 450775, 9780504, 1795265022 has been proved by Jim Sinclair to deterministically test if a number less than 2^64 is prime. See http://miller-rabin.appspot.com/ . I believe that the asymptotically fastest current (non

Scala, Erastothenes: Is there a straightforward way to replace a stream with an iteration?

泄露秘密 提交于 2019-11-27 02:56:35
问题 I wrote a function that generates primes indefinitely (wikipedia: incremental sieve of Erastothenes) usings streams. It returns a stream, but it also merges streams of prime multiples internally to mark upcoming composites. The definition is concise, functional, elegant and easy to understand, if I do say so myself: def primes(): Stream[Int] = { def merge(a: Stream[Int], b: Stream[Int]): Stream[Int] = { def next = a.head min b.head Stream.cons(next, merge(if (a.head == next) a.tail else a, if

Feasible implementation of a Prime Counting Function [closed]

我怕爱的太早我们不能终老 提交于 2019-11-27 02:51:47
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . Can anyone provide computationally feasible pseudocode of any prime-counting function implementation? I initially attempted coding the Hardy-Wright algorithm, but its factorials began generating miserable overflow, and many others appear bound to yield similar problems. I've scoured Google for practical

Optimize Sieve of Eratosthenes Further

谁说我不能喝 提交于 2019-11-27 02:21:44
I have written a Sieve of Eratosthenes--I think--but it seems like it's not as optimized as it could be. It works, and it gets all the primes up to N, but not as quickly as I'd hoped. I'm still learning Python--coming from two years of Java--so if something isn't particularly Pythonic then I apologize: def sieve(self): is_prime = [False, False, True, True] + [False, True] * ((self.lim - 4) // 2) for i in range(3, self.lim, 2): if i**2 > self.lim: break if is_prime[i]: for j in range(i * i, self.lim, i * 2): is_prime[j] = False return is_prime I've looked at other questions similar to this one

Adding wheel factorization to an indefinite sieve

只谈情不闲聊 提交于 2019-11-27 02:21:38
I’m modifying an indefinite sieve of Eratosthenes from here so it uses wheel factorization to skip more composites than its current form of just checking all odds. I’ve worked out how to generate the steps to take to reach all the gaps along the wheel. From there I figured I could just substitute the +2’s for these wheel steps but it’s causing the sieve to skip primes. Here's the code: from itertools import count, cycle def dvprm(end): "finds primes by trial division. returns a list" primes=[2] for i in range(3, end+1, 2): if all(map(lambda x:i%x, primes)): primes.append(i) return primes def

Prime number function in R

只谈情不闲聊 提交于 2019-11-27 02:05:44
I am trying to create a function to test if a given integer is a prime number, I tried using the following: tpn <- function(prime.num){ if(prime.num==2){ print("PRIME") } else { if(prime.num%%(2:(prime.num-1))!=0){ print("PRIME") } else { print("NOT PRIME") }}} This doesn't work, although I cant understand why. I am checking to see if the given number can be divided by any of the integers up to this number with no remainders. If it cant, then the number is prime. Another solution I found was: tpn <- function(pn){ if(sum(pn/1:pn==pn%/%1:pn)==2) print("prime") } This works. Although, I cant get

Enumerate factors of a number directly in ascending order without sorting?

允我心安 提交于 2019-11-27 01:58:59
Is there an efficient algorithm to enumerate the factors of a number n , in ascending order, without sorting? By “efficient” I mean: The algorithm avoids a brute-force search for divisors by starting with the prime-power factorization of n . The runtime complexity of the algorithm is O( d log₂ d ) or better, where d is the divisor count of n . The spatial complexity of the algorithm is O( d ). The algorithm avoids a sort operation. That is, the factors are produced in order rather than produced out of order and sorted afterward. Although enumerating using a simple recursive approach and then

Project Euler Question 3 Help

此生再无相见时 提交于 2019-11-27 01:02:35
问题 I'm trying to work through Project Euler and I'm hitting a barrier on problem 03. I have an algorithm that works for smaller numbers, but problem 3 uses a very, very large number. Problem 03: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? Here is my solution in C# and it's been running for I think close to an hour. I'm not looking for an answer because I do actually want to solve this myself. Mainly just looking for some help.

To find first N prime numbers in python

只愿长相守 提交于 2019-11-27 00:41:05
I am new to the programming world. I was just writing this code in python to generate N prime numbers. User should input the value for N which is the total number of prime numbers to print out. I have written this code but it doesn't throw the desired output. Instead it prints the prime numbers till the Nth number. For eg.: User enters the value of N = 7. Desired output: 2, 3, 5, 7, 11, 13, 19 Actual output: 2, 3, 5, 7 Kindly advise. i=1 x = int(input("Enter the number:")) for k in range (1, (x+1), 1): c=0 for j in range (1, (i+1), 1): a = i%j if (a==0): c = c+1 if (c==2): print (i) else: k =

nth ugly number

我们两清 提交于 2019-11-27 00:05:30
Numbers whose only prime factors are 2, 3 or 5 are called ugly numbers. Example: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... 1 can be considered as 2^0. I am working on finding nth ugly number. Note that these numbers are extremely sparsely distributed as n gets large. I wrote a trivial program that computes if a given number is ugly or not. For n > 500 - it became super slow. I tried using memoization - observation: ugly_number * 2, ugly_number * 3, ugly_number * 5 are all ugly. Even with that it is slow. I tried using some properties of log - since that will reduce this problem from