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
1
vote
1 answer

Why is my algorithm about Fermat primality test so slow?

I am learning Number theory. Now, I want to write a program that perform Fermat primality test. First, I write a modular square algorithm: #modular_square.py def modular_square(a, n, m): res = 1 exp = n b = a while exp !=0 : …
Land
  • 171
  • 11
1
vote
1 answer

Writing an isPrime function in Haskell

isPrime :: Int -> Bool isPrime n = leastDivisor n == n leastDivisor :: Int -> Int leastDivisor n = leastDivisorFrom 2 n leastDivisorFrom :: Int -> Int -> Int leastDivisorFrom k n | n `mod` k == 0 = k | otherwise =…
F. Zer
  • 1,081
  • 7
  • 9
1
vote
2 answers

Is it possible to test whether a number is prime or not in O(logn)?

I have been reading a competitive programming book for one month. The book is written by one of the world finalists of our country (Bangladesh). Point to be noted, the book is written in our native language (Bengali) and that is not so popular…
1
vote
1 answer

isPrime function with TLA+

This question is about TLA+ using toolbox (https://github.com/tlaplus/tlaplus/releases) I haven't been able to find any tag about it. Sorry about that. This is why I only tagged with Primes. If I am missing something please be kind to add better…
Minirock
  • 628
  • 3
  • 13
  • 28
1
vote
3 answers

Why is my prime number checking code not displaying the correct output?

I have a code that checks whether a number is prime or not, and outputs "Yes" or "No" accordingly. But when I input 1763, it outputs "Yes" even though it isn't a prime. The code checks whether a number is prime or not by checking if it can be…
user10441782
1
vote
2 answers

Lucas-Lehmer primality test with python

I wrote the code below, to get the Lucas-Lehmer series up to p, for p the exponent of a Mersenne number. After checking it I found that it doesn't work for some primes p such as 11, 23, 29 etc. Any help will be very valuable! Here's the code: def…
nik panik
  • 11
  • 1
  • 2
1
vote
1 answer

Finding a prime with Miller Rabin

I have what I believe is a proper implementation of the miller-rabin algorithm using Lua, and I am trying to get a consistent return for prime numbers. It seems my implementation only works half of the time. Although if I try implementing similar…
uninformed
  • 15
  • 1
  • 6
1
vote
0 answers

Miller-Rabin primality test-SAGE

I'm trying to program the Miller-Rabin primality test in SAGE. Here is my code: def miller(n,k): i=1 s=(n-1).valuation(2) t=(n-1)/(2**s) while(i>0 and i<=k): a=randint(3,n-3) if gcd(a,n)!=1: i=0 else: y=a**t%n if…
1
vote
4 answers

Primality of a number

It's known that to check whether a number 'n' is prime or not we just need to check whether it has a factor less than the square root of n. My question is isn't it is sufficient to check for all primes less than the square root of n.
Anurag Jain
  • 111
  • 2
1
vote
1 answer

Miller Rabin primality test two types?

I encountered two types of Miller Rabin primality test methods suddenly. One which uses randoms and another does not use randoms. Is there a hidden random generation inside the second one or what? Thank you.
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
4 answers

Improving trial division primality test using 6k+/-1 rule

I was going through the basics of trial division primality test, and hence, implementing it in code. The performance of the algorithm can be increased using many tricks like: 1) running the trial division only up to square-root(n) 2) trading memory…
Tanmay Garg
  • 801
  • 11
  • 20
1
vote
1 answer

Modified version of Miller-Rabin to test deterministic primality?

The Miller-Rabin test uses k random integers to test for primality. According to CLRS, 3rd Edition, Page 971: Theorem 31.38 If n is an odd composite number, then the number of witnesses to the compositeness of n is at least (n - 1)/2. Then why…
Existent
  • 697
  • 1
  • 7
  • 14
1
vote
3 answers

Using an If statement to find prime numbers

The following is a piece of code that shows an output stating whether a number, entered by a user, is a prime number or not. #include #include int a,b; int main(void) { printf("Enter number: "); fflush(stdout); …