title: Fibonacci Sequence in R
body: <p>I am a new R user and have very limited programming experience, hence my question and poorly written code.</p>
  
  <p>I was assigned a problem where I had to use a while loop to generate the numbers of the Fibonacci sequence that are less than 4,000,000 (the Fibonacci sequence is characterized by the fact that every number after the first two is the sum of the two preceding ones).</p>
  
  <p>Next, I had to compute the sum of the even numbers in the sequence that was generated.</p>
  
  <p>I was successful with my response, however, I don't think the code is written very well. What could I have done better?</p>
  
  <pre><code>&gt; x &lt;- 0
  &gt; y &lt;- 1
  &gt; z &lt;- 0
  if (x == 0 &amp; y == 1) {
    cat(x)
    cat(" ")
    cat(y)
    cat(" ")
      while (x &lt; 4000000 &amp; y &lt; 4000000) {
      x &lt;- x + y
      cat(x)
      cat(" ")
      if (x %% 2 == 0) {
          z &lt;- x + z
      }
      y &lt;- x + y
      cat(y)
      cat(" ")
      if (y %% 2 == 0) {
          z &lt;- y + z
      }
    }
  }
  </code></pre>
  
  <p>0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465</p>
  
  <pre><code>cat(z)
  </code></pre>
  
  <p>4613732</p>