Jump to content

Primality by trial division: Difference between revisions

m
→‎{{header|R}}: replaced buggy implementation (according to it 3 is not prime...). Mind the bounds in seq.
m (→‎{{header|R}}: replaced buggy implementation (according to it 3 is not prime...). Mind the bounds in seq.)
Line 3,407:
 
=={{header|R}}==
<lang rslang>is.prime <- function(n) n == 2 || n > 2 && n %% 2 == 1 && (n < 9 || all(n %% seq(3, floor(sqrt(n)), 2) > 0))
<lang R>isPrime <- function(n) {
if (n == 2) return(TRUE)
if ( (n <= 1) || ( n %% 2 == 0 ) ) return(FALSE)
for( i in 3:sqrt(n) ) {
if ( n %% i == 0 ) return(FALSE)
}
TRUE
}</lang>
 
<lang R>printwhich(lapplysapply(1:100, isPrimeis.prime))</lang>
# [1] 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97</lang>
 
=={{header|Racket}}==
1,336

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.