primes

A way to find the nearest prime number to an unsigned long integer ( 32 bits wide ) in C?

跟風遠走 提交于 2020-01-03 07:38:25
问题 I'm looking for a way to find the closest prime number. Greater or less than, it doesn't matter, simply the closest ( without overflowing, preferably. ) As for speed, if it can compute it in approximately 50 milliseconds on a 1GHz machine ( in software, running inside Linux ), I'd be ecstatic. 回答1: UPDATE 2 : Fixed (in a heavy-handed way) some bugs that caused wrong answers for small n. Thanks to Brett Hale for noticing! Also added some asserts to document some assumptions. UPDATE : I coded

Using java streams to find if a number is prime or not

亡梦爱人 提交于 2020-01-03 04:53:07
问题 I am reading Cracking the Coding Interview and it has an example of finding prime number which I ran on JShell boolean isPrime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } then I am trying to convert this to streams in java, but finding this difficult as mentioned boolean isPrimeStream(int n) { return IntStream.range(0, n) // how to do similar to for loop above .anyMatch(i -> n % i == 0); // i think this is the reason for failure } 回答1:

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

旧城冷巷雨未停 提交于 2020-01-02 10:08:48
问题 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

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

懵懂的女人 提交于 2020-01-02 07:31:13
问题 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

Generate Random Prime number in C/C++ between 2 limits

牧云@^-^@ 提交于 2020-01-02 03:30:29
问题 Is there a built in function that can generate a random prime number between 2 given limits in C/C++? I want a function that can generate a random prime number between 1 million and 1 billion 回答1: You can do this efficiently like so: Generate a random number in that interval; Check if it is divisible by any of the first few primes (say 2 .. 17 , experiment for best results). If yes, go to 1; Use Miller-Rabin to test for primality. Also see this for a similar, a bit more complex, idea. 回答2:

2-3-5-7 wheel factorization seems to skip prime number 331

家住魔仙堡 提交于 2020-01-01 15:07:13
问题 When following the procedure on wikipedia for wheel factorization, I seem to have stumbled into a problem where the prime number 331 is treated as a composite number if I try to build a 2-3-5-7 wheel. With 2-3-5-7 wheel, 2*3*5*7=210. So I setup a circle with 210 slots and go through steps 1-7 without any issues. Then I get to step 8 and strike off the spokes of all multiples of prime numbers, I eventually strike off the spoke rooted at 121, which is a multiple of 11, which is a prime. For the

Explain a code to check primality based on Fermat's little theorem

孤街醉人 提交于 2020-01-01 11:58:49
问题 I found some Python code that claims checking primality based on Fermat's little theorem: def CheckIfProbablyPrime(x): return (2 << x - 2) % x == 1 My questions: How does it work? What's its relation to Fermat's little theorem? How accurate is this method? If it's not accurate, what's the advantage of using it? I found it here. 回答1: 1. How does it work? Fermat's little theorem says that if a number x is prime, then for any integer a : If we divide both sides by a , then we can re-write the

Prime factor and JavaScript

冷暖自知 提交于 2019-12-31 04:13:06
问题 I'm stuck with the JavaScript code I am using for solving a problem which states: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? (this is not homework, is an online coding/mathematical challenge) So I came up with this solution: <html> <head> <script type="text/javascript"> // This function checks whether it's possible to divide the prime number function al(n){ // k = 13195 is the number that I have to find the prime factor for

Clojure - tail recursive sieve of Eratosthenes

旧城冷巷雨未停 提交于 2019-12-30 08:23:34
问题 I have this implementation of the sieve of Eratosthenes in Clojure: (defn sieve [n] (loop [last-tried 2 sift (range 2 (inc n))] (if (or (nil? last-tried) (> last-tried n)) sift (let [filtered (filter #(or (= % last-tried) (< 0 (rem % last-tried))) sift)] (let [next-to-try (first (filter #(> % last-tried) filtered))] (recur next-to-try filtered)))))) For larger n (like 20000) it ends with stack overflow. Why doesn't tail call elimination work here? How to fix it? 回答1: Problem: filter does lazy

Algorithm to find largest prime number smaller than x [closed]

a 夏天 提交于 2019-12-30 06:42:08
问题 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 8 years ago . How do I calculate the largest prime number smaller than value x? In actual fact, it doesn't have to be exact, just approximate and close to x. x is a 32 bit integer. The idea is that x is a configuration