primes

Generate a random big prime number with forge (or another JavaScript approach)

点点圈 提交于 2019-12-10 13:48:01
问题 I need to generate a random big (around 4096 bit) prime number in JavaScript and I'm already using forge. Forge has to have some kind of generator for such tasks as it implements RSA which also relies on random prime numbers. However I haven't found something in the documentation of forge when you just want to get a random prime number (something like var myRandomPrime = forge.random.getPrime(4096); would have been great). So what would be the best approach to get such a prime (with or

Prime Iterator in Julia

て烟熏妆下的殇ゞ 提交于 2019-12-10 13:45:44
问题 Is there an (efficient) iterator to generate prime numbers in Julia? The inbuilt function primes[N] generates all primes up to N at once, rather than as required, and may not be usable when N is very large, or unknown. 回答1: You can filter a counter going through the (big) ints (the Base.Count{BigInt} iterator) using the probabilistic primality test iterprimes = filter(isprime,countfrom(big(2),1)) Then for example julia> collect(take(iterprimes, 5)) 5-element Array{Any,1}: 2 3 5 7 11 This is

Haskell: where is Data.Numbers.Primes library?

冷暖自知 提交于 2019-12-10 13:27:51
问题 I tried importing Data.Numbers.Primes import Data.Numbers.Primes runhaskell gave me: 5.hs:1:8: Could not find module `Data.Numbers.Primes' Use -v to see a list of the files searched for. ghci gave me: <no location info>: Could not find module `Data.Numbers.Primes' It is not a module in the current program, or in any known package. I tried to install Data.Numbers.Primes through cabal, but I got: cabal update ... cabal install Data cabal: There is no package named 'Data'. You may need to run

Why is my Sieve of Eratosthenes so slow?

爱⌒轻易说出口 提交于 2019-12-10 13:16:21
问题 Im solving some problems on Project Euler and had to generate 2 million primes to solve a problem. My implementation of the Sieve of Eratosthenes turned out to be EXTREMELY slow but I don't quite know why. Could someone please explain the major problems with this implementation. I thought it was so pretty, and then I figured out it is utterly terrible :(. I found another implementation of it online and it was so much faster than mine. def generatePrimes(upperBound): numbers = range(2

prime number generator in python: accumulation of numbers

和自甴很熟 提交于 2019-12-10 11:54:28
问题 My generator works great: I've tested it many times. Only, there is a problem: as the numbers increase, as one might believe, the program becomes slower. I have thought up of a way to do this, but don't know how, as I have only started with python not too long ago. My generator looks like this: while 0==0: i=input('Enter the number for which all previous shall be tested for primality: ') n=0 a=[0,1,2] while n<=i: n=n+1 for b in range(2,n): if n%b==0: break if b==(n-1): a.append(n) print a` I

Prime Number generator with recursion and list comprehension

这一生的挚爱 提交于 2019-12-10 10:22:50
问题 I am a newbie to Haskell programming and have trouble understanding how the below list comprehension expands. primes = sieve [2..] sieve (p:xs) = p : sieve [x | x <-xs, x `mod` p /= 0] Can someone correct me how the sieve expansion works: As we are pattern matching in sieve , p would associate to 2 and x s from [3..] . Next, in the list comprehension x<-3 , but then how can we call sieve with just 3 when there is no short-circuit. Other thing I do not understand is how recursion works here. I

C++ Sieve of Atkin overlooks a few prime numbers

霸气de小男生 提交于 2019-12-09 19:37:46
问题 Recently I've been working on a C++ prime generator that uses the Sieve of Atkin ( http://en.wikipedia.org/wiki/Sieve_of_atkin ) to generate its primes. My objective is to be able to generate any 32-bit number. I'll use it mostly for project euler problems. mostly it's just a summer project. The program uses a bitboard to store primality: that is, a series of ones and zeros where for example the 11th bit would be a 1, the 12th a 0, and the 13th a 1, etc. For efficient memory usage, this is

How to optimize this Haskell code summing up the primes in sublinear time?

百般思念 提交于 2019-12-09 14:36:38
问题 Problem 10 from Project Euler is to find the sum of all the primes below given n . I solved it simply by summing up the primes generated by the sieve of Eratosthenes. Then I came across much more efficient solution by Lucy_Hedgehog (sub-linear!). For n = 2⋅10^9 : Python code (from the quote above) runs in 1.2 seconds in Python 2.7.3. C++ code (mine) runs in about 0.3 seconds (compiled with g++ 4.8.4). I re-implemented the same algorithm in Haskell, since I'm learning it: import Data.List

CUDA Primes Generation

坚强是说给别人听的谎言 提交于 2019-12-09 13:15:32
问题 My CUDA program stops working(it prints nothing) as data size increases over 260k. Can someone tell me why this is happening? This is my first CUDA program. And if I want bigger primes, how to use datatype larger than long long int on CUDA? The graphics card is GT425M. #include<stdio.h> #include<stdlib.h> #include<cuda.h> #define SIZE 250000 #define BLOCK_NUM 96 #define THREAD_NUM 1024 int data[SIZE]; __global__ static void sieve(int *num,clock_t* time){ const int tid = threadIdx.x; const int

Prime Sieve in Haskell

眉间皱痕 提交于 2019-12-09 05:39:16
问题 I'm very new to Haskell and I'm just trying to find the sum of the first 2 million primes. I'm trying to generate the primes using a sieve (I think the sieve of Eratosthenes?), but it's really really slow and I don't know why. Here is my code. sieve (x:xs) = x:(sieve $ filter (\a -> a `mod` x /= 0) xs) ans = sum $ takeWhile (<2000000) (sieve [2..]) Thanks in advance. 回答1: It is very slow because the algorithm is a trial division that doesn't stop at the square root. If you look closely what