🖥

0から8までの3桁の数字で、それぞれの桁に数字の重複があるもののパターン数を #Ruby で数えてみる

2019/07/12に公開

Try to count the number of patterns of 3-digit numbers from 0 to 8 with duplicate numbers in each digit with #Ruby

回答

[*0..8].repeated_permutation(3).to_a.select { |numbers| numbers.uniq.size != 3 }.size

# => 225

全ての組み合わせ数

[*0..8].repeated_permutation(3).to_a.size

# => 729

重複するものの内訳

[*0..8].repeated_permutation(3).to_a.select { |numbers| numbers.uniq.size != 3 }

[[0, 0, 0],
[0, 0, 1],
[0, 0, 2],
[0, 0, 3],
[0, 0, 4],
[0, 0, 5],
[0, 0, 6],
[0, 0, 7],
[0, 0, 8],
[0, 1, 0],
[0, 1, 1],
[0, 2, 0],
[0, 2, 2],
[0, 3, 0],
[0, 3, 3],
[0, 4, 0],
[0, 4, 4],
[0, 5, 0],
[0, 5, 5],
[0, 6, 0],
[0, 6, 6],
[0, 7, 0],
[0, 7, 7],
[0, 8, 0],
[0, 8, 8],
[1, 0, 0],
[1, 0, 1],
[1, 1, 0],
[1, 1, 1],
[1, 1, 2],
[1, 1, 3],
[1, 1, 4],
[1, 1, 5],
[1, 1, 6],
[1, 1, 7],
[1, 1, 8],
[1, 2, 1],
[1, 2, 2],
[1, 3, 1],
[1, 3, 3],
[1, 4, 1],
[1, 4, 4],
[1, 5, 1],
[1, 5, 5],
[1, 6, 1],
[1, 6, 6],
[1, 7, 1],
[1, 7, 7],
[1, 8, 1],
[1, 8, 8],
[2, 0, 0],
[2, 0, 2],
[2, 1, 1],
[2, 1, 2],
[2, 2, 0],
[2, 2, 1],
[2, 2, 2],
[2, 2, 3],
[2, 2, 4],
[2, 2, 5],
[2, 2, 6],
[2, 2, 7],
[2, 2, 8],
[2, 3, 2],
[2, 3, 3],
[2, 4, 2],
[2, 4, 4],
[2, 5, 2],
[2, 5, 5],
[2, 6, 2],
[2, 6, 6],
[2, 7, 2],
[2, 7, 7],
[2, 8, 2],
[2, 8, 8],
[3, 0, 0],
[3, 0, 3],
[3, 1, 1],
[3, 1, 3],
[3, 2, 2],
[3, 2, 3],
[3, 3, 0],
[3, 3, 1],
[3, 3, 2],
[3, 3, 3],
[3, 3, 4],
[3, 3, 5],
[3, 3, 6],
[3, 3, 7],
[3, 3, 8],
[3, 4, 3],
[3, 4, 4],
[3, 5, 3],
[3, 5, 5],
[3, 6, 3],
[3, 6, 6],
[3, 7, 3],
[3, 7, 7],
[3, 8, 3],
[3, 8, 8],
[4, 0, 0],
[4, 0, 4],
[4, 1, 1],
[4, 1, 4],
[4, 2, 2],
[4, 2, 4],
[4, 3, 3],
[4, 3, 4],
[4, 4, 0],
[4, 4, 1],
[4, 4, 2],
[4, 4, 3],
[4, 4, 4],
[4, 4, 5],
[4, 4, 6],
[4, 4, 7],
[4, 4, 8],
[4, 5, 4],
[4, 5, 5],
[4, 6, 4],
[4, 6, 6],
[4, 7, 4],
[4, 7, 7],
[4, 8, 4],
[4, 8, 8],
[5, 0, 0],
[5, 0, 5],
[5, 1, 1],
[5, 1, 5],
[5, 2, 2],
[5, 2, 5],
[5, 3, 3],
[5, 3, 5],
[5, 4, 4],
[5, 4, 5],
[5, 5, 0],
[5, 5, 1],
[5, 5, 2],
[5, 5, 3],
[5, 5, 4],
[5, 5, 5],
[5, 5, 6],
[5, 5, 7],
[5, 5, 8],
[5, 6, 5],
[5, 6, 6],
[5, 7, 5],
[5, 7, 7],
[5, 8, 5],
[5, 8, 8],
[6, 0, 0],
[6, 0, 6],
[6, 1, 1],
[6, 1, 6],
[6, 2, 2],
[6, 2, 6],
[6, 3, 3],
[6, 3, 6],
[6, 4, 4],
[6, 4, 6],
[6, 5, 5],
[6, 5, 6],
[6, 6, 0],
[6, 6, 1],
[6, 6, 2],
[6, 6, 3],
[6, 6, 4],
[6, 6, 5],
[6, 6, 6],
[6, 6, 7],
[6, 6, 8],
[6, 7, 6],
[6, 7, 7],
[6, 8, 6],
[6, 8, 8],
[7, 0, 0],
[7, 0, 7],
[7, 1, 1],
[7, 1, 7],
[7, 2, 2],
[7, 2, 7],
[7, 3, 3],
[7, 3, 7],
[7, 4, 4],
[7, 4, 7],
[7, 5, 5],
[7, 5, 7],
[7, 6, 6],
[7, 6, 7],
[7, 7, 0],
[7, 7, 1],
[7, 7, 2],
[7, 7, 3],
[7, 7, 4],
[7, 7, 5],
[7, 7, 6],
[7, 7, 7],
[7, 7, 8],
[7, 8, 7],
[7, 8, 8],
[8, 0, 0],
[8, 0, 8],
[8, 1, 1],
[8, 1, 8],
[8, 2, 2],
[8, 2, 8],
[8, 3, 3],
[8, 3, 8],
[8, 4, 4],
[8, 4, 8],
[8, 5, 5],
[8, 5, 8],
[8, 6, 6],
[8, 6, 8],
[8, 7, 7],
[8, 7, 8],
[8, 8, 0],
[8, 8, 1],
[8, 8, 2],
[8, 8, 3],
[8, 8, 4],
[8, 8, 5],
[8, 8, 6],
[8, 8, 7],
[8, 8, 8]]

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/2263

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-07-12

Discussion