0

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

Community
  • 1
  • 1
  • 3
    Collatz-Ulam? You need a break condition. Look at `while` to get a loop, `if` for the conditions and take a look at `%%` for mod so you are able to determine if the number is even or odd. – Martin Gal May 26 '20 at 14:47
  • 1
    And welcome to SO. Please take a look at [How to make a great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and [How to ask](https://stackoverflow.com/help/how-to-ask). So you have to show some effort, what have you tried so far? What errors occured? – Martin Gal May 26 '20 at 14:49
  • I add "which (x %% 2 == 0)" command to the function for even numbers, but when I enter the command to add odd numbers, the function returns an error. – Ümit Tütüncü May 26 '20 at 14:50
  • 1
    `x` is unlikely to be a vector input if it stands for the number you are iterating from. `which` doesn't really make sense for the problem as described. Do you know how to use `if`? If not -- you really need a basic tutorial in R programming. – John Coleman May 26 '20 at 14:52
  • 2
    What command do you use to add odd numbers? What does the error say? Please do have a look at Martin Gal's [How to Ask](https://stackoverflow.com/help/how-to-ask) link - improving the question will help us help you more. – Gregor Thomas May 26 '20 at 14:52
  • How do you start your calculation? Are you using a function? What are you exactly doing? `which` is usally used to find a special element inside a vector. Are you using a vector? Please show your code, so we can answer your question. – Martin Gal May 26 '20 at 14:53
  • 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 > – Ümit Tütüncü May 26 '20 at 15:01
  • 1
    `which` is pointless in your code (which belongs in the question itself, not in a comment) `if(x%%2 == 0)` works fine. – John Coleman May 26 '20 at 15:06
  • Please put your code in your question (there's an "edit" button at the bottom) - it gets buried in the comments. – Gregor Thomas May 26 '20 at 15:11
  • 1
    The code that you posted suggests that you are trying to write the code in the console rather than in a code file (which you then `source` to test). Bad idea. The console is good for quick interactive testing, but is awful for coding. Please post properly formatted code (without stray `+`) in the question itself. – John Coleman May 26 '20 at 15:11
  • I changed it as you said John Coleman. I got this result. f=function(x) { + if(x%%2==0) { + x=x/2 + print(x) + } else { + if(x%%2==1) { + x=3*x+1 + print(x) + } + } + } > f(2) [1] 1 > f(3) [1] 10 > f(5) [1] 16 > f(6) [1] 3 > f(9) [1] 28 > So what code do I need to write to continue this automatically? – Ümit Tütüncü May 26 '20 at 15:12
  • 1
    As @MartinGal said above, use a `while` loop. – John Coleman May 26 '20 at 15:13
  • 1
    Please look up `while` (in R help) and think about the condition for stopping the algorithm. – Martin Gal May 26 '20 at 15:14

1 Answers1

0

You can vastly simplify this code. I think others have already mentioned that "which" is not correct here. But also look at the fact that if something is not even (divisible by two) then it must be odd. This implies that the decision is dichotomous (i.e. 0 or 1). You can therefore simplify the code to the following. Notice that we have also move the print after the if statement since we must always print the result. It isn't necessary to duplicate the code. As for the series you will need to use a loop. You could look at running a recursive loop, but keep in mind that this is a never ending function and you should stop it unless you'd like to make an attempt at seeing infinity.

f=function(x) { 
  if(x%%2==0) { 
    x=x/2 
  } else { 
      x=3*x+1  
  } 
  return (x) # Ben Bolker made a good point that you will need to return X

} 

f(1)
f(2)
f(7)
TsTeaTime
  • 881
  • 1
  • 13
  • 34