Questions tagged [primes]

Primes or prime numbers are integers greater than 1 which are divisible only by themselves and 1, i.e.: 2, 3, 5, 7, 11, ... .

See Wikipedia on primes.

If your program for checking or computing prime numbers doesn't work, please check the following points before posting a question:

  • If it is too slow, try testing divisibility only up to and including the square root. If you want to compute all primes in a range better use the sieve of Eratosthenes, implemented properly it will be much faster.

  • If your algorithm says square numbers are prime you neglected the and including part above.

  • Check for overflow. n * n overflows a 32 bit signed integer for n > 46340. The sum of the first two million primes for Project Euler problem 10 will also overflow a 32 bit integer.

3346 questions
1
vote
1 answer

OpenMP and Threading

I'm newly introduced into the stage of learning OpenMP, so please bear with me. I have the working codes for getting the prime numbers under the specified number in the argv[1], one using the regular for loop and OpenMP. Here are my following…
user5319150
1
vote
1 answer

Implementing the Sieve of Eratosthenes in Idris

I'm struggling to translate my definition of the Sieve of Eratosthenes into Idris. Here is the function so far: %default total eratos : Nat -> (l : List Nat) -> { auto ok: NonEmpty l } -> List Nat eratos limit (prime :: rest) = if prime * prime >…
Langston
  • 1,083
  • 10
  • 26
1
vote
4 answers

How to generate huge amount of prime numbers in java?

In order to solve a question I have to generate a list of prime numbers from 1 to 3000000, so I tried several ways to do this and unfortunately all failed... First try: because all prime numbers bigger than 2 are odd numbers, so I first generate a…
Tony Chen
  • 183
  • 1
  • 16
1
vote
2 answers

Mathematical algorithm failing but seems correct

I've been given a problem in which a function is fed in A and B. These are target numbers from 1, 1, whereby B may only increase by A and A may only increase by B (Ex, 1 1 -> 2 1 or 1 2. 2 1 -> 3 1 or 2 3. 2 3 -> 5 3 or 2 5). This creates a binary…
1
vote
3 answers

Sieve of eratosthenes not working C++

So I am trying to make a C++ program to generate all prime numbers till a certain point but for some reason, it prints all numbers as non-primes after 2. int A[1000000]; void sieve(int till) { for(int i = 2; i < till; i++) { if(A[i]…
Komninos
  • 96
  • 1
  • 1
  • 10
1
vote
3 answers

Sum of Numbers as Distinct Primes

//List Style using System; using System.Collections.Generic; using System.Linq; public class pr{ static public void Main (){ int n, i, j, k, l, sum,flag = 0; //int sum = i+j; //int k = (n-i); …
user4077377
1
vote
2 answers

How to find the maximum product of two prime numbers in an array?

#include int final[3]; int checkprime(int n,int loopcount) { int flag=0,m; for(m=2; m<=loopcount; m++) { if(n%m==0 && n!=2) { flag=1; return 0; break; } else…
user123
  • 271
  • 1
  • 10
1
vote
1 answer

to find sum of primes up to 2 million

I am trying to find the sum of all prime numbers <=2 million so i used sieve of Eratosthenes TO mark all prime numbers .As i declared the boolean array of size 2 million ,i got this error "main" java.lang.ArrayIndexOutOfBoundsException:…
1
vote
1 answer

Project Euler #50 - Consecutive prime sum - Ruby

I'm trying to solve Project Euler problem #50 (https://projecteuler.net/problem=50) where the problem is defined as: Which prime, below one-million, can be written as the sum of the most consecutive primes? I've come up with two different…
Ciaran
  • 151
  • 8
1
vote
2 answers

given prime factorization of a number iterate through all factors in c++ without recursion

I am given the prime factorization of a number p1^x1 * p2^x2 * .... in a map. I need to iterate through all its factors, prime as well as composite. I managed to write a solution using recursion. #include #include #include…
xylon97
  • 153
  • 5
1
vote
1 answer

Getting an incorrect answer with a mercende prime number generator

all! recently i've been trying to build a mercende prime number producer/generator, using the lucas lehmer method of testing. the code works for the first 4 numbers, and then fails for the rest. any suggestions? thanks! var totalPrimes =…
A. Eglin
  • 11
  • 4
1
vote
2 answers

What is wrong with this prime factorization code in Java?

I was trying to make a method to do the prime factorization of a number, and it's probably not the most efficient, but I don't see why it shouldn't work. public static ArrayList primeFactorize(int num) { ArrayList primeFactors…
gcpreston
  • 19
  • 8
1
vote
3 answers

Sieve of Eratosthenes Optimization

I've made my own program that finds primes from 2 - n, using the Sieve of Eratosthenes. Is there any way I can implement a more efficient way to remove composite numbers? Link to project: https://github.com/Gurran/Sieve-of-Eratosthenes class…
Gustav Blomqvist
  • 161
  • 1
  • 12
1
vote
7 answers

My Sieve of Eratosthenes takes too long

I have implemented Sieve of Eratosthenes to solve the SPOJ problem PRIME1. Though the output is fine, my submission exceeds the time limit. How can I reduce the run time? int main() { vector prime_list; prime_list.push_back(2); …
1
vote
2 answers

Trying to find the 1000th prime number

I am trying to write a script in python to find the 1000th prime number. I don't understand why this isn't working here. Basically while the mod is less than the square root of the number and still has a remainder, the mod goes up by one. This…