is_prime1 <- function ( n ) #*****************************************************************************80 # ## is_prime1() 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 ) { return ( TRUE ) } # # Astoundingly, R parses ( i in 2 : n - 1 ) as (i in (2:n)-1)! # # R morons execute ( i in 2 : 1 ) as 2 downto 1. # To avoid this, we have to make 2 a special case. # for ( i in 2 : ( n - 1 ) ) { if ( ( n %% i ) == 0 ) { return ( FALSE ) } } return ( TRUE ) }