primes

Modifying the Prime Number Sieve in C

巧了我就是萌 提交于 2019-12-12 18:27:10
问题 There are many implementations of the Sieve of Eratosthenes online. Through searching Google, I found this implementation in C. #include <stdio.h> #include <stdlib.h> #define limit 100 /*size of integers array*/ int main(){ unsigned long long int i,j; int *primes; int z = 1; primes = malloc(sizeof(int) * limit); for (i = 2;i < limit; i++) primes[i] = 1; for (i = 2;i < limit; i++) if (primes[i]) for (j = i;i * j < limit; j++) primes[i * j] = 0; printf("\nPrime numbers in range 1 to 100 are: \n

How does segmentation improve the running time of Sieve of Eratosthenes?

て烟熏妆下的殇ゞ 提交于 2019-12-12 16:58:01
问题 I came across a segmented implementation of sieve of Eratosthenes which promises to run many times faster than the conventional version. Can someone please explain how segmentation improves the running time? Note that I want to find prime numbers in [1,b] . It works on this idea: (for finding prime numbers till 10^9) We first generate the sieving primes below sqrt(10^9) which are needed to cross-off multiples. Then we start crossing-off the multiples of the first prime 2 until we reach a

Python while loop for finding prime numbers

﹥>﹥吖頭↗ 提交于 2019-12-12 14:04:01
问题 As a first exercise with Python, I'm trying to write a program using loops to find primes. Everything works with a for loop so I am trying to use a while loop. This works but the program returns a few incorrect numbers. import math # looking for all primes below this number max_num = int(input("max number?: ")) primes = [2] # start with 2 test_num = 3 # which means testing starts with 3 while test_num < max_num: i = 0 # It's only necessary to check with the primes smaller than the square #

I've heard that some “break”s aren't bad practice. What about this one?

∥☆過路亽.° 提交于 2019-12-12 10:39:07
问题 I have often heard that using break s in Java is considered bad practice, but after reading some threads on Stack Overflow, I've seen otherwise. Many say that it is acceptable in certain cases. I'm a little confused as to what is/isn't bad practice in this case. For Project Euler: Problem 7, I've constructed the code below. The challenge was to find the 10001st prime. int index = 2, count = 1, numPrime = 1; while (true) { index++; if (isPrime(index)) { count = index; numPrime++; } if

Project Euler #3, infinite loop on factorization

限于喜欢 提交于 2019-12-12 10:19:53
问题 So I'm doing Project Euler because dear gods do I need to practice writing code, and also my math skills are rusty as something very rusty. Thusly; Project Euler. I'm sure most here have already seen or heard of the problem, but I'll put it here just for completeness: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? For this, I've written two functions: from math import sqrt def isprime(n): if n == 1: return False elif n == 2:

Why .Net dictionaries resize to prime numbers?

删除回忆录丶 提交于 2019-12-12 09:33:24
问题 According to this question a .Net dictionary resizes its allocated space to prime numbers that are at least twice the current size. Why is it important to use prime numbers and not just twice the current size? (I tried to use my google-fu powers to find an answer, but to no avail) 回答1: It is an algorithm implementation detail related to choosing a good hashing function and which provides uniform distribution. A non-uniform distribution increases the number of collisions, and the cost of

How do I find the nearest prime number?

廉价感情. 提交于 2019-12-12 07:44:38
问题 Is there any nice algorithm to find the nearest prime number to a given real number? I only need to search within the first 100 primes or so. At present, I've a bunch of prime numbers stored in an array and I'm checking the difference one number at a time (O(n)?). 回答1: Rather than a sorted list of primes, given the relatively small range targetted, have an array indexed by all the odd numbers in the range (you know there are no even primes except the special case of 2) and containing the

Working with large primes in Python [closed]

╄→尐↘猪︶ㄣ 提交于 2019-12-12 07:38:37
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . What is an efficient way for working with large prime numbers with Python? You search on here or on google, and you find many different methods for doing so... sieves, primality test algorithms... Which ways work for larger primes? 回答1: For determining if a number is a prime,

Project Euler #10 Sum of Primes

江枫思渺然 提交于 2019-12-12 06:38:52
问题 I am working on Project Euler's project #10 in which I am asked to find the sum of all primes below 2,000,000. For some reason, I cannot get my code to work. I am fairly certain I am not understanding which data types to use, but neither int, long, or long long seems to be working. Any help or advice would be greatly appreciated. Here is my code. Thanks. int main(int argc, char *argv[]) { int primes[100] = {2, 3, 5, 7}; //array of primes that have been found int fillcell = 4; //cell we are

How to calculate first n prime numbers?

三世轮回 提交于 2019-12-12 05:03:51
问题 Assume the availability of a function is_prime . Assume a variable n has been associated with a positive integer. Write the statements needed to compute the sum of the first n prime numbers. The sum should be associated with the variable total. Note: is_prime takes an integer as a parameter and returns True if and only if that integer is prime. Well, I wrote is_prime function like this: def is_prime(n): n = abs(n) i = 2 while i < n: if n % i == 0: return False i += 1 return True but it works