i have a set of five numbers numbers and i want to Write a function that finds a prime and non_prime numbers.The function Should write the non-prime number(s) factors.
Number_of_set <- c(89, 107, 597, 931, 1083)
the Result return by the function should be Like
#Prime numbers : 89 107
#Non-prime numbers : 597 [3 199] 931 [7 7 19] 1083 [3 19 19]
i tried this but the prime_factors do not return any numbers
prime_factors <- function(x, i=2, factors = NULL){
if(x<i) factors
else if(! x %% i) prime_factors(x/i, i, c(factors, i))
else prime_factors(x, i+1, factors)
}
Ifprime <- function(x){
if(x == 2){
print("Prime")
}
else if(all(x %% (2:(x-1)) != 0)){
print("Prime")
}
else {
return(prime_factors(x))
}
}
for(i in seq_along(NumberSet)){
Ifprime(NumberSet[i])
}