primes

How much time should it take to find the sum of all prime numbers less than 2 million?

て烟熏妆下的殇ゞ 提交于 2019-12-01 04:34:15
问题 I was trying to solve this Project Euler Question. I implemented the sieve of euler as a helper class in java. It works pretty well for the small numbers. But when I input 2 million as the limit it doesn't return the answer. I use Netbeans IDE. I waited for a lot many hours once, but it still didn't print the answer. When I stopped running the code, it gave the following result Java Result: 2147483647 BUILD SUCCESSFUL (total time: 2,097 minutes 43 seconds) This answer is incorrect. Even after

Haskell style/efficiency

放肆的年华 提交于 2019-12-01 02:37:38
问题 So I was working on a way to lazily generate primes, and I came up with these three definitions, which all work in an equivalent way - just checking whether each new integer has a factor among all the preceding primes: primes1 :: [Integer] primes1 = mkPrimes id [2..] where mkPrimes f (x:xs) = if f (const True) x then let g h y = y `mod` x > 0 && h y in x : mkPrimes (f . g) xs else mkPrimes f xs primes2 :: [Integer] primes2 = mkPrimes id (const True) [2..] where mkPrimes f f_ (x:xs) = if f_ x

Prime numbers program

大兔子大兔子 提交于 2019-12-01 00:41:43
I'm currently trying out some questions just to practice my programming skills. ( Not taking it in school or anything yet, self taught ) I came across this problem which required me to read in a number from a given txt file. This number would be N. Now I'm suppose to find the Nth prime number for N <= 10 000. After I find it, I'm suppose to print it out to another txt file. Now for most parts of the question I'm able to understand and devise a method to get N. The problem is that I'm using an array to save previously found prime numbers so as to use them to check against future numbers. Even

Prime Number Generator Logic

六眼飞鱼酱① 提交于 2019-12-01 00:32:42
I am supposed to make a class PrimeNumberGenerator which has a method nextPrime that will print out all prime numbers up to a number the user inputs. Ex) Enter a Number: 20 2 3 5 7 11 13 17 19 Our teacher told us that we should use a nested for loop. I tried, but when I tried to make the inner (nested) loop, I got really confused. Here is my code: (I'm going to make a tester class later) public class PrimeGenerator { private int num; boolean isPrime; public PrimeGenerator(int n) { num = n; } public int nextPrime (int num) { for (int i=2; i < num; i++) // The first prime number is 2 and the

Python- Sieve of Eratosthenes- Compact Python

为君一笑 提交于 2019-12-01 00:13:23
This is my code for finding primes using the Sieve of Eratosthenes. list = [i for i in range(2, int(raw_input("Compute primes up to what number? "))+1)] for i in list: for a in list: if a!=i and a%i == 0: list.remove(a) Trying to find a way to compress those nested for loops into some kind of generator or comprehension, but it doesn't seem that you can apply a function to a list using a comprehension. I tried using map and filter, but I can't seem to get it right. Thinking about something like this: print map(list.remove(a), filter(lambda a, i: (a%i ==0 and a!=i), [(a, i) for i in list for a

Finding lists of prime numbers with SIMD - SSE/AVX

自作多情 提交于 2019-11-30 23:19:46
I'm curious if anyone has advice on how to use SIMD to find lists of prime numbers. Particularly I'm interested how to do this with SSE/AVX. The two algorithms I have been looking at are trial division and the Sieve of Eratosthenes. I have managed to find a way to use SSE with trial division. I found a faster way to to division which works well for a vector/scalar "Division by Invariant Integers Using Multiplication" http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.1.2556 Each time I find a prime I form the results to do a fast division and save them. Then the next time I do the

Stack space overflow when computing primes

不想你离开。 提交于 2019-11-30 22:24:39
I'm working my way through Real World Haskell (I'm in chapter 4) and to practice a bit off-book I've created the following program to calculate the nth prime. import System.Environment isPrime primes test = loop primes test where loop (p:primes) test | test `mod` p == 0 = False | p * p > test = True | otherwise = loop primes test primes = [2, 3] ++ loop [2, 3] 5 where loop primes test | isPrime primes test = test:(loop primes' test') | otherwise = test' `seq` (loop primes test') where test' = test + 2 primes' = primes ++ [test] main :: IO() main = do args <- getArgs print(last (take (read

Prime numbers list generator immediately closing when run

亡梦爱人 提交于 2019-11-30 21:50:58
问题 I'm trying to make a list of prime numbers. primes = [] num=int for num in range (2,100): for x in range (2, num): if (num % x) == 0: pass else: primes.append(num) break print(primes) input() but it closes immediately when I try to open the .py file. I think there is a problem with the code. 回答1: Your code executes and finishes, but it does not compute a list of prime numbers because it contains an error: When you test each num to see if it's prime, you can test all possible divisors (as you

Faster way to check if a number is a prime? [duplicate]

岁酱吖の 提交于 2019-11-30 21:01:11
This question already has an answer here: Program to find prime numbers 24 answers Fastest algorithm for primality test [closed] 10 answers I got this code that checks if a number is a prime: public static bool isPrime(int num) { if (num == 1) return false; if (num == 2) return true; int newnum = Math.Floor(Math.Sqrt(num)); for (int i = 2; i <= newnum; i++) if (num % i == 0) return false; return true; } Is there any better and faster way to check if a number is a prime? Yes there is. For one, you could check for 2 separately and then loop through only odd numbers. That would cut the search

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

佐手、 提交于 2019-11-30 20:52:41
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 parameter. I'm using the largest prime number less than x (call it y) as the parameter to a class constructor. The value y must be a prime number. Some good info here on the function pi(x). Apparently, pi(x) = the number of primes less than x and you can approximate pi(x) with x/(log x - 1) while the n-th prime of that list of primes is equal to approximately n(log n) How fast you need the