primes

reporting all prime numbers less than n

我的未来我决定 提交于 2019-12-01 14:34:13
I need to print all the prime numbers less than a given number n. I can use sieve of Eratothenes but the running time of that algorithm IS NOT O(n). Is there any O(n) time solution for this problem? I don't think you'll find an algorithm for checking an arbitrary number for primality with an O(n) time complexity. I'm pretty certain the NSA (and any other organisations that deal with crypto issues) wouldn't be very happy with that :-) The only way you'll get an O(n) or better way is to pre-calculate (for example) the first fifty million primes (or use someone else's already-precalculated list )

C++ Prime Numbers program [closed]

老子叫甜甜 提交于 2019-12-01 13:34:14
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . I'm working on a C++ program that determines and prints the prime numbers between 3 and an integer 'x' the user inputs. I'm assuming that I need a double

prime number summing still slow after using sieve

独自空忆成欢 提交于 2019-12-01 12:57:21
I had a go at a project euler coding challenge below, the answer given by the code is correct but I do not understand why its taking nearly a minute to run. It was finishing with similar times prior to using a sieve. Others users are reporting times as low as milliseconds. I assume I am making a basic error somewhere... // The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. // Find the sum of all the primes below two million. public static long Ex010() { var sum = 0L; var sieve = new bool[2000000]; var primes = new List<int>(10000); for (int i = 2; i < sieve.Length; i++) { if (sieve[i-1])

reporting all prime numbers less than n

浪尽此生 提交于 2019-12-01 12:34:45
问题 I need to print all the prime numbers less than a given number n. I can use sieve of Eratothenes but the running time of that algorithm IS NOT O(n). Is there any O(n) time solution for this problem? 回答1: I don't think you'll find an algorithm for checking an arbitrary number for primality with an O(n) time complexity. I'm pretty certain the NSA (and any other organisations that deal with crypto issues) wouldn't be very happy with that :-) The only way you'll get an O(n) or better way is to

How do I find the sum of prime numbers in a given range in Python 3.5?

隐身守侯 提交于 2019-12-01 12:16:36
问题 I managed to create a list of prime numbers in a given range using this: import numpy as np num = int(input("Enter a number: ")) for a in range(2,num+1): maxInt=int(np.sqrt(a)) + 1 for i in range(2,maxInt): if (a%i==0): break else: print (a) I want to now find the sum of all of the prime numbers in the range so I just put down print (sum(a)) But when trying to do that, I get the following traceback: Traceback (most recent call last): File "C:/Users/Jason/PycharmProjects/stackidiots/scipuy.py"

prime number summing still slow after using sieve

陌路散爱 提交于 2019-12-01 11:04:32
问题 I had a go at a project euler coding challenge below, the answer given by the code is correct but I do not understand why its taking nearly a minute to run. It was finishing with similar times prior to using a sieve. Others users are reporting times as low as milliseconds. I assume I am making a basic error somewhere... // The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. // Find the sum of all the primes below two million. public static long Ex010() { var sum = 0L; var sieve = new bool

Sieve of Eratosthenes using precalculated primes

别等时光非礼了梦想. 提交于 2019-12-01 10:46:56
I've all prime numbers that can be stored in 32bit unsigned int and I want to use them to generate some 64bit prime numbers . using trial division is too slow even with optimizations in logic and compilation. I'm trying to modify Sieve of Eratosthenes to work with the predefined list, as follow: in array A from 2 to 4294967291 in array B from 2^32 to X inc by 1 find C which is first multiple of current prime. from C mark and jump by current prime till X. go to 1. The problem is step 3 which use modulus to find the prime multiple, such operation is the reason i didn't use trail division. Is

Haskell: faster summation of primes

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 06:55:24
Disclaimer: I'm working on Euler Problem 9. I'm adding up some pretty large numbers, all the primes from 1 to 2 000 000. Summing those primes takes forever. I'm using the haskell built in function 'sum'. as in: sum listOfPrimes Are there any other faster options? --My prime generator was the slow link in my code. It sounds like your problem is not summing the numbers, but generating them. What is your implementation of listOfPrimes? This paper may be of interest: http://lambda-the-ultimate.org/node/3127 I'm hoping you are using ghc -O2 and not ghci, right? Your problem will be in the

Program to find all primes in a very large given range of integers

限于喜欢 提交于 2019-12-01 06:10:37
i came across this following question on a programming website : Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! Input The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space. I came up with the following solution : import java.util.*; public class PRIME1 { static int numCases; static int left, right; static boolean[] initSieve = new boolean[32000]; static

Haskell style/efficiency

坚强是说给别人听的谎言 提交于 2019-12-01 05:18:51
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 then let g h y = y `mod` x > 0 && h y in x : mkPrimes (f . g) ( f $ g $ const True) xs else mkPrimes f