Arc Forumnew | comments | leaders | submitlogin
Superfactorial, noobhelp plz
1 point by globalrev 6057 days ago | 2 comments
so fac(torial) is working but superfactorial is not. meh im a beginner LISPer and this is just confuses me still. superfactorial is sf(4)!= 1!2!3!4! = 288 in python it is: def superfactorial(nbr): y=1 for x in range(1,nbr+1): y=yfactorial(x) return y

i think i am doing that in sufac in Arc but apparenlty im not because i get much bigger answers. help the noob please.

(def fac(n) (if (is n 0) 1 (* n (fac(- n 1)))))

(def sufac (n) (let y 1 (for x 1 (+ n 1) (= y (* y (fac n)))) y))



5 points by sacado 6056 days ago | link

  (def sufac (n) (let y 1 (for x 1 (+ n 1) (= y (* y (fac n)))) y))
The for syntax is (for x first last), not (for x first (+ last 1)) (unlike python). So you have to write :

  (def sufac (n) (let y 1 (for x 1 n (= y (* y (fac n)))) y))
You've also got another bug (a typo I guess) : it's not (fac n) but (fac x) :

  (def sufac (n) (let y 1 (for x 1 n (= y (* y (fac x)))) y))

-----

6 points by rincewind 6056 days ago | link

these are tail-recursive:

  (def fac (n (o m 1)) 
    (if (> n 0) 
      (fac (- n 1) (* m n))
      m))

  (def sfac (n (o m 1)) 
    (if (> n 0) 
      (sfac (- n 1) (* m (fac n))) 
      m))
  
these use higher-order-functions:

  (def fac (n) (apply * (range 1 n)))

  (def sfac (n) (apply * (map fac (range 1 n))))

-----