Skip to content

Instantly share code, notes, and snippets.

@johnistan
Created December 20, 2011 16:54
Show Gist options
  • Save johnistan/1502281 to your computer and use it in GitHub Desktop.
Save johnistan/1502281 to your computer and use it in GitHub Desktop.
Lehmann Primality Test in R
primeTest <- function(n, iter){
a <- sample(1:(n-1), 1)
lehmannTest <- function(y, tries){
x <- ((y^((n-1)/2)) %% n)
if (tries == 0) {
return(TRUE)
}else{
if ((x == 1) | (x == (-1 %% n))){
lehmannTest(sample(1:(n-1), 1), (tries-1))
}else{
return(FALSE)
}
}
}
lehmannTest(a, iter)
}
primeTest(4, 50) # false
primeTest(3, 50) # true
primeTest(10, 50)# false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment