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

Corner cases of codechef farmer feb.Code:POTATOES

This question originates from codechef, the online contest website. It requires calculating prime numbers. The question is: Farmer Feb has three fields with potatoes planted in them. He harvested x potatoes from the first field, y potatoes from the…
bhavya joshi
  • 1,096
  • 10
  • 20
1
vote
1 answer

What variation of Sieve of Eratosthenes is this?

I am trying to solve a problem Prime Path on spoj, and I am trying to understand the solution I found on github. The broad logic to solve this problem is to generate all four digit primes and add an edge iff we can go from one prime to the next by…
Abhishek Kusnoor
  • 198
  • 1
  • 4
  • 13
1
vote
4 answers

Prime factor in javascript, why is this case not working?

I'm writing a primality checker in in j/s and I was wondering why I'm getting true as a return for when I test 55... It seems to work fine for all the other cases I checked just not 55, can someone tell me where I went wrong? var isPrime =…
hackrnaut
  • 581
  • 5
  • 20
1
vote
3 answers

Euler #3 in ruby. What am I not understanding about ruby?

Project Euler 3: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? I don't want to just google the solution for this problem, because I'd like to come to understand this language..…
1
vote
2 answers

Primality test predicate isPrime/1 in Prolog

I am trying to understand solutions of exercises in order to prepare my logic programming exam and somehow I could not understand the logic of the code below. Explanations in the question: n is a prime number n > 1 and 1 < m < n n/m has a non-zero…
limonik
  • 499
  • 1
  • 6
  • 28
1
vote
3 answers

Prime Sieve only prints integers 1-3

Recently, I've been attempting to create a program that prints prime numbers until a user-specified integer is achieved, the program itself including a "PrimeCheck" class, a "PrimeSieve" class of sorts, and a "Main" class: public class PrimeCheck…
1
vote
2 answers

Finding smallest prime factor

I am trying to create a function that returns the smallest prime factor of a given number: require 'prime' def findSmallestPrimeFactor(number) return 2 if number.even? return number if Prime.prime? number arrayOfFactors = (1..number).collect…
Nemo
  • 187
  • 13
1
vote
3 answers

Primes and boolean logic

I wanted to write a program to find all the primes from b to a so I wrote this code (which worked): public static void main(String[] args) { int a; int c; boolean isPrime; a = 2; c = 0; while(a <= 100000 ){ …
Idan Gelber
  • 195
  • 3
  • 4
  • 11
1
vote
2 answers

How to make the Sieve of Eratosthenes faster?

I am trying to solve the 10 problem in the Project Euler. It consists on finding the sum of all the primes below two million. I wrote the following code based on the Sieve of Eratosthenes. import time t0 =…
1
vote
5 answers

Count prime number till N

This code works fine to count prime number till n. The problem is when n value is large like 1000000 or more, then it takes long time to execute and print the output (more than 30 secs.). I want to fix this issue. Any help would be great. Below is…
Ujjwal
  • 346
  • 4
  • 11
1
vote
3 answers

F# finding only prime numbers

I'm recently new to F# so please bear with me. The problem i have is I'm trying to find only prime numbers. I've write this code: let isPrime n = let rec check i = i > n/2 || (n % i <> 0 && check (i + 1)) check 2;; let listNums =…
user3473445
  • 184
  • 5
  • 18
1
vote
3 answers

Out Of Memory error with HackerEarth Problem: Reverse Primes

Generate as many distinct primes P such that reverse (P) is also prime and is not equal to P. Output: Print per line one integer( ≤ 10^15 ). Don't print more than 10^6 integers in all. Scoring: Let N = correct outputs. M = incorrect…
Ankur Anand
  • 3,873
  • 2
  • 23
  • 44
1
vote
5 answers

Python prime number function returning error in tutorial

Python newbie here, so bear with me... Unfortunately there's no "support" for these tutorials, except posting questions in a Q&A forum and maybe another student can help. I know that there are a ton of Python prime functions out there, but I think…
AdjunctProfessorFalcon
  • 1,790
  • 6
  • 26
  • 62
1
vote
3 answers

Python-Prime number checker-product of primes

I wrote the following code to check for whether a number is prime. While there are more efficient ways to do this, I did notice that while this works for the largest of the primes, it breaks for every product of primes. So, while it correctly…
Arun Kumar
  • 11
  • 1
1
vote
3 answers

Python – Have a variable be both an int and a str

Here is my code: def retest2(): print "Type in another chapter title! Or type \"Next\" to move on." primenumbers2() def primenumbers1(): print "--------------------------------------------------\nChapters in books are usually given the…
theo1010
  • 127
  • 10