Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6960209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:22:22+00:00 2026-05-27T15:22:22+00:00

I spent a little time hacking an R implementation of the lehmann primality test.

  • 0

I spent a little time hacking an R implementation of the lehmann primality test. The function design I borrowed from http://davidkendal.net/articles/2011/12/lehmann-primality-test

Here is my code:

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
primeTest(97, 50) # gives false # SHOULD BE TRUE !!!! WTF

prime_test<-c(2,3,5,7,11,13,17 ,19,23,29,31,37)

for (i in 1:length(prime_test)) {
  print(primeTest(prime_test[i], 50))
}

For small primes it works but as soon as i get around ~30, i get a bad looking message and the function stops working correctly:

2: In lehmannTest(a, iter) : probable complete loss of accuracy in modulus

After some investigating i believe it has to do with floating point conversions. Very large numbers are rounded so that the mod function gives a bad response.

Now the questions.

  1. Is this a floating point problem? or in my implementation?
  2. Is there a purely R solution or is R just bad at this?

Thanks

Solution:

After the great feedback and a hour reading about modular exponentiation algorithms i have a solution. first it is to make my own modular exponentiation function. The basic idea is that modular multiplication allows you calculate intermediate results. you can calculate the mod after each iteration, thus never getting a giant nasty number that swamps the 16-bit R int.

modexp<-function(a, b, n){
    r = 1
    for (i in 1:b){
        r = (r*a) %% n
    }
    return(r)
}


primeTest <- function(n, iter){
   a <- sample(1:(n-1), 1)
    lehmannTest <- function(y, tries){
      x <- modexp(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)
         }
    }
  }
   if( n < 2 ){
     return(FALSE)
     }else if (n ==2) {
       return(TRUE)
       } else{
         lehmannTest(a, iter)
         }
}

primeTest(4, 50) # false
primeTest(3, 50) # true
primeTest(10, 50)# false
primeTest(97, 50) # NOW IS TRUE !!!!


prime_test<-c(5,7,11,13,17 ,19,23,29,31,37,1009)

for (i in 1:length(prime_test)) {
  print(primeTest(prime_test[i], 50))
}
#ALL TRUE
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:22:23+00:00Added an answer on May 27, 2026 at 3:22 pm

    Of course there is a problem with representing integers. In R integers will be represented correctly up to 2^53 – 1 which is about 9e15. And the term y^((n-1)/2) will exceed that even for small numbers easily. You will have to compute (y^((n-1)/2)) %% n by continually squaring y and taking the modulus. That corresponds to the binary representation of (n-1)/2.

    Even the ‘real’ number theory programs do it like that — see Wikipedia’s entry on “modular exponentiation”. That said it should be mentioned that programs like R (or Matlab and other systems for numerical computing) may not be a proper environment for implementing number theory algorithms, probably not even as playing fields with small integers.

    Edit: The original package was incorrect
    You could utilize the function modpower() in package ‘pracma’ like this:

    primeTest <- function(n, iter){
      a <- sample(1:(n-1), 1)
        lehmannTest <- function(y, tries){
        x <- modpower(y, (n-1)/2, n)  # ((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)
    }
    

    The following test is successful as 1009 is the only prime in this set:

    prime_test <- seq(1001, 1011, by = 2)
    for (i in 1:length(prime_test)) {
        print(primeTest(prime_test[i], 50))
    }
    # FALSE FALSE FALSE FALSE TRUE  FALSE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently spent some time chasing an annoying little bug and I'm looking for
Spent a bunch of time looking at this.. It seems that what little info
I have just spent little time with iOS. Anybody can tell me how to
We have two systems with built in GPS today I spent a little time,
Spent some time troubleshooting a problem whereby a PHP/MySQL web application was having problems
I spent the day experimenting with AWS for the first time. I've got an
I spent some time trying to figure out why this query isn't pulling the
I spent some time looking around, and all I could find is Jython. It's
I spent some time looking for a way of resolving my issue but with
I spent a lot of time to find out why my app crashed. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.