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

Find maximum occurrence digit in a range of prime numbers

Given two number a and b (1 <= a <= b <= 10^6). Find most frequent digit among all prime numbers between a and b inclusive. if frequency is same print highest digit. Example: from 1 to 20, prime numbers are - 2, 3, 5, 7, 11, 13, 17, 19. Here 2, 5, 9…
coderx
  • 509
  • 6
  • 22
1
vote
1 answer

What's the runtime of my Sieve of Eratosthenes (finding primes) and how to optimize it?

I decided to try out Sieve of Eratosthenes to find the list of primes less or equal to N. Here is my solution: def findPrimeN(num): result = [] multiples = [] for i in range(2, num+1): if i not in multiples: …
return 0
  • 4,226
  • 6
  • 47
  • 72
1
vote
1 answer

Clojure Lazy Seq Results in Stack Overflow

I want to compute a lazy sequence of primes. Here is the interface: user=> (take 10 primes) (2 3 5 7 11 13 17 19 23 29) So far, so good. However, when I take 500 primes, this results in a stack overflow. core.clj: 133 …
Charles R
  • 17,989
  • 6
  • 20
  • 18
1
vote
6 answers

program to print series of prime numbers using java

This code is to print the series of prime number up to given limit but when I am trying to execute this,it goes into infinite loop. import java.io.*; class a { public static void main(String s[]) throws IOException { int count=1; …
1
vote
0 answers

Why am i getting NZEC error on codechef and spoj for the segmented sieve of eratosthenes while it runs perfectly fine on my computer?

I am trying to solve prime generator problem on spoj but i got run time error. link:http://www.spoj.com/problems/PRIME1/ The code goes as below def sieve(n): from math import sqrt x=int(sqrt(n)) numbers=[None]*x for i in range(2,x): …
Goku
  • 11
  • 1
1
vote
1 answer

Primitive root of a number

I have tried to implement the algorithm described in here to find primitive roots for a prime number. It works for small prime numbers, however as I try big numbers, it doesn't return correct answers anymore. I then notice that a^(p-1)/pi tends to…
dede9714
  • 73
  • 2
  • 13
1
vote
0 answers

Find the nth Fibonacci prime in the shortest code in PHP

I was asked this question in a recent interview. I was able to answer but the interviewer was not impressed at all and moved to the next question. So I need to find the nth Fibonacci prime number. This is my understanding: we generate a fibonacci…
Wild Widow
  • 2,359
  • 3
  • 22
  • 34
1
vote
2 answers

Subtracting the Nearest Prime Given Two Lists using for loop and max

I have two lists: (1) of primes (prime_list), and (2) of odd numbers (odd_list). I would like to find the highest prime under each odd number, but I am having some difficultly. For example, for odd number 99 I want to subtract 97. I have been trying…
Matty
  • 277
  • 5
  • 17
1
vote
1 answer

Python sqrt() doubles runtime?

I've recently started learning Python. My apologies if this is really obvious. I am following along with the 2008 MIT open course on Computer Science and am working on the problem of calculating the 1000th prime integer. Python 2.7.3, Win7 lappy…
MmmHmm
  • 3,435
  • 2
  • 27
  • 49
1
vote
3 answers

The product of two large prime numbers in encryption

Ok, so I understand the importance of using the product of two large primes, but why not use the product of three large primes instead? Would this make the encryption weaker in some way? If the answer is not a simple one, then I would appreciate a…
Euthenia
  • 111
  • 2
1
vote
2 answers

C# - Need help creating program that simulates Prime Number behavior by drawing lines

I challenged myself to create a program to experiment with Prime Numbers, I already have an idea how to do it, but not the coding skills.. My plan is this: First ill create a program in C# that makes straight lines that follow some rules: rule 1:…
Bearnnitr
  • 13
  • 5
1
vote
3 answers

Runtime error in PrimeNumber Generator on SPOJ

I was trying to solve the PrimeNumber Generator problem on SPOJ. The code always giving runtime error on SPOJ, but in ideone.com it is working fine. Don't know what is causing the issue. I tried both Scanner and BufferedReader for reading inputs.…
Sarath S Nair
  • 603
  • 2
  • 14
  • 29
1
vote
3 answers

Determine a prime number with right fold

I found this solution on the internet and I need some help understanding it: isPrime' :: Integer -> Bool isPrime' n = foldr (\x acc -> (n `rem` x) /= 0 && acc) True primes where primes = 2 : filter isPrime' [3,5..] A couple of things: My…
dopatraman
  • 13,416
  • 29
  • 90
  • 154
1
vote
1 answer

Another Pollard Rho Implementation

In an attempt to solve the 3rd problem on project Euler (https://projecteuler.net/problem=3), I decided to implement Pollard's Rho algorithm (at least part of it, I'm planning on including the cycling later). The odd thing is that it works for…
Galactasm
  • 97
  • 9
1
vote
1 answer

Given a number n, return true is all the factors of n are prime numbers. Note that 1 and the number itself are not considered as factors

public class AllFactorsArePrime { public static void main(String[] args) { AllFactorsArePrime obj = new AllFactorsArePrime(); boolean result = obj.areAllFactorsPrime(8); System.out.println(result); } public boolean…
nisha
  • 87
  • 6