is_prime2 <- function ( n ) #*****************************************************************************80 # ## is_prime2() reports whether an integer is prime. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 29 December 2022 # # Author: # # John Burkardt. # # Input: # # integer n: the value to be tested. # # Output: # # logical value: true if n is a prime. # { if ( n != round ( n ) ) { stop ( 'is_prime1(): input n is not an integer!\n' ) } if ( n < 0 ) { return ( FALSE ) } if ( n == 0 | n == 1 ) { return ( FALSE ) } # # I had to insert this check to avoid the catastrophic # idiocy of the for loop limits. # if ( n == 2 | n == 3 ) { return ( TRUE ) } ihi = sqrt ( n ) for ( i in 2 : ihi ) { if ( ( n %% i ) == 0 ) { return ( FALSE ) } } return ( TRUE ) }