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

Deleting first N prime numbers from a list (Prolog)

Basically I want to remove the first N numbers from a list, the function that checks whether a number is prime or not seems to work well but the program itself doesn't For example for input [2,4,5,7,6,9,11] and N = 3 I should get [4, 6, 9, 11] but I…
user4850912
1
vote
1 answer

MATLAB - How do I find the first integer of an infinite set that satisfies this condition?

I want to find the smallest integer P, such that the number of primes in the set {1,2,..., P} is less than P/6. I think have the answer via (long) trial and error but would like to know how to verify this through MATLAB.
joe doe
  • 13
  • 2
1
vote
0 answers

Counting coprime pairs in array

Size of array<=10^5 and each element in array is 2<=Ai<=10^5. Is there any way to calculate all the coprime pairs (Ai,Aj) where 1<=i,j<=100000 in O(nlogn) ? Any idea how to use mobious function to solve this?
rishi_07
  • 17
  • 1
  • 2
  • 7
1
vote
5 answers

Why does this make my prime-generating algorithm take longer?

I'm trying to find all primes below 1 000 000. As all non-primes can be factorised into primes, my method is to start my list of primes as [2, 3] and then run through every number up to 1 000 000. If a number is divisible by any number in…
11thHeaven
  • 349
  • 4
  • 8
1
vote
1 answer

Prime generator in java

I wrote this program for generating prime numbers between two values. It's working perfectly for integer values. I tried to make it work for long integer values but the program exits without any processing whenever long integer values are…
1
vote
2 answers

Goldbach function for list of even numbers [4..n]

Hello I am trying to make a function goldbach :: Integer -> Bool for interval [4..n] that should return True only if all elements in that interval is even and can be summed from two prim numbers. So far I have done…
Lukas Karmanovas
  • 409
  • 1
  • 4
  • 14
1
vote
1 answer

SICP exercise 1.28: false negatives in the Miller-Rabin test

Exercise 1.28. One variant of the Fermat test that cannot be fooled is called the Miller-Rabin test (Miller 1976; Rabin 1980). This starts from an alternate form of Fermat's Little Theorem, which states that if n is a prime number and a is any…
nalzok
  • 14,965
  • 21
  • 72
  • 139
1
vote
1 answer

What's wrong with my iterative expmod implementation?

The following program is designed to calculate base^expo mod m. (define (expmod base expo m) (define (square n) (* n n)) (define (even? n) (= (remainder n 2) 0)) (define (expmod-iter base expo m result) (cond ((= expo 0) result) …
nalzok
  • 14,965
  • 21
  • 72
  • 139
1
vote
2 answers

java cyrptography BigInteger - Generated prime q to find prime P =2q+1,Complete - Now how to generate and confirm primitive root g?

Now that I have generated prime P that is a strong prime, how could I generate a primitive root g? I believe I'll generate another bigInt for g, then check to see if it is a primitive root. The generation is easy and I can do it as I do in the Prime…
john stamos
  • 1,054
  • 5
  • 17
  • 36
1
vote
1 answer

Count the number of proper fractions

A fraction p/q (p and q are positive integers) is proper if p/q < 1. Given 3 <= N <= 50 000 000, write a program to count the number of proper fractions p/q such that p + q = n, and p, q are relative primes (their greatest common divisor is 1). This…
dh16
  • 139
  • 8
1
vote
2 answers

Prime Number Recursion- How Does It Work? (Python)

I was wondering how this program knows if a number is prime or not. I understand that it checks for remainders to find even numbers to divide by but how does it know that a number has only 2 factors? I'm new to the concept of recursion so a…
Judgey19XX
  • 11
  • 2
1
vote
1 answer

Algorithm of divisors

I am given a list of integers (up to 1000) which multiply to a given integer n . I need to find the highest power among all the divisors of the integer n . For instance : 4,7,8 multiply to 224 and the highest power will then be 5 since…
user6106260
1
vote
3 answers

Code to print Nth prime number

I have to print the Nth prime number. For example: 1st prime number is 2 2nd prime number is 3 . . 10th prime number is 29 and so on.... My algorithm is as follows: 1) Add 2 and 3 to stack. 2) If n <= Size of stack, get item at position n and…
user6889367
  • 123
  • 2
  • 7
1
vote
1 answer

Finding first n primes efficiently

I have a brute-force algorithm that takes an input n, and displays the first n prime numbers. The code works, but I also have to answer this question: Give the number of test items that would be required to assure correctness. Is there a way to…
Matt123
  • 556
  • 3
  • 15
  • 36
1
vote
1 answer

Prime numbers in a given range using Sieve of Eratosthenes

I am trying to print prime numbers less than 'n'.The code is below: def prime_numbers(n): A=[1 for i in range(n+1)] for i in range(2,int(sqrt(n))): if A[i]==1: for j in range(i*2,n,i): A[j]=0 for i in…
shubhamj
  • 778
  • 1
  • 7
  • 13