Arc Forumnew | comments | leaders | submitlogin
Number theory in Arc (ycombinator.com)
4 points by waterhouse 4961 days ago | 4 comments


1 point by waterhouse 4961 days ago | link

For reference, here are the definitions of "mod-expt", "mapn", and "num->digs" (which I used), plus their immediate dependencies.

  (def fast-expt-* (a n one *)
    (xloop (a a n n tt one)
      (if (is n 0)
          tt
          even.n
          (next (* a a) (/ n 2) tt)
          (next a dec.n (* a tt)))))
  (def mod-expt (a n m)
    (fast-expt-* a n 1 (fn (x y) (mod (* x y) m))))

  (def partial (f x)
    (fn args (apply f x args)))
  ;(mapn f a b c d) = (mapn (fn (x) (mapn [f x _] c d)) a b)
  (def mapn (f a b . xs)
    (if no.xs
        (map f (range a b))
        (mapn [apply mapn (partial f _)
                     xs]
              a b)))

  (= div $.quotient)
  (def accumulate (comb f xs init next done)
    ((afn (xs total)
       (if (done xs)
           total
           (self (next xs) (comb (f xs) total))))
     xs init))
  (def num->digs (n (o base 10) (o size nil))
    (let u (accumulate cons [mod _ base] n nil [div _ base] [is _ 0])
      (if no.size
          u
          (join (n-of (- size len.u) 0)
                u))))

-----

2 points by waterhouse 4961 days ago | link

Also, to comment on my naming practices (the function I called "ass"): I have to give it a name, 'cause I intend to type it repeatedly into the REPL. However, I don't want to spend time coming up with a good descriptive name, especially since I changed my mind at least once about what the function was supposed to do while writing it. (Originally I was going to make it return the ratio [number of powers of 2 with a 0 mod 10^k] / [number of powers of 2 mod 10^k]. But then I decided to make it return the list of those two quantities, instead. Had I given it a descriptive name, I would then have had to change it.)

Therefore, the thing to do is to give it a name I can remember in the immediate future. Also it should be short, so I can type it easily. So swearwords are an obvious choice, and I use them frequently. Sometimes I also use nonsense syllables or one- or two-character abbreviations; incidentally, I favor "u" as a local variable because it's the left index finger's home key on the Dvorak keyboard (which I use).

By the time I'm actually ready to save the definition to a file for future use, usually I've settled on the final form of the function, and I give it a better name. So my source code is usually more sensical and less obscene.

-----

3 points by akkartik 4961 days ago | link

I'm not sure why this was marked dead. Resuscitated.

-----

1 point by waterhouse 4961 days ago | link

Aha. Thank you. And that explains why I couldn't comment on it. That is surprising...

-----