primes

Infinite range in my python prime finder?

可紊 提交于 2020-01-24 06:57:05
问题 I am trying to get an infinite range in my python prime number finder! here is my code! import math print "Welcome to Prime Finder!" option = raw_input("continue(y/n)") if option == "y": for num in range(1,(infinite number)): if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)): print num I am trying to get where it says (infinite number) to actually be an infinite number. Is there some value or something that I can use to find that? Any help would be greatly appreciated! 回答1: You can

strange results when converting floating point numbers to fractions using MASS::fractions in R

浪尽此生 提交于 2020-01-23 12:07:16
问题 I am trying to do something similar to what is discussed in this post, but in R rather than Python. However: require(MASS) fractions(0.723618,max.denominator = 1000000) #[1] 89/123 This seems to indicate that floating point number 0.723618 is better described by fraction 89/123 than by 361809/500000, which does not seem correct to me. Even more puzzling: fractions(0.7236,max.denominator = 100000000000) #[1] 89/123 Surely it would be better to write 0.7236 as 1809/5000, wouldn't it? Do you

Optimize Haskell code calculating the sum of all the primes below two million

倖福魔咒の 提交于 2020-01-23 09:20:22
问题 Problem 10 in Project Euler. I saw some discussion there but only for C. I used the following code to calculate: print . sum . sieve $ [2..2000000] where sieve [] = [] sieve (x:xs) = x : sieve (filter ((/= 0) . (`mod` x)) xs) It takes ages to calculate. I am wondering if there is any more efficient way to calculate it? 回答1: Many really fast ways of calculating prime numbers in haskell is described on the haskellwiki page for Prime numbers. Specifically the second one seems to be good enough,

Finding lists of prime numbers with SIMD - SSE/AVX

余生长醉 提交于 2020-01-21 05:26:05
问题 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

Understanding Sieve of Eratosthenes in Python

自作多情 提交于 2020-01-16 03:29:09
问题 I've found an example code in python that gives out all prime numbers upto n but I simply don't get it, Why does it does what it does? I've read the wikipedia article about the Sieve of Eratosthenes but simply have no idea about how this works. pp = 2 ps = [pp] lim = raw_input("Generate prime numbers up to what number? : ") while pp < int(lim): pp += 1 for a in ps: if pp%a==0: break else: ps.append(pp) print set(ps) An explanation of how the loop works would be appreciated. EDIT - Figured out

What is a convenient base for a bignum library & primality testing algorithm?

折月煮酒 提交于 2020-01-15 06:56:46
问题 I am to program the Solovay-Strassen primality test presented in the original paper on RSA. Additionally I will need to write a small bignum library, and so when searching for a convenient representation for bignum I came across this specification: struct { int sign; int size; int *tab; } bignum; I will also be writing a multiplication routine using the Karatsuba method. So, for my question: What base would be convenient to store integer data in the bignum struct? Note: I am not allowed to

numpy fft is fast for lengths that are products of small primes, but how small?

耗尽温柔 提交于 2020-01-15 03:50:11
问题 I've seen several examples showing that if the input length is a product of 2,3,5,7 etc. then numpy's fft implementation is fast. But what is the largest prime number that is still considered "small" here? 回答1: Note that scipy's FFT has radices of 2, 3, 4, and 5 (reference) . I assume numpy may have a similar implementation, which would make 5 the largest efficient prime factor in FFT lengths. Empirically, the largest prime I'd consider "small" for the purpose of FFT performance is 11. But

Sum of all primes under 2 million

微笑、不失礼 提交于 2020-01-13 15:30:13
问题 I made a program that returns the sum of all primes under 2 million. I really have no idea what's going on with this one, I get 142891895587 as my answer when the correct answer is 142913828922. It seems like its missing a few primes in there. I'm pretty sure the getPrime function works as it is supposed to. I used it a couple times before and worked correctly than. The code is as follows: vector<int> getPrimes(int number); int main() { unsigned long int sum = 0; vector<int> primes =

DSA: How to generate the subprime?

社会主义新天地 提交于 2020-01-13 07:19:48
问题 Lately I did a bit of research about the Digital Signature Algorithm and how it works. My question according to this is of no practical matter for me but of pure interest. However, I'm curious how to generate the subprime in DSA: Somewhere during the generation of the parameters for the algorithm one chooses a 1024-bit prime p . The next step is to find a 160-bit prime q which is a divisor of p-1 . That's where I get stuck. I have no idea how to find that subprime q in time, without having to

Super Ugly Number

五迷三道 提交于 2020-01-13 05:25:27
问题 So the problem is: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4. So my algorithm basically finds all possible factors using the pattern they follow, pushes them to an array, sorts that array and then returns the nth value in the