chinese_prime_signature.sf 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 02 June 2022
  4. # https://github.com/trizen
  5. # Compute the least Chinese signature of n with respect to the prime numbers,
  6. # such that when the Chinese Remainder Theorem (CRT) is aplied, n is constructed.
  7. # Example:
  8. # The prime Chinese signature of 53 is [1,2,3,4], which represents:
  9. # 53 == 1 (mod 2)
  10. # 53 == 2 (mod 3)
  11. # 53 == 3 (mod 5)
  12. # 53 == 4 (mod 7)
  13. # By applying the CRT, we have:
  14. # chinese(Mod(1,2), Mod(2,3), Mod(3,5), Mod(4,7)) == 53
  15. func chinese_prime_signature(n) {
  16. var crt = Mod(n, 2)
  17. var sgn = [crt.lift]
  18. 3..n -> each_prime {|p|
  19. if (crt.lift == n) {
  20. break
  21. }
  22. var r = Mod(n, p)
  23. crt = chinese(crt, r)
  24. sgn << r.lift
  25. }
  26. return sgn
  27. }
  28. for n in (1..35) {
  29. say "#{n} = #{chinese_prime_signature(n)}"
  30. }
  31. __END__
  32. 1 = [1]
  33. 2 = [0]
  34. 3 = [1, 0]
  35. 4 = [0, 1]
  36. 5 = [1, 2]
  37. 6 = [0, 0, 1]
  38. 7 = [1, 1, 2]
  39. 8 = [0, 2, 3]
  40. 9 = [1, 0, 4]
  41. 10 = [0, 1, 0]
  42. 11 = [1, 2, 1]
  43. 12 = [0, 0, 2]
  44. 13 = [1, 1, 3]
  45. 14 = [0, 2, 4]
  46. 15 = [1, 0, 0]
  47. 16 = [0, 1, 1]
  48. 17 = [1, 2, 2]
  49. 18 = [0, 0, 3]
  50. 19 = [1, 1, 4]
  51. 20 = [0, 2, 0]
  52. 21 = [1, 0, 1]
  53. 22 = [0, 1, 2]
  54. 23 = [1, 2, 3]
  55. 24 = [0, 0, 4]
  56. 25 = [1, 1, 0]
  57. 26 = [0, 2, 1]
  58. 27 = [1, 0, 2]
  59. 28 = [0, 1, 3]
  60. 29 = [1, 2, 4]
  61. 30 = [0, 0, 0, 2]
  62. 31 = [1, 1, 1, 3]
  63. 32 = [0, 2, 2, 4]
  64. 33 = [1, 0, 3, 5]
  65. 34 = [0, 1, 4, 6]
  66. 35 = [1, 2, 0, 0]