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

Find the greatest prime number with 7 as the last digit in {1, ..., n}

Let's suppose n is an integer around 250000. Using Java, I need to find the greatest prime number that ends with 7 and belongs to {1, ..., n}. Also, I need to keep an eye on computational complexity and try to lower it as much as I can. So I was…
toxing
  • 47
  • 7
1
vote
2 answers

Time complexity finding n primes with trial division by all preceding primes

Problem : Finding n prime numbers. #include #include void firstnprimes(int *a, int n){ if (n < 1){ printf("INVALID"); return; } int i = 0, j, k; // i is the primes counter for (j…
Sai Shankar
  • 61
  • 1
  • 8
1
vote
1 answer

Python sum of prime factors function to produce finite sequence

In Python2.7 I would like to generate finite sequences for a given integer by repeating the sum of prime factors (sopfr) function over the result(s) until it reaches a prime. The following code for sopfr(n) comes from OEIS A001414. from sympy…
Nick Ward
  • 13
  • 4
1
vote
1 answer

How can I speed this python code up?

I have made some code that calculates prime numbers (Nothing special I know) and as expected it slows down the bigger the number, I know it is impossible to make it the same speed no matter the number but I am sure I could improve it's speed, I just…
Qwertykey
  • 21
  • 5
1
vote
3 answers

Why does the modulo formula to determine what are not prime numbers work for the prime number 2?

def prime?(num) return false if num == 1 (2..num/2).each do |x| if num%x==0 return false end end true end
1
vote
1 answer

Is this sieve really O(n)?

Can anyone please tell me how this is working in O(n). http://www.geeksforgeeks.org/sieve-eratosthenes-0n-time-complexity/ void manipulated_seive(int N) { // 0 and 1 are not prime isprime[0] = isprime[1] = false ; // Fill rest of the…
rittik
  • 63
  • 5
1
vote
7 answers

C++ Printing Odd numbers instead of Prime Numbers

I have been working on an assignment question for days and cannot seem to get the correct output (I've tried so many things!) The question is: Write a program that uses two nested for loops and the modulus operator (%) to detect and print the prime…
Afraser
  • 11
  • 1
1
vote
3 answers

prime numbers in python using for loop and break

I have written a python code to find prime numbers between 2 and 30. But my code is not evaluating for 2 and 3. Can anyone tell me what is wrong in this code? for i in range(2, 30): for j in range(2, i-1): if ((i % j) == 0): …
Lax_Sam
  • 1,099
  • 2
  • 14
  • 32
1
vote
1 answer

Type error: `character_code' expected with prolog

I am new to prolog. I was writing a code with Goldbach’s Conjecture problem which I have to list all possible groups of one even number. I found a code like this: is_prime(2). is_prime(3). is_prime(P) :- integer(P), P > 3, P mod 2 =\= 0, \+…
1
vote
1 answer

Trying to script a program that lists numbers whose digits add up to a prime in Python. How would I be able to fix an overflow error?

def isPrime(num): s=0 for j in range (1, num): if(num%j==0): s=s+1 if(s>1): break return(s) a = 10**20; b = 10**400; for i in xrange(a, b): if(isPrime(i)==1 and isPrime(sum(int(x)…
NyonX
  • 13
  • 5
1
vote
1 answer

Complexity of a simple algorithm to find a prime number

I want know if the asymptotic complexity of this simple algorithm to find a prime number is O(n): PrimeNumber(n) Int i; If (n%2=0) then { return "not prime"; } Else { For(i=3;i<(√n)+1;i=i+2;){ If (n%i=0) then {return "not prime";} } …
1
vote
0 answers

How to simplify prime factor code

I have a javascript algorithm which prints out the prime factors of a number based on a user's input.