primes

Why am I getting 'Floating point exception: 8'

痴心易碎 提交于 2019-11-28 09:53:10
问题 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

Scala, Erastothenes: Is there a straightforward way to replace a stream with an iteration?

一曲冷凌霜 提交于 2019-11-28 09:18:14
I wrote a function that generates primes indefinitely (wikipedia: incremental sieve of Erastothenes ) usings streams. It returns a stream, but it also merges streams of prime multiples internally to mark upcoming composites. The definition is concise, functional, elegant and easy to understand, if I do say so myself: def primes(): Stream[Int] = { def merge(a: Stream[Int], b: Stream[Int]): Stream[Int] = { def next = a.head min b.head Stream.cons(next, merge(if (a.head == next) a.tail else a, if (b.head == next) b.tail else b)) } def test(n: Int, compositeStream: Stream[Int]): Stream[Int] = { if

Concurrent Prime Generator

落花浮王杯 提交于 2019-11-28 07:53:55
问题 I'm going through the problems on projecteuler.net to learn how to program in Erlang, and I am having the hardest time creating a prime generator that can create all of the primes below 2 million, in less than a minute. Using the sequential style, I have already written three types of generators, including the Sieve of Eratosthenes, and none of them perform well enough. I figured a concurrent Sieve would work great, but I'm getting bad_arity messages, and I'm not sure why. Any suggestions on

Improving pure Python prime sieve by recurrence formula

别说谁变了你拦得住时间么 提交于 2019-11-28 06:56:11
I am trying to optimize further the champion solution in prime number thread by taking out the complex formula for sub-list length. len() of the same subsequence is too slow as len is expensive and generating the subsequence is expensive. This looks to slightly speed up the function but I could not yet take away the division, though I do the division only inside the condition statement. Of course I could try to simplify the length calculation by taking out the optimization of starting marking for n instead of n*n... I replaced division / by integer division // to be compatible with Python 3 or

advice on how to make my algorithm faster

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 06:35:04
问题 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",

Project Euler Question 3 Help

↘锁芯ラ 提交于 2019-11-28 05:30:30
I'm trying to work through Project Euler and I'm hitting a barrier on problem 03. I have an algorithm that works for smaller numbers, but problem 3 uses a very, very large number. Problem 03: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? Here is my solution in C# and it's been running for I think close to an hour. I'm not looking for an answer because I do actually want to solve this myself. Mainly just looking for some help. static void Main(string[] args) { const long n = 600851475143; //const long n = 13195; long count, half,

Why doesnt my ruby coding for finding prime numbers work?

試著忘記壹切 提交于 2019-11-28 05:28:27
问题 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

generator in Python generating prime numbers

ぃ、小莉子 提交于 2019-11-28 05:22:39
问题 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. 回答1: 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

Ruby puts not outputting in real time

孤人 提交于 2019-11-28 05:20:04
问题 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

How to determine if a number is prime

こ雲淡風輕ζ 提交于 2019-11-28 05:16:18
问题 Okay my issue is less of how to figure out if a number is prime, because I think I figured that out, but more of how to get it to display properly. Here's my code: public static void main(String[] args) { // Declare Variables int randomNumbers = 0; int sum = 0; //Loop for number generation and print out numbers System.out.print("The five random numbers are: "); for (int i = 0; i <= 4; i++) { randomNumbers = (int)(Math.random()*20); sum += randomNumbers; if (i == 4) { System.out.println("and "