We have difficulty in writing a function in the R program.Our question is: "If the number we have is odd, let's multiply by 3 and increase by 1. If our number is even, let's divide by two.
EXAMPLE
if our number is 7,
7*3+1=22
22/2=11
11*3+1=34
34/2=17
....
So that the series continues" could you please help us?
f=function(x) {
if(which(x%%2==0)) {
x=x/2
print(x)
} else {
if(which(x%%2==1)) {
x=3*x+1
print(x)
}
}
}
> f(2)
[1] 1
> f(4)
[1] 2
> f(3)
Error in if (which(x%%2 == 0)) { : argument is of length zero