r-statistics-fanの日記

統計好き人間の覚書のようなもの

孤独の7(名作虫食い算をRで解いてみる)

孤独の7

Rで名作虫食い算:孤独の7を解いてみる。

7だけが明らかになっていて、ほかはすべて伏せられているという

名作虫食い算である。ぜひ、ノーヒントで手計算でチャレンジするのを

勧めたい。

 

孤独の7 ⇐問題文はここのhtmlを引用しました

 

 

問題
       *7***
***)********
    **** 
      ***
      *** 
      ****
       ***  
        ****
        ****
           0

## 孤独の7 # temp2 / aaa = B7C0Dとする 商
B7C0Dの10の位は必然的に0になる

for (a in 100:999) {
    if (7 * a >= 1000 || 7 * a < 100) next
    for (b in 1:9) {
        if (a * b >= 10000 || a * b < 1000) next
        for (c in 0:9) {
            if (a * c >= 1000 || a * c < 100) next
            for (d in 0:9) {
                if (a * d >= 10000 || a * d < 1000) next
                temp <- (b * 10000 + 7 * 1000 + c * 100 + d)
                temp2 <- temp * a
                if (a * temp >= 1e+08 || a * temp < 1e+07) next
                if (temp2 %/% 1000 - a * b * 10 >= 1000 || temp2 %/% 1000 - a * b * 10 < 100) next
                if (temp2 %/% 100 - a * b * 100 - 7 * a * 10 >= 10000 || temp2 %/% 100 - a * b * 100 - 7 * a * 10 < 1000) next
                if (temp2 - a * b * 10000 - 7 * a * 1000 - a * c * 100 >= 10000 || 
                  temp2 - a * b * 10000 - 7 * a * 1000 - a * c * 100 < 1000) next
                #まちがいcat(sprintf("%s / %s = %s\n", temp, a, temp * a))
            cat(sprintf("%s / %s = %s\n", temp * a, a, temp) )
      } } } }
## 97809 / 124 = 12128316
## 12128316 / 124 = 97809

うむ解けた

しかし、この問題は手計算のほうが圧倒的に楽しかったな。 名作だ。