Advertisement
Bodigrim

Draft of number-theory macro

Jun 9th, 2011
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. \def\makeatletter{\catcode `\@11\relax}
  2. \def\makeatother{\catcode `\@12\relax}
  3.  
  4. \makeatletter
  5.  
  6. \newif\ifdivisible
  7. \newcount\testMod@n
  8. \def\testMod#1#2{% sets divisible = [#1 mod #2 == 0]
  9. \testMod@n=#1%
  10. \divide\testMod@n by#2%
  11. \multiply\testMod@n by#2%
  12. \ifnum#1=\testMod@n%
  13. \divisibletrue%
  14. \else%
  15. \divisiblefalse%
  16. \fi%
  17. }
  18.  
  19. \newcount\divisorpower
  20. \newcount\getDivisorPower@n
  21. \def\getDivisorPower#1#2{% sets divisorpower = {max a such that #2^a divides #1} and getDivisorPower@n = #1/#2^a
  22. \getDivisorPower@n=#1%
  23. \divisorpower=0%
  24. \testMod{\getDivisorPower@n}{#2}%
  25. \loop\ifdivisible%
  26. \advance\divisorpower by1%
  27. \divide\getDivisorPower@n by#2%
  28. \testMod{\getDivisorPower@n}{#2}%
  29. \repeat%
  30. }
  31.  
  32. \newcount\numberpower
  33. \newcount\getNumberPower@pow
  34. \def\getNumberPower#1#2{% sets numberpower = #1^#2
  35. \numberpower=1
  36. \getNumberPower@pow=#2
  37. \loop\ifnum\getNumberPower@pow>0
  38. \multiply\numberpower by#1
  39. \advance\getNumberPower@pow by-1
  40. \repeat
  41. }
  42.  
  43. \newif\ifcanonical@more
  44. \newcount\canonical@n
  45. \newcount\canonical@i
  46. \newcount\canonical@sqri
  47.  
  48. \def\canonical#1#2{% runs #2{p}{a} for each p^a such that divides #1 and p^{a+1} does not divide
  49. \canonical@n=#1%
  50. \canonical@i=2%
  51. %
  52. \ifnum\canonical@n<4
  53. \canonical@morefalse%
  54. \else
  55. \canonical@moretrue%
  56. \fi%
  57. %
  58. \loop\ifcanonical@more%
  59. {%
  60. \getDivisorPower{\canonical@n}{\canonical@i}%
  61. \ifnum\divisorpower>0%
  62. #2{\number\canonical@i}{\number\divisorpower}%
  63. \global\canonical@n=\getDivisorPower@n%
  64. \fi%
  65. }%
  66. %
  67. \ifodd\canonical@i%
  68. \advance\canonical@i by2%
  69. \else%
  70. \advance\canonical@i by1%
  71. \fi%
  72. \canonical@sqri=\canonical@i%
  73. \multiply\canonical@sqri by\canonical@sqri%
  74. %
  75. \ifnum\canonical@n<2
  76. \canonical@morefalse%
  77. \fi%
  78. \ifnum\canonical@n<\canonical@sqri%
  79. \canonical@morefalse%
  80. \fi%
  81. \repeat%
  82. %
  83. \ifnum\canonical@n>1
  84. #2{\number\canonical@n}{1}%
  85. \fi
  86. }
  87.  
  88. \def\printCanonHelper#1#2{\cdot#1^#2}
  89. \def\printCanon#1{$$ #1 = 1\canonical{#1}{\printCanonHelper} $$}
  90.  
  91. \newcount\eulerPhi@v
  92. \newcount\eulerPhi@p
  93. \newcount\eulerPhi@a
  94. \def\eulerPhiHelper#1#2{
  95. \eulerPhi@p=#1
  96. \eulerPhi@a=#2
  97. \advance\eulerPhi@a by-1
  98. \getNumberPower{\eulerPhi@p}{\eulerPhi@a}
  99. \advance\eulerPhi@p by-1
  100. \multiply\numberpower by\eulerPhi@p
  101. \global\multiply\eulerPhi@v by\numberpower
  102. }
  103. \def\eulerPhi#1{
  104. \eulerPhi@v=1
  105. \canonical{#1}{\eulerPhiHelper}
  106. $$ \varphi(#1)= \number\eulerPhi@v $$
  107. }
  108.  
  109. \makeatother
  110.  
  111. \printCanon{1546275796}
  112.  
  113. \eulerPhi{1546275796}
  114.  
  115. \end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement