elena1234

Function any()

Apr 11th, 2022 (edited)
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.44 KB | None | 0 0
  1. # Ex 6: Create a function that will return TRUE if an input integer is prime. Otherwise, return FALSE. You may want to look into the any() function.
  2. prime_check <- function (number) {
  3.   is.prime <- T
  4.   if (number == 2) {
  5.     is.prime <- T
  6.   } else if (any(number %% (2:(number - 1)) == 0)) {
  7.     is.prime <- F
  8.   }
  9.  
  10.   return(is.prime)
  11. }
  12.  
  13. prime_check(1)
  14. prime_check(2)
  15. prime_check(5)
  16. prime_check(4)
  17. prime_check(237)
  18. prime_check(131)
  19.  
Add Comment
Please, Sign In to add comment