rchinese-remainder-theorem

Finding Unknown variable "x" that satisfies two mod conditions


I was wondering how to find the smallest integer "x," that satisfies two mod conditions.

    x%%7 == 1
    x%%10 == 2

I have attempted to use for/while loops within a function, like so with no such luck:

  min.integer <- function(x) {
    y = c(1:500000)
    for (i in y) {
      if (x%%10 == 2) {
        print(x)
      } else {
        break
      }     
    }
  }

Is this the best way to approach this problem? Thank you!


Solution

  • A simple while loop

    m=n=0
    x=1
    while(!(m==1&n==2)){
      x=x+1
      m=x%%7
      n=x%%10
    }
    x
    22