Questions tagged [primality-test]

A primality test is an algorithm for determining whether an input number is prime.

A primality test is an algorithm for determining whether an input number is prime.

See https://en.wikipedia.org/wiki/Primality_test.

108 questions
463
votes
14 answers

Why do we check up to the square root of a prime number to determine if it is prime?

To test whether a number is prime or not, why do we have to test whether it is divisible only up to the square root of that number?
Pan
  • 5,555
  • 5
  • 24
  • 27
51
votes
26 answers

Check if number is prime number

I would just like to ask if this is a correct way of checking if number is prime or not? because I read that 0 and 1 are NOT a prime number. int num1; Console.WriteLine("Accept number:"); num1 = Convert.ToInt32(Console.ReadLine()); if (num1 ==…
user1954418
  • 813
  • 7
  • 19
  • 28
21
votes
3 answers

Confused on Miller-Rabin

As an exercise for myself, I'm implementing the Miller-Rabin test. (Working through SICP). I understand Fermat's little theorem and was able to successfully implement that. The part that I'm getting tripped up on in the Miller-Rabin test is this "1…
hraesvelgr
  • 3,961
  • 2
  • 32
  • 58
15
votes
6 answers

Determining if a given number is a prime in haskell

So I have devised the following function for seeing if a given number is a prime in Haskell (it assumes the first prime is 2): isPrime k = length [ x | x <- [2..k], k `mod` x == 0] == 1 it has the obvious pitfall of continuing the evaluation even…
devoured elysium
  • 90,453
  • 117
  • 313
  • 521
10
votes
5 answers

Better algorithm - Next semiprime

Given n, find m such that m is the smallest semiprime greater than n. Next prime is pretty straightforward, semiprime is less so. To be clear, only the semiprime is needed, though getting the factors at the same time would be convenient. I've…
user4159038
9
votes
3 answers

Learning Haskell: Seemingly Circular Program - Please help explain

I'm currently going through the book "The Haskell Road to Logic, Math, and Programming" by Doets and Van Eijck. I've never been exposed to any functional programming language until this book, so keep that in mind. Still early in the book, it gives…
7
votes
3 answers

Lisp - prime number

I am trying to learn lisp and I have some difficulties with prime numbers. I need a function is-prime and if it is prime I have to return t and if it is not I have to return nil. (prime 41) => t (prime 35) => nil So far I've got: (defun is-prime…
mad scientist
  • 265
  • 1
  • 6
  • 14
6
votes
2 answers

Deterministically checking whether a large number is prime or composite?

I'm searching for an algorithm to primality test large (like 10200) numbers. Are there any good algorithms? Ideally, I'd prefer an algorithm that isn't probabalistic. Note: Numbers have over 50 and less then 200 digits.
gaussblurinc
  • 3,476
  • 8
  • 31
  • 61
6
votes
1 answer

consolidated function is much slower

I wrote isPrime function. It checks if a given number is prime or not. The last "prime" list is given separately. prime :: [Integer] prime = 2 : filter isPrime [3..] isPrime :: Integer -> Bool isPrime n | n < 2 = False isPrime n = all (\p -> n `mod`…
eii0000
  • 697
  • 4
  • 13
6
votes
2 answers

Miller-Rabin Primality test FIPS 186-3 implementation

Im trying to implement the Miller-Rabin primality test according to the description in FIPS 186-3 C.3.1. No matter what I do, I cannot get it to work. The instructions are pretty specific, and I dont think I missed anything, and yet Im getting true…
calccrypto
  • 7,651
  • 19
  • 63
  • 96
5
votes
3 answers

Implementation of AKS primality test in Scheme or C++

I was reading about the prime test algorithm and found the AKS primality test. Could this algorithm be implemented in Scheme or in C++? Has anyone tried implementing the AKS test?
Carlochess
  • 596
  • 1
  • 5
  • 21
5
votes
5 answers

Miller Rabin Primality test accuracy

I know the Miller–Rabin primality test is probabilistic. However I want to use it for a programming task that leaves no room for error. Can we assume that it is correct with very high probability if the input numbers are 64-bit integers (i.e. long…
user3717225
  • 99
  • 1
  • 5
4
votes
3 answers

JavaScript: Check if number is prime with recursion

I am bit confused on how to solve this problem. I need all the prime numbers to return true. If not return false--I see that my logic is including 2 and that returns 0 so it automatically returns false, because 2 has a remainder of 0. …
4
votes
2 answers

MillerRabin primality test in C#

Welcome. I am trying to implement MillerRabin test for checking if large given number is a prime. Here is my code: public static bool MillerRabinTest(BigInteger number) { BigInteger d; var n = number - 1; …
Dago
  • 628
  • 1
  • 9
  • 22
4
votes
1 answer

Miller-Rabin test: impossible result

I was trying to implement the Miller-Rabin primality test on Java and confront its computational time with the native primality test of the BigInteger class. Given that I'm here, you will probably have guessed that my code doesn't work. Problem is,…
Argonath
  • 63
  • 3
1
2 3 4 5 6 7 8