primes

printing out prime numbers from array

I'd like to print out all prime numbers from an array with method. I can do it with one int but don't know how to return certain numbers from array. Thanks for help! public static boolean isPrime(int [] tab) { boolean prime = true; for (int i = 3; i <= Math.sqrt(tab[i]); i += 2) if (tab[i] % i == 0) { ...

Find out 20th, 30th, nth prime number. (I'm getting 20th but not 30th?) [Python]

The question is to find the 1000th prime number. I wrote the following python code for this. The problem is, I get the right answer for the 10th , 20th prime but after that each increment of 10 leaves me one off the mark. I can't catch the bug here :( count=1 #to keep count of prime numbers primes=() #tuple to hold p...

Fastest way to list all primes below N in python

This is the best algorithm I could come up with after struggling with a couple of Project Euler's questions. def get_primes(n): numbers = set(range(n, 1, -1)) primes = [] while numbers: p = numbers.pop() primes.append(p) numbers.difference_update(set(range(p*2, n+1, p))) return primes >>> timeit....

Can you tell me why this generates time limit exceeded in spoj(Prime Number Generator)

#include<iostream> #include<string.h> #include<math.h> using namespace std; bool prime[1000000500]; void generate(long long end) { memset(prime,true,sizeof(prime)); prime[0]=false; prime[1]=false; for(long long i=0;i<=sqrt(end);i++) { if(prime[i]==true) { for(long long y=i*...

Determine primality

put "enter a number to determine if it is or is not prime" get primenum % for i : 1 .. primenum by 1 % end for if (primenum / primenum) = 1 or primenum / 1 = 0 then put primenum, " is a prime number" else put primenum, " is not a prime number" end if Output says that 12 is a prime number, and that's wrong. How can I fix this c...

C++ Sieve of Atkin overlooks a few prime numbers

Hello All! Recently I've been working on a C++ prime generator that uses the Sieve of Atkin ( http://en.wikipedia.org/wiki/Sieve_of_atkin ) to generate its primes. My objective is to be able to generate any 32-bit number. I'll use it mostly for project euler problems. mostly it's just a summer project. The program uses a bitboard to...

java.util.concurrent : calculating primes

Hi all, I'm new to the package java.util.concurrent. I've created the following program testing if a number is a prime using a multi and a monothreaded strategy. import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class IsPrime implements Runnable { private static final long UPPER...

How to implement an efficient infinite generator of prime numbers in Python?

This is not a homework, I am just curious. INFINITE is the key word here. I wish to use it as for p in primes(). I believe that this is a built-in function in Haskell. So, the answer cannot be as naive as "Just do a Sieve". First of all, you do not know how many consecutive primes will be consumed. Well, suppose you could concoct 100...

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

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 (Rab...

What is the shortest way to calculate the nth prime?

What is the shortest C code to "Calculate the n-th prime"? Shortest in terms of significant characters, i.e. the number of semicolons, non-whitespace characters, keywords and commas. Input: Integers n from the standard input, separated by new lines. The input will be terminated by EOF. Output: Right after the input n, print the n-th...

For a given number N i have to find all the prime numbers it's consisting of

Need a suggestion for an algorithm. For a given number N i have to find all the prime numbers it's consisting of, like this: N = 49 49 = 7 ^ 2 N = 168 168 = (2 ^ 3) * (3 ^ 1) * (7 ^ 1) If you want to help me even more you can write the algo in c++. Thanks. ...

What is causing a segmentation fault?

I have been attempting to write a program that will determine if a number is prime or not. I have based it off of the Sieve of Eratosthenes. Anyway, my program works for small numbers (15485863 works), but if I use large numbers (ex. 17485863) I receive a segmentation fault. I am using unsigned long longs and do not think I have surpasse...

Assistance finding prime numbers for cryptosystem

I am a student in college and have an assignment which requires finding large prime numbers. I was given the following "simple" algorithm by the professor to find 2 likely prime numbers. generate random a and p where 1 < a < p confirm that gcd(a,p) is = 1 -- this is suppose to remove Carmichael numbers Edit(meant equal to 1) perform "m...

Most Elegant Way to write isPrime in java

public class Prime { public static boolean isPrime1(int n) { if (n <= 1) { return false; } if (n == 2) { return true; } for (int i = 2; i <= Math.sqrt(n) + 1; i++) { if (n % i == 0) { return false; ...

Why is my implementation of the Sieve of Atkin overlooking numbers close to the specified limit?

My implementation of Sieve of Atkin either overlooks primes near the limit or composites near the limit. while some limits work and others don't. I'm am completely confused as to what is wrong. def AtkinSieve (limit): results = [2,3,5] sieve = [False]*limit factor = int(math.sqrt(lim)) for i in range(1,factor): for j in range(1, fac...

fastest calculation of largest prime factor of 512 bit number in python

dear all, i am simulating my crypto scheme in python, i am a new user to it. p = 512 bit number and i need to calculate largest prime factor for it, i am looking for two things: Fastest code to process this large prime factorization Code that can take 512 bit of number as input and can handle it. I have seen different implementatio...

How do you generate a user defined amount prime numbers in c#?

I'm trying to generate prime numbers based on user input. this is what i have so far but i just can't seem to figure it out: Console.Write("Please enter the number of prime numbers you would like to see:"); int numberOfPrimes = Convert.ToInt32(Console.ReadLine()); for (int x = 0; x < numberOfPrimes; x++) { ...

Finding a prime number after a given number

How can I find the least prime number greater than a given number? For example, given 4, I need 5; given 7, I need 11. I would like to know some ideas on best algorithms to do this. One method that I thought of was generate primes numbers through the Sieve of Eratosthenes, and then find the prime after the given number. ...

Trying to calculate the 10001st prime number in Java.

Greetings. I am doing Problem 7 from Project Euler. What I am supposed to do is calculate the 10001st prime number. (A prime number being a number that is only divisible by itself and one.) Here is my current program: public class Problem7 { public static void main(String args []){ long numberOfPrimes = 0; long number = 2; ...

Enumerating large (20-digit) [probable] prime numbers

Given A, on the order of 10^20, I'd like to quickly obtain a list of the first few prime numbers greater than A. OK, my needs aren't quite that exact - it's alright if occasionally a composite number ends up on the list. What's the fastest way to enumerate the (probable) primes greater than A? Is there a quicker way than stepping throu...