primes

isPrime Function for Python Language

混江龙づ霸主 提交于 2019-11-26 12:55:02
So I was able to solve this problem with a little bit of help from the internet and this is what I got: def isPrime(n): for i in range(2,int(n**0.5)+1): if n%i==0: return False return True But my question really is how to do it, but WHY. I understand that 1 is not considered a "prime" number even though it is, and I understand that if it divides by ANYTHING within the range it is automatically prime thus the return False statement. but my question is what role does the squaring the "n" play here ? Thank you very much for your attention P.s. I am very inexperienced and have just been introduced

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

柔情痞子 提交于 2019-11-26 12:07:24
问题 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

Given Prime Number N, Compute the Next Prime?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 11:55:17
问题 A coworker just told me that the C# Dictionary collection resizes by prime numbers for arcane reasons relating to hashing. And my immediate question was, \"how does it know what the next prime is? do they story a giant table or compute on the fly? that\'s a scary non-deterministic runtime on an insert causing a resize\" So my question is, given N, which is a prime number, what is the most efficient way to compute the next prime number? 回答1: The gaps between consecutive prime numbers is known

Explain this chunk of haskell code that outputs a stream of primes

不打扰是莪最后的温柔 提交于 2019-11-26 10:36:24
I have trouble understanding this chunk of code: let sieve (p:xs) = p : sieve (filter (\ x -> x `mod` p /= 0) xs) in sieve [2 .. ] Can someone break it down for me? I understand there is recursion in it, but thats the problem I can't understand how the recursion in this example works. mipadi It's actually pretty elegant. First, we define a function sieve that takes a list of elements: sieve (p:xs) = In the body of sieve , we take the head of the list (because we're passing the infinite list [2..] , and 2 is defined to be prime) and append it (lazily!) to the result of applying sieve to the

What would be the fastest method to test for primality in Java?

别来无恙 提交于 2019-11-26 10:19:25
I am trying to find the fastest way to check whether a given number is prime or not (in Java). Below are several primality testing methods I came up with. Is there any better way than the second implementation(isPrime2)? public class Prime { public static boolean isPrime1(int n) { if (n <= 1) { return false; } if (n == 2) { return true; } for (int i = 2; i <= Math.sqrt(n) + 1; i++) { if (n % i == 0) { return false; } } return true; } public static boolean isPrime2(int n) { if (n <= 1) { return false; } if (n == 2) { return true; } if (n % 2 == 0) { return false; } for (int i = 3; i <= Math

Most efficient code for the first 10000 prime numbers?

扶醉桌前 提交于 2019-11-26 10:18:53
问题 I want to print the first 10000 prime numbers. Can anyone give me the most efficient code for this? Clarifications: It does not matter if your code is inefficient for n >10000. The size of the code does not matter. You cannot just hard code the values in any manner. 回答1: The Sieve of Atkin is probably what you're looking for, its upper bound running time is O(N/log log N). If you only run the numbers 1 more and 1 less than the multiples of 6, it could be even faster, as all prime numbers

Optimize Sieve of Eratosthenes Further

烂漫一生 提交于 2019-11-26 10:02:21
问题 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

Adding wheel factorization to an indefinite sieve

♀尐吖头ヾ 提交于 2019-11-26 10:02:20
问题 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\"

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

拟墨画扇 提交于 2019-11-26 09:51:02
问题 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

To find first N prime numbers in python

ぐ巨炮叔叔 提交于 2019-11-26 09:28:43
问题 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