chinese_signature.sf 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 02 August 2020
  4. # https://github.com/trizen
  5. # Compute the least Chinese signature of n with respect to the positive integers,
  6. # such that when the Chinese Remainder Theorem (CRT) is aplied, n is constructed.
  7. # Example:
  8. # The Chinese signature of 53 is [0, 1, 2, 1, 3], which represents:
  9. # 53 == 0 (mod 1)
  10. # 53 == 1 (mod 2)
  11. # 53 == 2 (mod 3)
  12. # 53 == 1 (mod 4)
  13. # 53 == 3 (mod 5)
  14. # By applying the CRT, we have:
  15. # chinese(Mod(0,1), Mod(1,2), Mod(2,3), Mod(1,4), Mod(3,5)) == 53
  16. func chinese_signature(n) {
  17. var crt = Mod(0, 1)
  18. var sgn = [crt.lift]
  19. for k in (2..Inf) {
  20. if (crt.lift == n) {
  21. break
  22. }
  23. var r = Mod(n, k)
  24. crt = chinese(crt, r)
  25. sgn << r.lift
  26. }
  27. return sgn
  28. }
  29. for n in (0..50) {
  30. say "#{n} = #{chinese_signature(n)}"
  31. }
  32. __END__
  33. 0 = [0]
  34. 1 = [0, 1]
  35. 2 = [0, 0, 2]
  36. 3 = [0, 1, 0]
  37. 4 = [0, 0, 1]
  38. 5 = [0, 1, 2]
  39. 6 = [0, 0, 0, 2]
  40. 7 = [0, 1, 1, 3]
  41. 8 = [0, 0, 2, 0]
  42. 9 = [0, 1, 0, 1]
  43. 10 = [0, 0, 1, 2]
  44. 11 = [0, 1, 2, 3]
  45. 12 = [0, 0, 0, 0, 2]
  46. 13 = [0, 1, 1, 1, 3]
  47. 14 = [0, 0, 2, 2, 4]
  48. 15 = [0, 1, 0, 3, 0]
  49. 16 = [0, 0, 1, 0, 1]
  50. 17 = [0, 1, 2, 1, 2]
  51. 18 = [0, 0, 0, 2, 3]
  52. 19 = [0, 1, 1, 3, 4]
  53. 20 = [0, 0, 2, 0, 0]
  54. 21 = [0, 1, 0, 1, 1]
  55. 22 = [0, 0, 1, 2, 2]
  56. 23 = [0, 1, 2, 3, 3]
  57. 24 = [0, 0, 0, 0, 4]
  58. 25 = [0, 1, 1, 1, 0]
  59. 26 = [0, 0, 2, 2, 1]
  60. 27 = [0, 1, 0, 3, 2]
  61. 28 = [0, 0, 1, 0, 3]
  62. 29 = [0, 1, 2, 1, 4]
  63. 30 = [0, 0, 0, 2, 0]
  64. 31 = [0, 1, 1, 3, 1]
  65. 32 = [0, 0, 2, 0, 2]
  66. 33 = [0, 1, 0, 1, 3]
  67. 34 = [0, 0, 1, 2, 4]
  68. 35 = [0, 1, 2, 3, 0]
  69. 36 = [0, 0, 0, 0, 1]
  70. 37 = [0, 1, 1, 1, 2]
  71. 38 = [0, 0, 2, 2, 3]
  72. 39 = [0, 1, 0, 3, 4]
  73. 40 = [0, 0, 1, 0, 0]
  74. 41 = [0, 1, 2, 1, 1]
  75. 42 = [0, 0, 0, 2, 2]
  76. 43 = [0, 1, 1, 3, 3]
  77. 44 = [0, 0, 2, 0, 4]
  78. 45 = [0, 1, 0, 1, 0]
  79. 46 = [0, 0, 1, 2, 1]
  80. 47 = [0, 1, 2, 3, 2]
  81. 48 = [0, 0, 0, 0, 3]
  82. 49 = [0, 1, 1, 1, 4]
  83. 50 = [0, 0, 2, 2, 0]