4 points by shader 3559 days ago | link | parent Nice. I only did the gcd function, so you've got an amazing head start on me. However, the version of gcd that I did was Euclid's algorithm. Probably faster than prime factorization; and I included a case for only one number, and a list of greater than two.Since I couldn't commit to github for some reason, here's the code:`````` ;;Int (predicate) (def int (n) "Returns true if n is an integer; i.e. has no decimal part." (is (trunc n) n)) ; return true if the number is the same as itself without the decimal part ;;Greatest Common Denominator (def gcd l "returns the greatest common denominator, or divisor, of a list of numbers. Numbers should be integers, or the result will default to 0." (with (a (car l) c (cdr l)) (if (len> c 1) (gcd a (gcd (car c) (cdr c))) ; handle lists of more than two numbers (no a) 0 (no c) (abs a) (let b (car (flat c)) (if (or (~int a) (~int b)) 0 ; skip non-integers (is a b) a ; return common divisor (if (> a b) (gcd (- a b) b) (gcd (- b a) a))))))) `````` Update:Figured out what I was doing wrong with github. Math library now started in lib/math.arc. Currently it only contains my gcd function and the 'int test, but feel free to add any other math functions.In the future, we may need to make it a math folder, with individual files for trig functions, hyperbolic functions, calculus, matrix and vector function, quaternions, etc. But for now one file is more than enough to contain my lowly gcd :)
 1 point by Darmani 3559 days ago | link `````` arc>(def int (n) (is (trunc n) n)) # arc>(int 1.0) nil `````` My original int? was identical to yours, but that's why I changed it. Although, now that I think about it, since prime-factorization discards the results of operations that cast n to a rational, that's unnecessary. Still, there will need to be separate functions for testing if a num's type is int (which yours does), and testing if a num lacks decimal places (which mine does).(And yes, Euclid's is definitely faster than prime factorization -- I originally looked at better algorithms for prime finding and gcd but I decided to settle for the "pure" approach -- sieve instead of some Fermat's or another fast primality test, prime factorization based instead of Euclid's.)P.S.: Feel free to make use of the code I posted.-----
 1 point by almkglor 3548 days ago | link 1) No, you should implement in the math in the underlying machine instructions, which are guaranteed to be as precise and as fast as the manufacturer can make it. The underlying machine instructions are fortunately possible to access in standard C libraries, and the standard C library functions are wrapped by mzscheme, which we then import in arc.2) It should be, and it isn't.`````` (defmemo fac (n) ((afn (n a) (if (> n 1) (self (- n 1) (* a n)) a)) n 1)) `````` 3) Yes, arc-on-mzscheme handles this automagically. arc2c does not (I think it'll overflow)-----