2017年6月15日木曜日

170615

Ruby


Wolstenholme's theorem

1 + 1/2 + ... + 1/(p - 1) ≡ 0 mod p^2,
1 + 1/2^2 + ... + 1/(p - 1)^2 ≡ 0 mod p
を次の方法で確認してみた。

1 + 1/2 + 1/3 + 1/4 ≡ 1 + 13 + 17 + 19 ≡ 50 ≡ 0 mod 5^2,
1 + 1/2^2 + 1/3^2 + 1/4^2 ≡ 1 + 4 + 4 + 1 ≡ 10 ≡ 0 mod 5.

require 'prime'

def f(m, n)
  (0..m - 1).each{|i|
    return i if (i * n - 1) % m == 0
  }
end

n = 50
Prime.each(n){|pr|
  q = pr * pr
  a = []
  (1..pr - 1).each{|i|
    a << f(q, i)
  }
  j = a.inject(:+)
  p [a, j, j % q]
}

Prime.each(n){|pr|
  a = []
  (1..pr - 1).each{|i|
    a << f(pr, i * i)
  }
  j = a.inject(:+)
  p [a, j, j % pr]
}

出力結果
[[1], 1, 1]
[[1, 5], 6, 6]
[[1, 13, 17, 19], 50, 0]
[[1, 25, 33, 37, 10, 41], 147, 0]
[[1, 61, 81, 91, 97, 101, 52, 106, 27, 109], 726, 0]
[[1, 85, 113, 127, 34, 141, 145, 148, 94, 17, 123, 155], 1183, 0]
[[1, 145, 193, 217, 58, 241, 124, 253, 257, 29, 184, 265, 89, 62, 212, 271], 2601, 0]
[[1, 181, 241, 271, 289, 301, 258, 316, 321, 325, 197, 331, 250, 129, 337, 158, 85, 341], 4332, 0]
[[1, 265, 353, 397, 106, 441, 378, 463, 294, 53, 481, 485, 407, 189, 388, 496, 249, 147, 362, 291, 126, 505], 6877, 0]
[[1, 421, 561, 631, 673, 701, 721, 736, 187, 757, 153, 771, 647, 781, 785, 368, 99, 514, 664, 799, 801, 497, 256, 806, 471, 744, 623, 811], 15979, 0]
[[1, 481, 641, 721, 769, 801, 412, 841, 534, 865, 699, 881, 74, 206, 897, 901, 848, 267, 607, 913, 778, 830, 585, 921, 346, 37, 178, 103, 232, 929], 17298, 0]
[[1, 685, 913, 1027, 274, 1141, 978, 1198, 1217, 137, 249, 1255, 316, 489, 1004, 599, 1208, 1293, 1297, 753, 326, 809, 1250, 1312, 1150, 158, 862, 929, 1133, 502, 265, 984, 83, 604, 1017, 1331], 28749, 0]
[[1, 841, 1121, 1261, 1345, 1401, 1441, 1471, 934, 1513, 917, 1541, 388, 1561, 1569, 1576, 890, 467, 177, 1597, 1601, 1299, 804, 1611, 269, 194, 1432, 1621, 58, 1625, 1193, 788, 866, 445, 1633, 1074, 1363, 929, 1250, 1639], 43706, 0]
[[1, 925, 1233, 1387, 370, 1541, 1585, 1618, 411, 185, 1681, 1695, 569, 1717, 1356, 809, 1414, 1130, 292, 1017, 1761, 1765, 402, 1772, 74, 1209, 137, 1783, 1594, 678, 1014, 1329, 1793, 707, 317, 565, 50, 146, 806, 1433, 451, 1805], 42527, 0]
[[1, 1105, 1473, 1657, 442, 1841, 1578, 1933, 491, 221, 1205, 2025, 170, 789, 1620, 2071, 130, 1350, 1744, 1215, 526, 1707, 2113, 2117, 972, 85, 900, 1499, 1752, 810, 1924, 2140, 1138, 65, 1641, 675, 1612, 872, 793, 1712, 1778, 263, 411, 1958, 540, 2161], 55225, 0]
[[1], 1, 1]
[[1, 1], 2, 2]
[[1, 4, 4, 1], 10, 0]
[[1, 2, 4, 4, 2, 1], 14, 0]
[[1, 3, 5, 9, 4, 4, 9, 5, 3, 1], 44, 0]
[[1, 10, 3, 9, 12, 4, 4, 12, 9, 3, 10, 1], 78, 0]
[[1, 13, 2, 16, 15, 9, 8, 4, 4, 8, 9, 15, 16, 2, 13, 1], 136, 0]
[[1, 5, 17, 6, 16, 9, 7, 11, 4, 4, 11, 7, 9, 16, 6, 17, 5, 1], 152, 0]
[[1, 6, 18, 13, 12, 16, 8, 9, 2, 3, 4, 4, 3, 2, 9, 8, 16, 12, 13, 18, 6, 1], 184, 0]
[[1, 22, 13, 20, 7, 25, 16, 5, 24, 9, 6, 28, 23, 4, 4, 23, 28, 6, 9, 24, 5, 16, 25, 7, 20, 13, 22, 1], 406, 0]
[[1, 8, 7, 2, 5, 25, 19, 16, 18, 9, 10, 14, 20, 28, 4, 4, 28, 20, 14, 10, 9, 18, 16, 19, 25, 5, 2, 7, 8, 1], 372, 0]
[[1, 28, 33, 7, 3, 36, 34, 11, 16, 10, 26, 9, 30, 27, 25, 12, 21, 4, 4, 21, 12, 25, 27, 30, 9, 26, 10, 16, 11, 34, 36, 3, 7, 33, 28, 1], 666, 0]
[[1, 31, 32, 18, 23, 8, 36, 25, 40, 16, 20, 2, 33, 9, 39, 37, 21, 10, 5, 4, 4, 5, 10, 21, 37, 39, 9, 33, 2, 20, 16, 40, 25, 36, 8, 23, 18, 32, 31, 1], 820, 0]
[[1, 11, 24, 35, 31, 6, 36, 41, 17, 40, 16, 23, 14, 9, 13, 21, 25, 15, 38, 10, 4, 4, 10, 38, 15, 25, 21, 13, 9, 14, 23, 16, 40, 17, 41, 36, 6, 31, 35, 24, 11, 1], 860, 0]
[[1, 12, 21, 3, 32, 17, 24, 36, 18, 8, 7, 16, 42, 6, 14, 9, 27, 28, 25, 2, 34, 37, 4, 4, 37, 34, 2, 25, 28, 27, 9, 14, 6, 42, 16, 7, 8, 18, 36, 24, 17, 32, 3, 21, 12, 1], 846, 0]

0 件のコメント:

コメントを投稿