Arc Forumnew | comments | leaders | submitlogin
2 points by cchooper 6096 days ago | link | parent

You don't need to check that pow is less than zero in that last case.

  (def power (nbr pow)
    (if (is pow 0) 1
        (> pow 0)
	    (let acc nbr
    		(for x 1 (- pow 1)
        		(= acc (* acc nbr))) acc)
	(let acc 1
    	    (for x pow -1
		(= acc (/ acc nbr))) (/ acc 1.0))))

  (def power (nbr pow)
    (if (is pow 0) 
	    1
        (> pow 0)
	    (* nbr (power nbr (- pow 1)))
	(/ 1 (power nbr (* -1 pow)))))
Of course, if you want it to be really fast...

  (time (expt 234 34445)) => 101 msec.


1 point by globalrev 6091 days ago | link

how can it be so much faster? its written in C or something? its built in the language somehow i guess, its not possible to recreate for me by writing a def?

-----

1 point by sacado 6091 days ago | link

It is probably written in C and does not check its args every time it is called. Basically, every time you call 'is, '<, '+, or any other primitive operation, it checks whether its arguments are numbers, then performs the operation. All these checks are useless since, within the body of your function, you know the type of your args are numbers, but you cannot avoid them anyway. Optimization will come, later :)

-----