Using R, I need to create a code that checks whether a number x is a prime number or not using RECURSION ONLY (while, for, and any iterative operators are not allowed. I made one but it seems that my code has a problem when I run it :
is_prime <- function="" (x)="">-> divisor <->-> divisor_check <- function="" (x,="" divisor)="">-> if (x <= 2)="">=> if (x == 2){ return(TRUE) } else { return(FALSE) } } else if (x %% divisor == 0) { return(FALSE) } else if (divisor * divisor > x) { return(TRUE) } else if (x == 0 || x != floor(x)){ return(NULL) } else { return(FALSE) } } divisor_check(x, divisor+1)}
above is the code and when I run it, the results are as follows:
> is_prime(1)
[1] FALSE
> is_prime(2)
[1] TRUE
> is_prime(3)
> is_prime(4)
> is_prime(5)
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here