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

Chain of Sieves to generate prime numbers C++

I'm trying to feed each prime number found into a chain of "sieve" objects. The below code achieves what I eventually want to do, but each sieve (Prime number)class is manually implemented (to check subsequent numbers are divisible by the stored…
Jerry
  • 424
  • 1
  • 4
  • 17
1
vote
0 answers

Modification of sieve of eratosthenes, time and space complexity check

I understand the time complexity for the 'sieve of eratosthenes' algorithm to be O(n log log n). I have modified this algorithm such that my sieve will always be of size 100. I will keep looping over next 100 numbers if I do not find the next prime.…
1
vote
2 answers

Adding elements to an array - Pascal

program Primes(input,output); var candidates, primes : Array[0..999] of Integer; n, i, j : Integer; begin for i := 0 to 999 do begin candidates[i] := 1; end; candidates[0] := 0; candidates[1] := 0; i := 0; while i < 1000 do …
Kate
  • 45
  • 4
  • 8
1
vote
1 answer

Determining if a user inputted variable is prime in a do-while loop

so my task is as follows: Construct a do-while() loop, which continues to prompt the user for an integer, and determines the sum of integers entered, until a prime number is encountered. The prime number should not be included in the sum. Show all…
Dillon J.
  • 23
  • 3
1
vote
1 answer

Prime numbers as public key - clarification?

I've read here that : If you’ve watched a security certificate being generated on your computer ..., here is exactly what happens – it produces two large numbers , checks that they are both prime and multiplies them together. This gives you…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
1
vote
5 answers

Why won't my code do literally anything?

I'm trying to write some code to compute the thousandth prime number and when I thought I was done I tried to run it but nothing is happening. I don't get errors or anything that should happen according to the code, just literally nothing. I'm…
pokemonfan
  • 209
  • 1
  • 2
  • 9
1
vote
3 answers

I can't find the bug in my java method that returns an array of prime numbers.

It only uses the prime numbers to check if the other numbers are also prime static public int[] primeGen(int a){ int[] series={2}; if (a==1 || a==2 || a<=0){ return series; } this is where the errors occur else{ …
1
vote
2 answers

What are the advantages of checking if N is a prime number using square root of N versus N/2?

Is it a matter of cutting out iterations to check if a number is a prime? ex. 37 is a prime, and checking up to 18.5 (half of 37) versus 6.08(square root) cuts out a lot of the work, but both follow the same principle? sorry for asking, i am trying…
1
vote
0 answers

Getting the 983rd prime number instead of 1000th

I have just started coding, and I don't seem to be getting it quite right. I would like some feedback on my program. I am getting 7753 instead of 7919 primes = [] sk = [] # list for all None values def primtal(a): if ((2 ** a) - 2) % a ==…
nuevo
  • 11
  • 2
1
vote
1 answer

How to make my code less slow (JavaScript) - largest prime factor

I intend to find the largest prime factor of a number n in Javascript. However, i think this code is to slow or could be sortened, but i dont see how. The point would be to display the last element of the arrayfactors, which would be the largest…
steedsnisps
  • 125
  • 1
  • 4
1
vote
2 answers

Why is the stdlib implementation for primes so slow?

I decided to code the sieve of eratosthenes in ruby for fun. Only for fun because I knew there was a library function. Also, I thought it would be fast. But I found that it is not, at least in my ruby 1.9.3, mine is several times faster on my…
user4527355
1
vote
2 answers

Better algorithm on prime numbers

I'm working on a program which will found the nth. prime number. For example, By listing the first six prime numbers: 2, 3, 5, 7, 11 and 13, we can see that the 6th prime is 13. I'm trying to making an algorithm, like, if I want to see 50th prime, I…
GLHF
  • 3,835
  • 10
  • 38
  • 83
1
vote
2 answers

Why this program does not give any output (to find out the next palindrome and prime no)?

I am trying to make a program in which a number is taken as input and the program displays the smallest number bigger than the input which is both prime and palindrome. But this program does not give any output. Can you please give me the reason? I…
P parker
  • 81
  • 1
  • 4
1
vote
1 answer

Python Implementation of the Sieve of Atkin

My code works to give most primes, however it still include 1 and misses some numbers: 23 and 47 (when calculating prime numbers under 100). For some reason it includes 91, any ideas why?? I have been using the Wikipedia instructions for the Sieve…
Tom
  • 918
  • 6
  • 19
1
vote
1 answer

make dynamic array larger

This is for a non-graded challenge problem where I am looking to find as many prime numbers as fast as possible. One of the constraints is that I must use new/delete, so std::vector is not an option. In this problem, I need to add to an array (in my…
David
  • 757
  • 1
  • 7
  • 17
1 2 3
99
100