primes

Python: how does the generator and filter work in the codes generating prime list with filter() [closed]

谁说胖子不能爱 提交于 2019-12-08 05:13:25
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 10 months ago . Note: This question is different with using filter and generator to generator endless prime number in python although both of them are related to Python code finding all the prime numbers up to a given limit. The core codes are actually very simple, but it's hard for me to understand how it works

Is the Sieve of Eratosthenes an example of Dynamic Programming?

半腔热情 提交于 2019-12-08 03:53:08
问题 I'm a bit confused as to whether the Sieve of Eratosthenes (implemented with an array for all the numbers and a loop marking the composite numbers) is an example of Dynamic Programming? A couple of friends were telling me the way it's implemented is an example of Bottom Up DP, but I'm having trouble seeing it. Exactly what are the subproblems and how would you implement SoE with Top-Down / Recursion? Thanks guys. 回答1: Sure, we could think of the Sieve of Eratosthenes as an example of dynamic

Haskell list comprehension for finding primes

给你一囗甜甜゛ 提交于 2019-12-08 01:08:30
问题 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

Check whether an integer (or all elements of a list of integers) be prime

血红的双手。 提交于 2019-12-07 22:07:18
问题 I'm doing a simple Haskell function using recursion. At the moment, this seems to work but, if I enter 2 , it actually comes up as false, which is irritating. I don't think the code is as good as it could be, so, if you have any advice there, that'd be cool too! I'm pretty new to this language! EDIT: Ok, so I understand what a prime number is. For example, I want to be able to check 2, 3, 5, 7, etc and have isPrime return true . And of course if I run the function using 1, 4, 6, 8 etc then it

Spoj PRIME1 using sieve of eratosthenes (in C)

試著忘記壹切 提交于 2019-12-07 17:13:29
问题 I'm trying to solve the problem PRIME1 using segmented sieve of Eratosthenes. My program works correctly with the normal sieve that is up to NEW_MAX . But there is some problem with cases n > NEW_MAX , where segmented sieving comes into play. In such cases it merely prints all the numbers. Here is the link to the code with relevant test cases: http://ideone.com/8H5lK#view_edit_box /* segmented sieve */ #include <math.h> #include <stdio.h> #include <stdlib.h> #define MAX_LIMIT 1000000000 //10

Of Ways to Count the Limitless Primes [closed]

℡╲_俬逩灬. 提交于 2019-12-07 13:28:53
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . Alright, so maybe I shouldn't have shrunk this question sooo much... I have seen the post on the most efficient way to find the first 10000 primes. I'm looking for all possible ways . The goal is to have a one stop shop for primality tests. Any and all tests people know for

C program to find a prime number

[亡魂溺海] 提交于 2019-12-07 08:11:27
I wrote a C program which tells whether a given number is prime or not. But it has a problem in it. It is working fine for numbers other than multiples of 5. But it is showing the multiples of 5 as prime like 15, 25, 35, 45... . I am not able to find the error. I've tried comparing it with other programs on the internet but I am not able to find the error. #include <stdio.h> int primeornot(int a) { int i; for (i = 2; i <= a / 2; i++) { if (a % i == 0) { return 0; break; } else { return 1; } } } main() { int number_given_by_user; printf("Enter a positive integer to find whether it is prime or

How can I find prime numbers through bit operations in C++?

≡放荡痞女 提交于 2019-12-07 07:35:22
问题 How can I find prime numbers through bit operations in C++? 回答1: I think the way to do this is to not think of the bitset as its numerical representation as we normally do, but to think of it at a list of numbers. So the bitset 1111 would represent the numbers 1, 2, 3, and 4. Now if we say that a '1' represents prime and a '0' represents not prime, we can make a sieve as follows. Set all the bits to 1 (I'm going to use 16 bits that represent the integers 1 through 16) 1111 1111 1111 1111 I

Getting functional sieve of Eratosthenes fast

岁酱吖の 提交于 2019-12-07 04:57:29
问题 I read this other post about a F# version of this algorithm. I found it very elegant and tried to combine some ideas of the answers. Although I optimized it to make fewer checks (check only numbers around 6) and leave out unnecessary caching, it is still painfully slow. Calculating the 10,000 th prime already take more than 5 minutes. Using the imperative approach, I can test all 31-bit integers in not that much more time. So my question is if I am missing something that makes all this so

Did I just prove that sieve of Eratosthenes is less efficient than trial division?

谁说我不能喝 提交于 2019-12-07 02:17:02
问题 I was trying to compare the run-time speed of two algorithms: A brute-force C program to print prime numbers (10,000 numbers), and a Sieve of Eratosthenes C program (also 10,000 prime numbers). My measured run-time for the sieve algorithm was: 0.744 seconds My measured run-time for the brute-force algorithm was: 0.262 seconds However, I was told that the Sieve of Eratosthenes algorithm is more efficient than the brute-force method, and so I thought it would run faster . So either I'm wrong or