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
4
votes
4 answers

Primality test using Fermat little theorem

code: void prime() { int i,N; scanf("%d",&N); for(i=2;i
user2771151
  • 411
  • 1
  • 7
  • 18
4
votes
3 answers

Can someone explain this Miller-Rabin Primality test pseudo-code in simple terms?

Here it is... Input: n > 3, an odd integer to be tested for primality; Input: k, a parameter that determines the accuracy of the test Output: composite if n is composite, otherwise probably prime Write n − 1 as (2^s)·d with d odd by factoring powers…
isados
  • 73
  • 2
  • 8
4
votes
2 answers

Turing Machine to accept strings of prime lengths

I have a homework problem that asks me to describe a program for a non deterministic Turing Machine that accepts L = {a^n: n is prime}. I'm not sure on how to go about this. do i know n? do i use the as as unary digits and count them? can i just…
calccrypto
  • 8,583
  • 21
  • 68
  • 99
3
votes
1 answer

128 bit Miller Rabin Primality test

I wanted to implement Miller Rabin Primality Test for large numbers. I wanted to know how to deal with such huge numbers in C++. Should I write any special function to store and process those large numbers or care is taken automatically?
maddie
  • 629
  • 10
  • 29
3
votes
0 answers

Time complexity of Miller Rabin Primality Test

What would be the time complexity be for this algorithm below? using ll = long long; using u64 = ll; using u128 = ll; u64 binpower(u64 base, u64 e, u64 mod) { u64 result = 1; base %= mod; while (e) { if (e & 1) …
Greg
  • 69
  • 6
3
votes
2 answers

Implementing Miller-Rabin in C

I'm trying to implement the Miller-Rabin primality test in C99, but I'm coming across some problems getting it to work. I crafted a small test-set to verify whether or not the implementation works, here's how I'm checking for primes int main() { …
Bernardo Meurer
  • 2,295
  • 5
  • 31
  • 52
3
votes
1 answer

How to implement the primality test algorithm from Wikipedia?

On Wikipedia the algorithm below is supposed to test if an odd integer, n, is a composite by the probabilistic Rabin-Miller primality test. Input: n > 3, an odd integer to be tested for primality; Input: k, a parameter that determines the accuracy…
Lehs
  • 812
  • 6
  • 18
3
votes
2 answers

Calculating whether number is prime in Prolog

I am trying to calculate if the input is a prime number but something goes wrong... here's my code: primeNumber(X):- prime_prime(A, 1). prime_prime(A, B):- R is A mod B, R =:= 1, R =:= A. prime_prime(X, B):- B < A, Next is B…
Fjodor
  • 529
  • 1
  • 7
  • 19
3
votes
1 answer

How do i improve the complexity of this randomized primality testing algorithm?

I wrote the following program which returns 1 if a number is prime and 0 if it is composite. Though there's a possibility of falsely identifying a composite as prime .I want suggestions on improving(decreasing) the time complexity for the…
Namit Sinha
  • 1,415
  • 3
  • 16
  • 30
3
votes
1 answer

Miller-Rabin Scheme implementation unpredictable output

I am new to Scheme. I have tried and implemented probabilistic variant of Rabin-Miller algorithm using PLT Scheme. I know it is probabilistic and all, but I am getting the wrong results most of the time. I have implemented the same thing using C,…
3
votes
5 answers

Generating large prime numbers in python

I can't seem to make random prime numbers using this code, please can someone help me? def RandomPrime(): prime = False while prime == False: n = random.randint(10000, 100000) if n % 2 != 0: for x in range(3, int(n**0.5), 2): …
3
votes
2 answers

Haskell prime number generator according to bits for very large numbers

I just got into haskell. I'm trying to write a prime number generator that would return me a prime number depending on the number of bits I give it. Do I use some sort of Sieve of Eratosthenes? Would this be the quickest way? Currently, I already…
user805981
  • 9,979
  • 8
  • 44
  • 64
3
votes
2 answers

Haskell Prime-testing confusion

So I'm totally new to Haskell, hope it doesn't show too much. By any means, I was trying to create a function to determine if a number is prime. The basic idea is this: Given a number, see if it is divisible by any other number less than it. If so,…
dj_bushido
  • 363
  • 4
  • 19
2
votes
1 answer

CUDA kernel for determining primes slower than OpenMP code - how can I optimize it?

To practice programming with CUDA in C++. I did an exercise which consists in displaying the prime numbers less than N. For each code I commented out the last display loop to compare only the calculation times. The Makefile : all: sum…
2
votes
1 answer

Time Complexity of this primality test function

What would be the time complexity of this function bool prime(int n) { if(n <= 1) { return false; } else if(n <= 3) { return true; } else if(n % 2 == 0 || n % 3 == 0) { return false; } else { for(int i…
Greg
  • 69
  • 6