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.

115 questions
2
votes
1 answer

Lucas-Lehmer test using Python not working for large numbers

I am trying to write a function that should take in a Mersenne number and return whether it is a prime number or not using the Lucas-Lehmer primality test. I am trying to return the last number generated in the Lucas-Lehmer sequence which should be…
2
votes
1 answer

Miller-Rabin test (SICP 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 positive integer…
Brady Dean
  • 3,378
  • 5
  • 24
  • 50
2
votes
5 answers

SICP Exercise 1.28 - Miller-Rabin - "at least half the numbers will reveal a nontrivial square root of 1 modulo n"

SICP Exercise 1.28 https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-11.html#%_thm_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…
Denziloe
  • 7,473
  • 3
  • 24
  • 34
2
votes
2 answers

Check for a prime number using recursive helper function

I am trying to check if a number is prime using recursion. I was required to use a recursive helper function, but I am not sure how I should implement it. I think I know the algorithm, but I've never tried to use a recursive helper function in…
Isaac
  • 273
  • 3
  • 19
2
votes
1 answer

What's the Time Complexity of my Primality Test?

I have a basic understanding of how to calculate Time Complexities, but I'm not sure how to calculate it in this case due to the random nature of primes. A quick explanation --> Essentially, I'm keeping a running count of the remainders so that I…
Adi219
  • 4,712
  • 2
  • 20
  • 43
2
votes
3 answers

Trouble with defining an ISPRIME function

I am very new to LISP and was working through some beginner problems. I tried defining an ISPRIME function but it doesn't seem to be working correctly. Here is my code: (defun ISPRIME (n &optional (d (- n 1))) (if (= d 0) (…
John Bucher
  • 59
  • 1
  • 7
2
votes
4 answers

Mersenne primes using Lucas-Lehmer primality test

Here is the code, where limit = 8: #include #include // pow(x, exp) //---------------------------------------------------------- char isMersenneLucasLehmer(unsigned int prime) { unsigned int i, termN = 4; unsigned long…
Ziezi
  • 6,375
  • 3
  • 39
  • 49
2
votes
1 answer

Why is my Miller Rabin algorithm not working (Haskell)?

I implemented the Miller Rabin test in Haskell. I tried to strictly follow the pseudo code as given for instance in the wikipedia entry for the Miller Rabin test. Now I found online that for certain choices of witnesses, the test is deterministic…
Slugger
  • 665
  • 1
  • 5
  • 17
2
votes
2 answers

Determine if number is prime in Prolog

So I'm trying to determine if a number is prime using only one predicate. I don't really understand why every number is being declared false here. is_prime(2). is_prime(X) :- X > 2, %0 and 1 aren't primes, 2 is dealt with above 1 is…
user3427168
  • 21
  • 1
  • 2
2
votes
3 answers

Haskell - What does the lambda in this primality test mean and how does this work?

From http://www.haskell.org/haskellwiki/Testing_primality, there is this code: isPrime n = n > 1 && foldr (\p r -> p*p > n || ((n `rem` p) /= 0 && r)) True primes Where primes is a list of prime numbers (possibly infinite). Two questions: How…
hgcrpd
  • 1,820
  • 3
  • 19
  • 32
1
vote
1 answer

Basic primality test predicate in Prolog

I am trying to create a predicate isPrime/1 that checks if a given number is prime or not. I have come up with the following code: primeRec(_, 2). primeRec(X, Y) :- Y > 2, X mod Y-1 > 0, primeRec(X, Y-1). isPrime(2). isPrime(X) :- X > 1, X mod 2 >…
Keroten
  • 99
  • 5
1
vote
0 answers

I am trying to write a function in Python encapsulating the Miller-Rabin primality test, but it is very slow

Here is my code: import random def miller(n, k): """ takes an integer n and evaluates whether it is a prime or a composite n > 3, odd integer to be tested for primality k, rounds of testing (the more tests, the more accurate the result) """ temp =…
1
vote
2 answers

C++: Why/How a Break Statement Works In This Code?

I have started to use C++ programming language as a complete beginner. With the aim of becoming a better programmer for my STEM degree and with the goal of competitive programming in mind. I have started Functions and Loops in C++ recently and there…
F.N.
  • 13
  • 5
1
vote
0 answers

Fermat's primality test

From my understanding, the fermat's primality test should fail for all carmichael numbers. This seems to identify prime numbers fine, but all the carmichael numbers that I tested returned 'composite' for high k values, which is unexpected. Can…
Alex Kang
  • 23
  • 2
1
vote
1 answer

Numpy AKS primality function

I'm implementing the AKS-primality test using Numpy. Specifically, I'm using the polynomial capabilities of Numpy to find the coefficients of the equation (x - 1)^n - (x^n - 1) and then returning True if all those coefficients are divisible by the…
mttpgn
  • 327
  • 6
  • 17