Werden wir Helden für einen Tag

Home | About | Archive

麥樂雞問題 #2 R Ver.

Posted on Feb 24, 2011 by Chung-hong Chan

無聊的 R 版,是怕自己寫慣 Python 忘記了怎樣寫 R 。

#chew that < -
buyable <- function(numN) {
  if (numN < 1) {return(NULL)}
  else {rangefound <- as.integer((numN/6)+1)}
  for (num6 in 0:rangefound) {
    for (num9 in 0:rangefound) {
      for (num20 in 0:rangefound) {
        totalN <- 6*num6 + 9*num9 + 20*num20
        if (totalN == numN) {
          return(c(num6,num9,num20))
        }
      }
    }
  }
  return(NULL)
}

unbuyable <- c()
for (trialnumN in 0:200) {
  if (is.null(buyable(trialnumN))) {
    unbuyable <- append(unbuyable, trialnumN)
  }
}

Powered by Jekyll and profdr theme