Search code examples
rloopswhile-loop

How do i solve an equation using loops in R?


if x and y are positive integers, x+xy+y=54. Solve x+y

I want to "translate" this equation into R language, but i'm farily new. I've tried using while loops but i don't know if it is the best approach for this problem


Solution

  • You can use a for loop to find positive integer solutions for the equation x + xy + y = 54 and then calculate the sum of x and y.

    for (x in 1:53) {
      for (y in 1:53) {
        if (x + x * y + y == 54) {
          sum_x_y = x + y
          cat("Solution found: x =", x, "and y =", y, ", x + y =", sum_x_y, "\n")
        }
      }
    }