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

Creating a list of Primes and printing out the list in Python 3.6.1

I am trying to learn Python, but I'm still quite new at it. I am attempting to create a list of numbers from 2 up to the number that the user will input and go through the list and remove all non-prime numbers from that list, then print it back out.…
1
vote
1 answer

Sum of primes under 2 million with Python and Sieve of Eratosthenes, how to do this faster and more efficiently?

This does the job in ~0.7s (2.2GHz i7 quad core), but I know it can be much faster. I think learning how to speed this up could teach me a lot about Python. How do I speed this up? How do I make it more memory efficient? (without using…
the tao
  • 253
  • 2
  • 6
  • 13
1
vote
1 answer

How do I chunk prime numbers for multithreading?

I'm relatively new to C, and up until now had barely any experience in multithreading. I've written a small program that calculates whether an array of numbers are primes or composites. This works fine, but when working with larger numbers I'd like…
1
vote
3 answers

How to fix - 41: non-static variable cannot be referenced from a static context -> What is the reason for this?

I'm trying to write this code to get the first initialCapacity prime numbers and then print them in sequence using java. It isn't working for two reasons, firstly I get the error 41: non-static variable listOfPrimeNumbers cannot be referenced from…
user476033
  • 4,607
  • 8
  • 32
  • 35
1
vote
1 answer

Why doesn't it terminate - BigInteger Prime test

I wrote two methods to test wheter a BigInteger is a prime or not. I start with (2^64)+1 and then I always add 2 until I find a prime. These are the two methods: public static boolean isPrime(BigInteger n) { BigInteger max_long =…
Tim Buchholz
  • 83
  • 1
  • 3
  • 10
1
vote
0 answers

finding nth prime number

I've written a code block to find n-th prime number. For instance if n=2, then the result is 3 and if n=3, then 5 and so on. Below is my code. def prime_or_not(n): for i in range(2, int(n ** 0.5)+1): if n % i == 0: return…
higee
  • 402
  • 1
  • 7
  • 16
1
vote
2 answers

Trying to write code that asks the user for a number, determines if it's prime, and lists the prime numbers between one and that number

For some reason when I test this, the second if statement always prints every number between one and the number the user input as not prime, even if it is. However, the third if statement correctly states if the user's number is prime or not. Is…
Solaire
  • 21
  • 7
1
vote
6 answers

Computing prime numbers up to N integers

I am trying to write a little script myself to compute all of the prime numbers up to n (a user submitted argument) and would appreciate a little bit of help. I want to use ArrayLists to write this function, and hopefully make it as efficient as…
user476033
  • 4,607
  • 8
  • 32
  • 35
1
vote
1 answer

How find a twin prime number from a range determined by the user in Fortran

I want to make a program that finds twin prime numbers in a certain range, from n to m. Here it is what I have so far: program twin implicit none integer i, count1, n, m, count2, j, k, pri1, pri2 …
Lívia Dantas
  • 41
  • 4
  • 8
1
vote
4 answers

Primality of a number

It's known that to check whether a number 'n' is prime or not we just need to check whether it has a factor less than the square root of n. My question is isn't it is sufficient to check for all primes less than the square root of n.
Anurag Jain
  • 111
  • 2
1
vote
2 answers

Why does my program repeat print lists?

I am writing a program for Project Euler question 7 that attempts to find the 10001st prime. I adapted a script I already had that found all primes up to any number. It worked fine. But now I have a problem. My code repeats the list. 881, 883, 887,…
Einstein.py
  • 61
  • 1
  • 9
1
vote
3 answers

a prime number program that allows the user to test numbers till the user enters zero. However, after testing 6 numbers, it prints incorrect message

a prime number program that allows the user to test whether a number is prime or not till the user enters zero. However, after testing about 6 numbers, it prints incorrect message like it prints the number is a prime for a non-prime number also and…
eyajosh
  • 61
  • 6
1
vote
1 answer

Accept a sentence and print only those words in reverse which has a prime number length

I am beginner in Java & I am assigned with a lot of programs for my project this week. However, this one is confusing me for a long time now. I wrote a code for it but it is not displaying any result. There are no syntax errors btw! Have a look-…
user58736
  • 9
  • 8
1
vote
0 answers

SPOJ - NDIV Cant find the error

I have implemented sieve approach and it runs correctly for my test cases. I used a counter rather than a flag for all numbers and the times the numbers been accessed. When i run it on SPOJ it gives wrong answer. Here is the…
Parth Mahajan
  • 119
  • 1
  • 4
1
vote
3 answers

Fast Prime Generator besides Sieve

I recently made this bit of code, but wonder if there is a faster way to find primes (not the Sieve; I'm still trying to make that). Any advice? I'm using Python and I'm rather new to it. def isPrime(input): current = 0 while current <…
Emil
  • 41
  • 2
  • 10