Skip to content

Instantly share code, notes, and snippets.

@toshi-ara
Last active December 28, 2020 03:53
Show Gist options
  • Select an option

  • Save toshi-ara/492fb85d0d41b30dbb766ffb19bea6ee to your computer and use it in GitHub Desktop.

Select an option

Save toshi-ara/492fb85d0d41b30dbb766ffb19bea6ee to your computer and use it in GitHub Desktop.
某問題を解くためのプログラム(R版)
## 某問題をしらみ潰し方式で解くためのプログラム
n <- 200
doors <- integer(n) # 配列の準備(0でリセットされる)
for (i in 1:100) {
idx <- seq(i, n, i) # 200を超えないiの倍数を指定
doors[idx] = doors[idx] + 1 # そのドアの開閉の回数を+1(奇数なら開、偶数なら閉)
}
Open <- (doors %% 2 == 1) # 奇数であれば(ドアが開いている)TRUE、偶数ならFALSE
## (1)
print(which(Open[1:10])) # 開いているロッカーのインデックス
## (2)
print(doors[99:101]) # 開閉の合計数
## (3)
print(sum(Open)) # 開いているロッカーの合計数(TRUEの個数)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment