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

Prime number nested loop Logic

I am currently working on assignment which is stated below. My question is that why does my code only repeat prime number as 2 and not the remainder of the numbers. I would much appreciate if someone could help me to walk through the logic so i can…
1
vote
7 answers

Dictionary of Primes

I was trying to create this helper function in C# that returns the first n prime numbers. I decided to store the numbers in a dictionary in the format. The key is the number in question and the bool represents whether the int is a prime…
sc_ray
  • 7,803
  • 11
  • 63
  • 100
1
vote
2 answers

SPOJ PRIME1: RTE; Is there a method to avoid dynamic array?

I read about Sieve of Eratosthenes algorithm and tried to implement it, the code is getting compiled without any errors but I am getting blank output. Here is the code:- #include #include #define limit 100000 int main() { int…
1
vote
2 answers

Scala stream to find primes in specific interval

Problem: I need to create a Scala program which uses Stream class and finds n-th prime number from interval [i, j] (whereas 1 <= i < j). More information: I am completely new in Scala but I've looked for various examples on how to find primes in…
Alexander Gusev
  • 295
  • 1
  • 9
1
vote
2 answers

How could I make my prime generator function faster

to gain hands-on experience, I'm trying to solve the problems in spoj . The problem in the link asks to find all prime numbers between given 2 numbers. So how I implement this with python 2.7 # printing all prime numbers between given two…
zwlayer
  • 1,752
  • 1
  • 18
  • 41
1
vote
2 answers

Testing for primes

I'm trying to write a simple Python (ver 3.5) code for testing a given integer for primality. When I input 97 (for example) I get four True outputs. What am I doing wrong? # Testing for primality: True or False 25.11.2015 # True means prime, False…
Yehuda Katz
  • 177
  • 1
  • 8
1
vote
2 answers

Prime Number Java

This is for homework and I have most of it but I feel like my prime number generator is off because when i run it, it only increases the Y value by 5 for 1 and 3 not all the other primes between 1 and 100; heres the homework question It's Joen's…
inzikind
  • 31
  • 5
1
vote
2 answers

Prime number calculator. Are these both correct?

Been trying to write program for displaying prime numbers and just wanted to know if there is much difference between the following two algorithms. 1, #include #define MAXNUMB 100000 int main(void){ int flag; long i,j=MAXNUMB; …
riegour
  • 25
  • 5
1
vote
2 answers

Logical error for prime number generator using while loops alone

Following is the code to generate prime nos: to_num=int(raw_input("Enter till where u wish to generate prime nos > ")) i=2 flag="prime" j=2 while i <= to_num: while j
abhishek nair
  • 324
  • 1
  • 6
  • 18
1
vote
1 answer

Powershell Multithreaded math

I'm currently working on a self-inspired project to learn powershell, and have been writing a script to generate prime numbers. As it stands, the script works without issue, but my next goal is to increase it's processing speed. cls $Primes =…
1
vote
1 answer

How to remove prime numbers from a linked list

I am trying to remove the prime numbers from a LinkedList by iterating over it using an Iterator. I have the following code import java.util.LinkedList; import java.util.ListIterator; import java.util.Random; public class LinkedListProcesing{ …
jcvandam
  • 57
  • 1
  • 1
  • 3
1
vote
2 answers

Unexpected output from primes program

I have the following code for a primes program: class Test2 { public static void main(String[] args) { System.out.println("Prime numbers inbetween 2-100: "); boolean isComposite = false; for (int i = 2; i <= 100; i++) { …
RobertR
  • 745
  • 9
  • 27
1
vote
2 answers

Show certain amount of prime numbers

I have been working on some java code to show prime numbers . I have got as far as having it show all prime numbers between 0 and 100 . How would I make it so that I could set a variable to say 20 and it would show me the first 20 prime numbers. My…
Dan
  • 13
  • 2
1
vote
2 answers

Best prime numbers to choose for a double hashed hash table size?

What are the best prime numbers to choose for a double hashed hash table size? side info the hash table is part of a word analysis project, Markov models, training bots to model and generate text as if someone else would write it (which takes a lot…
Fero
  • 567
  • 8
  • 25
1
vote
13 answers

Prime Number Formula

I am trying to write a prime number function in C# and I am wondering if the follow code will work. It "appears" to work with the first 50 numbers or so. I just want to make sure it will work no matter how big the number is: static bool IsPrime(int…
Icemanind
  • 47,519
  • 50
  • 171
  • 296