factor.bf 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. * factor an arbitrarily large positive integer
  2. *
  3. * Copyright (C) 1999 by Brian Raiter
  4. * under the GNU General Public License
  5. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>-
  6. *
  7. * read in the number
  8. *
  9. <<<<<<<<<+
  10. [-[>>>>>>>>>>][-]<<<<<<<<<<[[->>>>>>>>>>+<<<<<<<<<<]<<<<<<<<<<]
  11. >>>>>>>>>>,----------]
  12. >>>>>>>>>>[------------------------------------->>>>>>>>>->]
  13. <[+>[>>>>>>>>>+>]<-<<<<<<<<<<]-
  14. *
  15. * display the number and initialize the loop variable to two
  16. *
  17. [>++++++++++++++++++++++++++++++++++++++++++++++++.
  18. ------------------------------------------------<<<<<<<<<<<]
  19. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
  20. --------------------------.[-]
  21. >>>>>>>>>>>>++<<<<+
  22. *
  23. * the main loop
  24. *
  25. [ [-]>>
  26. *
  27. * make copies of the number and the loop variable
  28. *
  29. [>>>>[-]>[-]>[-]>[-]
  30. >[-]>[-]
  31. <<<<<<<[->>>+>+<<<<]>>>>>>>>]
  32. <<<<<<<<<<[>>>>>>[-<<<<+>>>>]<<<<<<<<<<<<<<<<]>>>>>>>>>>
  33. [>[->>>+>>+<<<<<]>>>>>>>>>]
  34. <<<<<<<<<<[>>>>>>[-<<<<<+>>>>>]<<<<<<<<<<<<<<<<]>>>>>>>>>>
  35. *
  36. * divide the number by the loop variable
  37. *
  38. [>>>[-]>>>[-]>[-]>>>] initialize
  39. <<<<<<<<<<[<<<<<<<<<<]
  40. >>>>>>>>>[-]>>>>>>>+<<<<<<<<[+]+
  41. [ ->> double divisor until above dividend
  42. [>>>>>>[->++<]>>>>]<<<<<<<<<<
  43. [>>>>>>>>[-]>[-]
  44. <<<<[->>>++<<<]<<<<<<<<<<<<<<<]>>>>>>>>>>
  45. [>>>>>>>>[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<
  46. [->--------->>>>>>>>>+<<<<<<<<<<[->+<]]]]]]]]]]]>>]
  47. <<<<<<<<<<[>>>>>>>>>[-<+<<<+>>>>]<<<<<<<<<<<<<<<<<<<]>>>>>>>>>>
  48. [>>>>>>>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>
  49. [-<--------->>>>>>>>>>>+<<<<<<<<<<[-<+>]]]]]]]]]]]>>>]
  50. <<<<<<<<<<
  51. [>>>>[->>>+>>+<<<<<]<<<<<<<<<<<<<<]
  52. >>>>>>>>>>[>>>>>>>[-<<<+>>>]>>>]<<<<<<<<<<
  53. [>>>>>>>>[->-<]>
  54. [<<<<<<<<<[<[-]>>>>>>>>>>[-<<<<<<<<<<+>>>>>>>>>>]<<<<<<<<<<<<<<<<<<<]
  55. >>>>>>>>>>>>>>>>>>>]
  56. <<<<<<<<<<<<<<<<<<<]
  57. >>>>>>>>>[+[+[+[+[+[+[+[+[+[+[[-]<+>]]]]]]]]]]]<
  58. ]
  59. >>>>>>>>
  60. [ subtract divisor from dividend
  61. <<<<<<
  62. [>>>>>>>>[-]>[-]<<<<<[->>>+>+<<<<]>>>>>>]<<<<<<<<<<
  63. [>>>>>>>>[-<<<<+>>>>]<<<[->>>+>+<<<<]<<<<<<<<<<<<<<<]>>>>>>>>>>
  64. [>>>>>>>>>[-<<<<+>>>>]>]<<<<<<<<<<
  65. [>>>>>>>>[-<->]<<<<<<<<<<<<<<<<<<]>>>>>>>>>>
  66. [>>>>>>>[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<
  67. [++++++++++[+>-<]>>>>>>>>>>-<<<<<<<<<<]]]]]]]]]]]>>>]
  68. >>>>>>>+
  69. [ if difference is nonnegative then
  70. [-]<<<<<<<<<<<<<<<<< replace dividend and increment quotient
  71. [>>>>[-]>>>>[-<<<<+>>>>]<<[->>+<<]<<<<<<<<<<<<<<<<]>>>>>>>>>>
  72. [>>>>>>>>[->+<<<+>>]>>]<<<<<<<<<<
  73. [>>>[->>>>>>+<<<<<<]<<<<<<<<<<<<<]>>>>>>>>>>
  74. [>>>>>>>>>[-<<<<<<+>>>>>>[-<<<<<<+>>>>>>
  75. [-<<<<<<+>>>>>>[-<<<<<<+>>>>>>
  76. [-<<<<<<+>>>>>>[-<<<<<<+>>>>>>
  77. [-<<<<<<+>>>>>>[-<<<<<<+>>>>>>
  78. [-<<<<<<+>>>>>>[-<<<<<<--------->>>>>>>>>>>>>>>>+<<<<<<<<<<
  79. [-<<<<<<+>>>>>>]]]]]]]]]]]>]
  80. >>>>>>>
  81. ] halve divisor and loop until zero
  82. <<<<<<<<<<<<<<<<<[<<<<<<<<<<]>>>>>>>>>>
  83. [>>>>>>>>[-]<<[->+<]<[->>>+<<<]>>>>>]<<<<<<<<<<
  84. [+>>>>>>>[-<<<<<<<+>>>>>>>[-<<<<<<<->>>>>>+>
  85. [-<<<<<<<+>>>>>>>[-<<<<<<<->>>>>>+>
  86. [-<<<<<<<+>>>>>>>[-<<<<<<<->>>>>>+>
  87. [-<<<<<<<+>>>>>>>[-<<<<<<<->>>>>>+>
  88. [-<<<<<<<+>>>>>>>]]]]]]]]]<<<<<<<
  89. [->>>>>>>+<<<<<<<]-<<<<<<<<<<]
  90. >>>>>>>
  91. [-<<<<<<<<<<<+>>>>>>>>>>>]
  92. >>>[>>>>>>>[-<<<<<<<<<<<+++++>>>>>>>>>>>]>>>]<<<<<<<<<<
  93. [+>>>>>>>>[-<<<<<<<<+>>>>>>>>[-<<<<<<<<->>>>>+>>>
  94. [-<<<<<<<<+>>>>>>>>[-<<<<<<<<->>>>>+>>>
  95. [-<<<<<<<<+>>>>>>>>[-<<<<<<<<->>>>>+>>>
  96. [-<<<<<<<<+>>>>>>>>[-<<<<<<<<->>>>>+>>>
  97. [-<<<<<<<<+>>>>>>>>]]]]]]]]]<<<<<<<<
  98. [->>>>>>>>+<<<<<<<<]-<<<<<<<<<<]
  99. >>>>>>>>[-<<<<<<<<<<<<<+>>>>>>>>>>>>>]>>
  100. [>>>>>>>>[-<<<<<<<<<<<<<+++++>>>>>>>>>>>>>]>>]<<<<<<<<<<
  101. [<<<<<<<<<<]>>>>>>>>>>
  102. >>>>>>
  103. ]
  104. <<<<<<
  105. *
  106. * make copies of the loop variable and the quotient
  107. *
  108. [>>>[->>>>+>+<<<<<]>>>>>>>]
  109. <<<<<<<<<<
  110. [>>>>>>>[-<<<<+>>>>]<<<<<[->>>>>+>>+<<<<<<<]<<<<<<<<<<<<]
  111. >>>>>>>>>>[>>>>>>>[-<<<<<+>>>>>]>>>]<<<<<<<<<<
  112. *
  113. * break out of the loop if the quotient is larger than the loop variable
  114. *
  115. [>>>>>>>>>[-<->]<
  116. [<<<<<<<<
  117. [<<[-]>>>>>>>>>>[-<<<<<<<<<<+>>>>>>>>>>]<<<<<<<<<<<<<<<<<<]
  118. >>>>>>>>>>>>>>>>>>]<<<<<<<<<<<<<<<<<<]
  119. >>>>>>>>[>-<[+[+[+[+[+[+[+[+[+[[-]>+<]]]]]]]]]]]>+
  120. [ [-]
  121. *
  122. * partially increment the loop variable
  123. *
  124. <[-]+>>>>+>>>>>>>>[>>>>>>>>>>]<<<<<<<<<<
  125. *
  126. * examine the remainder for nonzero digits
  127. *
  128. [<<<<<<[<<<<[<<<<<<<<<<]>>>>+<<<<<<<<<<]<<<<]
  129. >>>>>>>>>>>>>>>>>>>>[>>>>>>>>>>]<<<<<<<<<<[<<<<<<<<<<]
  130. >>>>-
  131. [ [+]
  132. *
  133. * decrement the loop variable and replace the number with the quotient
  134. *
  135. >>>>>>>>-<<[>[-]>>[-<<+>>]>>>>>>>]<<<<<<<<<<
  136. *
  137. * display the loop variable
  138. *
  139. [+>>[>>>>>>>>+>>]<<-<<<<<<<<<<]-
  140. [>>++++++++++++++++++++++++++++++++++++++++++++++++.
  141. ------------------------------------------------<<<<<<<<<<<<]
  142. ++++++++++++++++++++++++++++++++.[-]>>>>
  143. ]
  144. *
  145. * normalize the loop variable
  146. *
  147. >>>>>>
  148. [>>[->>>>>+<<<<<[->>>>>+<<<<<
  149. [->>>>>+<<<<<[->>>>>+<<<<<
  150. [->>>>>+<<<<<[->>>>>+<<<<<
  151. [->>>>>+<<<<<[->>>>>+<<<<<
  152. [->>>>>+<<<<<[->>>>>--------->>>>>+<<<<<<<<<<
  153. [->>>>>+<<<<<]]]]]]]]]]]>>>>>>>>]
  154. <<<<<<<<<<[>>>>>>>[-<<<<<+>>>>>]<<<<<<<<<<<<<<<<<]
  155. >>>>>>>>>
  156. ]<
  157. ]>>
  158. *
  159. * display the number and end
  160. *
  161. [>>>>>>>>>>]<<<<<<<<<<[+>[>>>>>>>>>+>]<-<<<<<<<<<<]-
  162. [>++++++++++++++++++++++++++++++++++++++++++++++++.<<<<<<<<<<<]
  163. ++++++++++.