primes

Code to print Nth prime number

▼魔方 西西 提交于 2019-12-24 08:57:52
问题 I have to print the Nth prime number. For example: 1st prime number is 2 2nd prime number is 3 . . 10th prime number is 29 and so on.... My algorithm is as follows: 1) Add 2 and 3 to stack. 2) If n <= Size of stack, get item at position n and output answer 3) Else, start from last element of stack, check if prime 4) To check for prime, divide from each element in stack. If remainder 0 for any element in stack, not a prime. Break loop 5) If Prime, add to stack 6) Search only odd numbers My

algorithm to find products of a set of primes, in order, greater than x

若如初见. 提交于 2019-12-24 08:18:29
问题 Consider the finite set {2,3,5,...,n}. I am interested in primes but the question could apply to any set of numbers. I want to find all possible products of these numbers in ascending order, and in particular greater than or equal to some number x. Does anyone know a nice algorithm for this? EDIT to clarify: Each factor in the input set may be used any number of times. If the input were {2,3,5,7} the output would be {2,3,4,5,6,7,8,9,10,12,14,15,16,18,...}. The algorithm can stop as soon as it

Is this prime generator inefficient C++?

一世执手 提交于 2019-12-24 04:30:10
问题 Is this seen as an in efficient prime number generator. It seems to me that this is pretty efficient. Is it the use of the stream that makes the program run slower? I am trying to submit this to SPOJ and it tells me that my time limit exceeded... #include <iostream> #include <sstream> using namespace std; int main() { int testCases, first, second, counter = 0; bool isPrime = true; stringstream out; cin >> testCases; for (int i = 0; i < testCases; i++) { // get the next two numbers cin >>

Sieve of Eratosthenes - Implementation returning some non-prime values?

旧街凉风 提交于 2019-12-24 03:42:31
问题 I implemented the Sieve of Eratosthenes in Java, from pseudocode: public static void sieveofEratosthenes(int n) { boolean numArray[]; numArray = new boolean[n]; for(int i = 0; i < n; i++) numArray[i] = true; int a = 0; for(int i = 2; i < Math.sqrt((double)n); i++) { if(numArray[i]) { for(int j = (int)Math.pow(i, 2); j < n; a++) { numArray[j] = false; j += (a * i); } } } for(int i = 2; i < n; i++) { if(numArray[i]) System.out.println(i); } } The output it gives me, when i is 15: 2 3 5 7 8 11

Sieve of Eratosthenes - Implementation returning some non-prime values?

こ雲淡風輕ζ 提交于 2019-12-24 03:42:03
问题 I implemented the Sieve of Eratosthenes in Java, from pseudocode: public static void sieveofEratosthenes(int n) { boolean numArray[]; numArray = new boolean[n]; for(int i = 0; i < n; i++) numArray[i] = true; int a = 0; for(int i = 2; i < Math.sqrt((double)n); i++) { if(numArray[i]) { for(int j = (int)Math.pow(i, 2); j < n; a++) { numArray[j] = false; j += (a * i); } } } for(int i = 2; i < n; i++) { if(numArray[i]) System.out.println(i); } } The output it gives me, when i is 15: 2 3 5 7 8 11

Prime factorization for big numbers

做~自己de王妃 提交于 2019-12-23 21:33:34
问题 I am trying to find the complexity of a factorization for big numbers. Which is the best algorithm and which is the complexity of finding a number's prime factors? Assume that the length of the number is n. 回答1: The best know algoritm for factoring integer larger than 100 digits is General number field sieve. It's complexity is explained on the page that the link links to. Wikipedia has a nice article about other algoritms: http://en.wikipedia.org/wiki/Integer_factorization 回答2: the

Checking primality of very large numbers in Python

跟風遠走 提交于 2019-12-23 18:07:29
问题 What would be the fastest way to check if a given large number is prime? I'm talking about numbers the size of around 10^32. I've tried the algorithm from the great answer by @MarcoBonelli which is: from math import sqrt; from itertools import count, islice def isPrime(n): return n > 1 and all(n%i for i in islice(count(2), int(sqrt(n)-1))) but it gives the error of Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize when used against such large numbers. What would be

is Sieve of erathosthens the best algorithm to generate prime numbers from 1 to N?

纵饮孤独 提交于 2019-12-23 15:53:14
问题 I was asked this question in an interview. I implemented an algorithm using sieve of eratosthenes concept and an array. Is there a better way to go about this question For those who dont know the sieve , here is the link: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes EDIT: Best in terms of both time and space complexity. I just told them the flaw of SoE is space complexity. So they asked me if I could do something about it . Here is how the interview went about: 1) Implement a algo that

Calculating Primes and Appending to a List

╄→尐↘猪︶ㄣ 提交于 2019-12-23 09:18:02
问题 I've recently started attempting to solve problems on project Euler using python, and have hit this road bump when trying to calculate primes and append them to a list. I have written the following code, but I'm confused as to why it doesn't output anything when I run it. import math primes = [] def isPrime(i): if number<=1: return False if number==2: return True if number%2==0: return False for i in range(3,int(sqrt(number))+1): if number%i==0: return False return True for i in range (1,

Getting all prime numbers between 2 and 100 in C

若如初见. 提交于 2019-12-23 06:26:44
问题 This is my code which is supposed to output prime numbers only. #include <stdio.h> int prime(int n){ int j; for (j=2;j<=n/2;j++){ if((n%j)==0){ return 0; } else{ return 1; } } } void main(){ int i,p; for (i=2;i<=100;i++){ p=prime(i); if(p==1){ printf("%d \n",i); } } } The result is 2,3,7,9,11,13,15.... not 2,3,5,7,11,13.... What did I do wrong? 回答1: Your probably want: int prime(int n){ int j; for (j=2;j<=n/2;j++) if((n%j)==0) return 0; return 1; } 回答2: Prime Numbers are numbers which are