primes

How can I find prime numbers through bit operations in C++?

若如初见. 提交于 2019-12-05 13:33:29
How can I find prime numbers through bit operations in C++? I think the way to do this is to not think of the bitset as its numerical representation as we normally do, but to think of it at a list of numbers. So the bitset 1111 would represent the numbers 1, 2, 3, and 4. Now if we say that a '1' represents prime and a '0' represents not prime, we can make a sieve as follows. Set all the bits to 1 (I'm going to use 16 bits that represent the integers 1 through 16) 1111 1111 1111 1111 I know that one is not prime, so I'm setting it to zero. 0111 1111 1111 1111 Now, I know that the next '1' I

Decompose integers larger than 100 digits [closed]

本小妞迷上赌 提交于 2019-12-05 12:07:38
X and Y are integers larger than 100 digits. Find the integer P which is within the range [ X , Y [ and that guaranties the "best" prime decomposition (i.e. the decomposition with the most unique prime factors). What I've done is just check the primality and decompose each number in the range and find the number that respects the rule. Is there any other way to do this? An example on small integers Edit: In the above example, 123456 is decomposed to 2^6 * 3^1 * 643^1 , that's 2 * 2 * 2 * 2 * 2 * 2 * 3 * 643 but only 3 unique factors. While the answer, 123690, is decomposed to 6 unique factors

is this primes generator pythonic

拈花ヽ惹草 提交于 2019-12-05 10:48:34
Is the following code for generating primes pythonic? def get_primes(n): primes=[False,False]+[True]*(n-1) next_p=(i for i,j in enumerate(primes) if j) while True: p=next(next_p) yield p primes[p*p::p]=[False]*((n-p*p)//p+1) Note that next(next_p) will eventually throw a StopIteration error which somehow ends the function get_primes. Is that bad? Also note that next_p is a generator which iterates over primes, however primes changes during iteration. Is that bad style? adding the following if statement gets it under 0.25 seconds for the first million primes: if p*p<=n: primes[p*p::p]=[False]*(

Did I just prove that sieve of Eratosthenes is less efficient than trial division?

人走茶凉 提交于 2019-12-05 08:36:00
I was trying to compare the run-time speed of two algorithms: A brute-force C program to print prime numbers (10,000 numbers), and a Sieve of Eratosthenes C program (also 10,000 prime numbers). My measured run-time for the sieve algorithm was: 0.744 seconds My measured run-time for the brute-force algorithm was: 0.262 seconds However, I was told that the Sieve of Eratosthenes algorithm is more efficient than the brute-force method, and so I thought it would run faster . So either I'm wrong or my program is flawed (which I doubt). Therefore, my question is: Since I got the opposite results than

Prime generating number finder not producing correct output

耗尽温柔 提交于 2019-12-05 08:13:52
问题 I'm working on this problem: Consider the divisors of 30: 1,2,3,5,6,10,15,30. It can be seen that for every divisor d of 30, d+30/d is prime. Find the sum of all positive integers n not exceeding 100 000 000 such that for every divisor d of n, d+n/d is prime. and I thought for sure I had it, but alas, it's apparently giving me the wrong answer ( 12094504411074 ). I am fairly sure my sieve of Eratosthenes is working (but maybe not), so I think the problem is somewhere in my algorithm. It seems

CUDA - Sieve of Eratosthenes division into parts

空扰寡人 提交于 2019-12-05 05:42:57
问题 I'm writing implementation of Sieve of Eratosthenes (https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) on GPU. But no sth like this - http://developer-resource.blogspot.com/2008/07/cuda-sieve-of-eratosthenes.html Method: Creating n-element array with default values 0/1 (0 - prime, 1 - no) and passing it on GPU (I know that it can be done directly in kernel but it's not problem in this moment). Each thread in block checks multiples of a single number. Each block checks in total sqrt(n)

Generate Random Prime number in C/C++ between 2 limits

早过忘川 提交于 2019-12-05 05:29:12
Is there a built in function that can generate a random prime number between 2 given limits in C/C++? I want a function that can generate a random prime number between 1 million and 1 billion You can do this efficiently like so: Generate a random number in that interval; Check if it is divisible by any of the first few primes (say 2 .. 17 , experiment for best results). If yes, go to 1; Use Miller-Rabin to test for primality. Also see this for a similar, a bit more complex, idea. When I had to do this I created a function called isPrime(). isPrime() would check and determine if a number was

Java implementation of Sieve of Eratosthenes that can go past n = 2^32?

我们两清 提交于 2019-12-05 04:18:01
Currently I have this prime generator that is limited to n < 2^32-1. I'm not entirely sure how I could expand the limit further, given the limit of elements in an array. Sieve: public class Main { public static void main(String args[]){ long N = 2000000000; // initially assume all integers are prime boolean[] isPrime = new boolean[N + 1]; for (int i = 2; i <= N; i++) { isPrime[i] = true; } // mark non-primes <= N using Sieve of Eratosthenes for (int i = 2; i*i <= N; i++) { // if i is prime, then mark multiples of i as nonprime // suffices to consider mutiples i, i+1, ..., N/i if (isPrime[i]) {

I have a new algorithm to find factors or primes in linear time - need verification for this

旧城冷巷雨未停 提交于 2019-12-05 03:00:55
I have developed an algorithm to find factors of a given number. Thus it also helps in finding if the given number is a prime number. I feel this is the fastest algorithm for finding factors or prime numbers. This algorithm finds if a give number is prime in time frame of 5*N (where N is the input number). So I hope I can call this a linear time algorithm. How can I verify if this is the fastest algorithm available? Can anybody help in this matter? (faster than GNFS and others known) Algorithm is given below Input: A Number (whose factors is to be found) Output: The two factor of the Number.

How to find out if two numbers are relatively prime?

混江龙づ霸主 提交于 2019-12-05 01:10:44
I'm trying to write a method that will calculate if two numbers are relatively prime for an assignment. I'm primarily looking for answers on where to start. I know there is a method gcd() that will do a lot of it for me, but the assignment is pretty much making me do it without gcd or arrays. I kind of have it started, because I know that I will have to use the % operator in a for loop. public static boolean relativeNumber(int input4, int input5){ for(int i = 1; i <= input4; i++) Obviously this method is only going to return true or false because the main function is only going to print a