group_numbers.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from sympy.core import Integer, Pow, Mod
  2. from sympy import factorint
  3. def is_nilpotent_number(n):
  4. """
  5. Check whether `n` is a nilpotent number. A number `n` is said to be
  6. nilpotent if and only if every finite group of order `n` is nilpotent.
  7. For more information see [1]_.
  8. Examples
  9. ========
  10. >>> from sympy.combinatorics.group_numbers import is_nilpotent_number
  11. >>> from sympy import randprime
  12. >>> is_nilpotent_number(21)
  13. False
  14. >>> is_nilpotent_number(randprime(1, 30)**12)
  15. True
  16. References
  17. ==========
  18. .. [1] Pakianathan, J., Shankar, K., *Nilpotent Numbers*,
  19. The American Mathematical Monthly, 107(7), 631-634.
  20. """
  21. if n <= 0 or int(n) != n:
  22. raise ValueError("n must be a positive integer, not %i" % n)
  23. n = Integer(n)
  24. prime_factors = list(factorint(n).items())
  25. is_nilpotent = True
  26. for p_j, a_j in prime_factors:
  27. for p_i, a_i in prime_factors:
  28. if any([Mod(Pow(p_i, k), p_j) == 1 for k in range(1, a_i + 1)]):
  29. is_nilpotent = False
  30. break
  31. if not is_nilpotent:
  32. break
  33. return is_nilpotent
  34. def is_abelian_number(n):
  35. """
  36. Check whether `n` is an abelian number. A number `n` is said to be abelian
  37. if and only if every finite group of order `n` is abelian. For more
  38. information see [1]_.
  39. Examples
  40. ========
  41. >>> from sympy.combinatorics.group_numbers import is_abelian_number
  42. >>> from sympy import randprime
  43. >>> is_abelian_number(4)
  44. True
  45. >>> is_abelian_number(randprime(1, 2000)**2)
  46. True
  47. >>> is_abelian_number(60)
  48. False
  49. References
  50. ==========
  51. .. [1] Pakianathan, J., Shankar, K., *Nilpotent Numbers*,
  52. The American Mathematical Monthly, 107(7), 631-634.
  53. """
  54. if n <= 0 or int(n) != n:
  55. raise ValueError("n must be a positive integer, not %i" % n)
  56. n = Integer(n)
  57. if not is_nilpotent_number(n):
  58. return False
  59. prime_factors = list(factorint(n).items())
  60. is_abelian = all(a_i < 3 for p_i, a_i in prime_factors)
  61. return is_abelian
  62. def is_cyclic_number(n):
  63. """
  64. Check whether `n` is a cyclic number. A number `n` is said to be cyclic
  65. if and only if every finite group of order `n` is cyclic. For more
  66. information see [1]_.
  67. Examples
  68. ========
  69. >>> from sympy.combinatorics.group_numbers import is_cyclic_number
  70. >>> from sympy import randprime
  71. >>> is_cyclic_number(15)
  72. True
  73. >>> is_cyclic_number(randprime(1, 2000)**2)
  74. False
  75. >>> is_cyclic_number(4)
  76. False
  77. References
  78. ==========
  79. .. [1] Pakianathan, J., Shankar, K., *Nilpotent Numbers*,
  80. The American Mathematical Monthly, 107(7), 631-634.
  81. """
  82. if n <= 0 or int(n) != n:
  83. raise ValueError("n must be a positive integer, not %i" % n)
  84. n = Integer(n)
  85. if not is_nilpotent_number(n):
  86. return False
  87. prime_factors = list(factorint(n).items())
  88. is_cyclic = all(a_i < 2 for p_i, a_i in prime_factors)
  89. return is_cyclic