factor.bf (5902B) - raw


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