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

Generating tuples of primes with a list comprehension, each tuple having higher sum

I searched, but I didn't find something that helped, so I post a new question. I am learning Haskell, and this is an exercise I don't understand. I want to create an infinite list of tuples of prime numbers, each tuple's sum being a higher even…
m b
  • 73
  • 6
1
vote
2 answers

Python Checking if prime number

so...this is my code down below. I altered it all ways I can think of, but regardless of what I do it will ether say all the numbers are prime or all the numbers are not prime. I was hoping someone can point out the obvious error. Currently this…
Andrew Ricci
  • 475
  • 5
  • 21
1
vote
2 answers

About speed of procedures between user-made and built-in in scheme (related with SICP exercise 1.23)

//My question was so long So I reduced. In scheme, user-made procedures consume more time than built-in procedures? (If both's functions are same) //This is my short version question. //Below is long long version question. EX 1.23 is problem(below),…
lsc4719
  • 17
  • 1
  • 1
  • 4
1
vote
1 answer

Prime factorization with Actionscript 3

I am currently trying to make a program which is able to prime factorize any given number, but I am having some problems. I have already coded a program which generates the first x primes, and I thought I could use this program to solve my former…
Smeestad
  • 37
  • 7
1
vote
1 answer

Prime generator in python returns multiple composites instead of primes

I'm fairly new to Python, and am trying to practice programming by doing Project Euler problems. In order to solve the 7th problem, I decided to first build a simple prime generating function using for loops, which doesn't seem to work. Here's my…
1
vote
2 answers

Understanding Haskell code which applies the `tails` function to an infinite list within a list comprehension

After submitting my solution to Project Euler's problem 50 earlier today, I was scrolling through the problem's forums, taking a look at other folks' solutions/execution times. After a while, I started to feel quite proud of my code which solved…
iceman
  • 2,020
  • 2
  • 17
  • 24
1
vote
2 answers

Generating prime numbers in poly-time

I am struggling to see how we can generate a list of J smallest primes in poly-time J, based on the fact that p'j is less than or equal to 2j * ln(j) for j > 2, where j indicates the j-th consecutive prime number. For instance, p1 = 2 for j=1, p2 =…
Rumen Hristov
  • 867
  • 2
  • 13
  • 29
1
vote
8 answers

the method to calculate prime number

I am trying to figure out how this Java method calculates a prime number but something is confusing me. public static boolean isPrime(int number){ for(int divisor =2; divisor <= number / 2; divisor++){ if (number % divisor ==0){ …
1
vote
2 answers

Generating a prime number bigger than a number found in a list

What isn't working below: I can't make the genPrim function work, as I get the "TypeError: 'int' object is not subscriptable". A few observations: 1. What my program should do is first input a number into a list and then apply the other functions on…
Geosphere
  • 315
  • 4
  • 15
1
vote
3 answers

Custom Ruby Method For Indetifying a Prime Number (Error Help)

I'm having trouble with Coderbyte's Prime Time problem. The link is: http://www.coderbyte.com/CodingArea/GuestEditor.php?ct=Prime%20Time&lan=Ruby def PrimeTime(num) for i in 1..num if (num % i) == 0 return false else …
1
vote
4 answers

Prime number program in C

This program is supposed to print the first x prime numbers, but I noticed that it was printing some non-prime numbers, such as 27 or 35. I've been looking at it for hours and nothing seems to pop up. So please, if you know what's wrong, tell…
Gordon Ramsay
  • 51
  • 1
  • 6
1
vote
3 answers

Printing prime numbers from x to y algorithm

I have this snippet of code that generates the primes on "max" in a sufficient time with Sieve of Eratosthenes. I want to give the function the posibility to use a starting value to calculate a range of primes. So I wonder at what point in the…
Codez
  • 23
  • 1
  • 5
1
vote
2 answers

Counting primes?

So here's my problem that I have for homework. Write a program that takes a command-line argument N (integer, N>= 1) and prints the number of primes less than or equal to N. A number is prime if it is divisible only by itself and 1. Here's the code…
Bryan Cruz
  • 11
  • 4
1
vote
2 answers

Sieve of Eratosthenes without arrays?

I have to write a java code for the 'sieve of eratosthenes' algorithm to print out primes up to a given max value on the console but I'm not allowed to use arrays. Our professor told us it is possible to do only with the help of loops. So I thought…
1
vote
3 answers

What is the space complexity of a prime sieve with data in proportion to number of primes?

I'm practicing writing algorithms optimized for space or time complexity. With prime sieves, at minimum you must store a list of all primes found. It seems data in proportion to the number of primes found is the least amount of space such an…
1 2 3
99
100