primes

Merge of lazy streams (using generators) in Python

喜夏-厌秋 提交于 2019-11-29 16:33:23
I'm playing with functional capacities of Python 3 and I tried to implement classical algorithm for calculating Hamming numbers. That's the numbers which have as prime factors only 2, 3 or 5. First Hamming numbers are 2, 3, 4, 5, 6, 8, 10, 12, 15, 16, 18, 20 and so on. My implementation is the following: def scale(s, m): return (x*m for x in s) def merge(s1, s2): it1, it2 = iter(s1), iter(s2) x1, x2 = next(it1), next(it2) if x1 < x2: x = x1 it = iter(merge(it1, s2)) elif x1 > x2: x = x2 it = iter(merge(s1, it2)) else: x = x1 it = iter(merge(it1, it2)) yield x while True: yield next(it) def

Why am I getting 'Floating point exception: 8'

六月ゝ 毕业季﹏ 提交于 2019-11-29 15:41:42
I'm trying to calculate all the prime numbers from 0 - 100 and I'm getting a floating point exception, could anyone tell me why? (If it helps I'm using gcc) #include <stdio.h> int main(void) { int nums[100], i; for(i=0;i<100;i++) nums[i] = i; int j,k,l,z; for(i=1;i<100;i++) for(j=2;j<100;j++) if((nums[i] % nums[j]) == 0) { nums[j] = 0; } for(i=0;i<100;i++) if(nums[i] != 0) break; for(z=0;z<100;z++) { for(k=i;k<100;k++) for(l = (k+2);l < 100;l++) if((nums[k] % nums[l]) == 0) nums[k] = 0; } for(i=0;i<100;i++) if(nums[i] != 0) printf("%d,",nums[i]); printf("\n"); return 0; } Well, it's really

Why do two algorithms for finding primes differ in speed so much even though they seem to do the same number of iterations?

老子叫甜甜 提交于 2019-11-29 14:22:40
I have two algorithms of finding primes, in Python. The inner loop of each one seems to be executed the same number of times, and is equally simple. However, one of them takes 10 times as much as the other. My question is: Why? Is this some quirk of Python that can be optimized away (how?), or am I missing something else? The problem I am solving is essentially from http://www.spoj.pl/problems/PRIME1/ . In my case, I have N = 10 ** 9, delta = 10 ** 5, and I want to find all primes between N-delta and delta. I also have smallprimes , a pre-made list of all primes less than or equal to square

Find a largest prime number less than n [closed]

馋奶兔 提交于 2019-11-29 13:05:32
How can I find a largest prime number which is less than n, where n ≤ 10¹⁸ ? Please help me find an Efficient Algorithm. for(j=n;j>=2;j--) { if(j%2 == 1) { double m = double(j); double a = (m-1)/6.0; double b = (m-5)/6.0; if( a-(int)a == 0 || b-(int)b == 0 ) { printf("%llu\n",j); break; } } } I used this approach but this is not efficient to solve for n>10^10; How to optimize this.. Edit: Solution: Use Primality test on each j. Miller Rabin , Fermat's Test . I don't think this question should be so quickly dismissed, as efficiency is not so easy to determine for numbers in this range. First of

advice on how to make my algorithm faster

会有一股神秘感。 提交于 2019-11-29 12:35:35
here is my code in C for problem#3 from project-Euler, where I have to find the largest prime factor of 600851475143. #include <stdio.h> #include <stdlib.h> bool is_prime(long int number){ long int j; for (j=2; j<=number/2; j++){ if (number%j==0) return false; if (j==number/2) return true; } } int main(){ long int input; scanf("%d", &input); long int factor; int ans=0; for (factor=input/2; factor>1; factor--){ if (input%factor==0 && is_prime(factor)) { ans = factor; break; } } printf("%d\n", ans); system("pause"); return 0; } Although it works fine for small numbers, gradually it takes more

Finding the exponent of n = 2**x using bitwise operations [logarithm in base 2 of n]

你。 提交于 2019-11-29 11:55:49
问题 Is there a straightforward way to extracting the exponent from a power of 2 using bitwise operations only? EDIT: Although the question was originally about bitwise operations, the thread is a good read also if you are wondering "What's the fastest way to find X given Y = 2 X in Python ?"** I am currently trying to optimize a routine (Rabin-Miller primality test) that reduces an even number N in the forms 2**s * d . I can get the 2**s part by: two_power_s = N & -N but I can't find a way to

Why doesnt my ruby coding for finding prime numbers work?

北慕城南 提交于 2019-11-29 11:51:44
I am wondering why my code does not work. I am new to the code world so if anyone can break this problem down for me and how would be best to solve it thanks! I am trying to create a program which will indicate the prime numbers from a list of numbers that I specify. Please tell my why these two codes do not work! I am confused as to what the second code is trying to do as I found it as someone else's solution to my problem. I am new to coding but I love it so bear with me! Here is my simple code: def is_prime?(*nums) i = 2 nums.each do |num| while i < num if num % i == 0 puts "#{num} is not a

Sieve of Eratosthenes Scheme

一个人想着一个人 提交于 2019-11-29 11:45:16
I've been searching the web for an implementation of the Sieve of Eratosthenes in scheme and although I came up with a lot of content, none of them seemed to have made it like I need it to be done. The problem is most algorithms either use a static end or use iteration. This paired with my lack of knowledge of the language led me to ask all of you for help. I need an implementation of the Sieve that takes in one argument (number to Sieve until), uses only recursion and has a list of "cons" of a number with a #t (true) or #f (false). So essentially the algorithm would go as such: Make list from

generator in Python generating prime numbers

北城余情 提交于 2019-11-29 11:42:10
I need to generate prime numbers using generator in Python. Here is my code: def genPrimes(): yield 2 x=2 while True: x+=1 for p in genPrimes(): if (x%p)==0: break else: yield x I have a RuntimeError: maximum recursion depth exceeded after the 2nd prime.next() when I run it. The fastest way to generate primes is with a sieve. Here we use a segmented Sieve of Eratosthenes to generate the primes, one by one with no maximum, in order; ps is the list of sieving primes less than the current maximum and qs is the offset of the smallest multiple of the corresponding ps in the current segment. def

Ruby puts not outputting in real time

两盒软妹~` 提交于 2019-11-29 11:31:05
I've started some problems on Project Euler . One of the questions: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? I have some code written up...and it works: class Integer def primeFactors load('/home/arseno/ruby/lib/prime.rb') a = [] for i in (1..self) div = self.to_f/i.to_f if((div==div.to_i)&&(Prime.prime?(i))) a << i end end a end end puts 13195.primeFactors Outputs: 5 7 13 29 So far so good! Now, when I put in 600851475143 instead, my terminal hangs up (rightfully so, it's computing a whole lot of stuff!) So what I attempted