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
2 answers

Haskell Sieve of Eratosthenes with list of composites

I have to implement the classic problem of the Sieve of Eratosthenes in Haskell for a project. Rather than computing each prime I only have to compare numbers between lists. For instance I pass a list of potential primes (parameter 1) and a list of…
1
vote
1 answer

Primes generating perl

I'm trying to generate random prime number print Math::Prime::Util->random_strong_prime(128); But, when I call one of the methods (i tried various) of Math::Prime::Util, I get: Parameter 'Math::Prime::Util' must be a positive integer at…
Ivan
  • 325
  • 3
  • 9
1
vote
2 answers

for loop unintentionally breaking at if statement

I am trying to determine the largest prime factor of a number by working backwards from the largest possible factor. Once a factor is found, I test to see if it is prime by using the PrimeTest3 function within the original function. However, it is…
1
vote
5 answers

How does this program for finding prime numbers work?

I had to write a simple program to print the prime numbers from 2 until 100. First I did some research about what a prime number is. I tried for a long time, and finally i looked up the answer in the book, sinceIi did not succeed every time writing…
Shibito
  • 11
  • 3
1
vote
6 answers

My while loop goes into infinite loop, what's wrong?

I tried to write the BiggerThanPrime program which allows the user to give an input p and the program can find the next closest prime(n) to it such that the value of (n-p) is minimum. Here is my code: static boolean Prime (int n){ boolean…
meowthecat
  • 51
  • 8
1
vote
1 answer

Massive for loop taking too long to load

I'm working on a program designed to generate prime numbers, and I'm trying to find the 10001st prime number. Each time the loop runs, it tests variable i to see if it's divisible by variable j, which is part of another loop that goes up to half of…
K. Michael
  • 75
  • 1
  • 9
1
vote
1 answer

Bits not being set for a number golang

I am trying to solve project euler problem 3 in golang: The problem is as follows: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? I am trying to solve it as follows: package main import…
sc_ray
  • 7,803
  • 11
  • 63
  • 100
1
vote
2 answers

Android Studio (Java): Continue prime number for loop on button press

I want to be able to display the next prime number each time the button is clicked but cannot find a way for it to work. Anybody help please? public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle…
Oyee
  • 41
  • 3
  • 10
1
vote
3 answers

Odd math error in Java

I'm writing a program to demonstrate Miller-Rabin probability testing in Java. The code is pretty much done... import java.util.Random; import java.util.Scanner; /** * Program to demonstrate Miller-Rabin primality testing * * @author Nick…
Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90
1
vote
2 answers

Improving algorithm execution time and prime numbers

I am trying to find the largest prime value of a big number, but it takes far too long. For example, to find the largest prime of 999999999, it takes about 55 seconds. How can I improve this? require 'prime' def highestprime num i = 1 counter…
RubyUser
  • 29
  • 7
1
vote
4 answers

Perfect Numbers and Mersenne Primes - Python

I've written a program which works out the even perfect numbers for all Mersenne Primes from 1-1000, by using ((2^n)-1)(2^(n-1)) where n is a Mersenne Prime number. This is the program: def PrimeFinder(PotPrime): PlaceNum=1 for x in range…
1
vote
1 answer

Finding number of divisors of a big integer using prime/quadratic factorization (C#)

I'm trying to get the number of divisors of a 64 bit integer (larger than 32 bit) My first method (for small numbers) was to divide the number until the resulting number was 1, count the number of matching primes and use the formula (1 + P1)(1+…
Bartemis
  • 13
  • 3
1
vote
3 answers

Next prime number algorithm

i want to know if there is a easy way to find the prime number next to X. For example, if X=2, the next prime will be 3. The algorithm that i have would be ok, if i wanted to know little numbers but i want to calculate like X=3 million. I found a…
Pedro Lino
  • 601
  • 1
  • 9
  • 18
1
vote
3 answers

Vector addition of elements behaving weird

Before I go into the problem description, note that the datatype 'factrep' is merely a typedef of vector. The problem Given two factrep objects f1 & f2, passed as arguments into the factrep function mult, I expect the returned factrep object…
Syncretic
  • 75
  • 10
1
vote
2 answers

Function to return prime numbers

I want to write a function in R which accepts a list of integers and returns only the values which are prime. So far I have this: primefindlist<-function(n){ return(n[n==2 | all(n %% seq(2,ceiling(sqrt(n)),by=1) !=0)]) } But I keep getting an…
Namch96
  • 187
  • 6