Created
December 20, 2011 16:54
-
-
Save johnistan/1502281 to your computer and use it in GitHub Desktop.
Lehmann Primality Test in R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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