Just testing you

Just testing you

john.uhden
Mentor Mentor
788 Views
10 Replies
Message 1 of 11

Just testing you

john.uhden
Mentor
Mentor

What's wrong with this code?

(defun factorial (n / product)
  (setq i 0 product 1)
  (repeat n
    (setq i (1+ i) product (* i product))
  )
  product
)

John F. Uhden

0 Likes
789 Views
10 Replies
Replies (10)
Message 2 of 11

pbejse
Mentor
Mentor

@john.uhden wrote:

What's wrong with this code?


 

It works as expected. <--- it does have a limit,  like most things  in lisp that deals with integers

 

(defun factorial (n / product)
  (setq i 0.0 product 1)
  (repeat n
    (setq i (1+ i) product (* i product))
  )
  (fix product)
)

 

 

 

0 Likes
Message 3 of 11

john.uhden
Mentor
Mentor
@pbejse
You sorta beat around the bush with that answer, but You deserve an A+ for
demonstrating that you totally understood. Yes, I saw that you changed one
of the operands to a 'REAL.
Seems like even long integers have their limit in AutoLisp.

Anyone else?

John F. Uhden

Message 4 of 11

hak_vz
Advisor
Advisor
(defun factorial (n / product)
  (setq i 0 product 1.0)
  (repeat n
    (setq i (1+ i) product (* i product))
  )
  product
)

 

Yes. Integers have limits in autolisp too.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 5 of 11

dbroad
Mentor
Mentor

IMO, the ideal factorial function should be recursive, not iterative. It is the paradigm for a recursive definition. If the user wants a real result, then, he should provide a real argument, else, he should accept the limitations of integers.

(defun factorial (n)
  (if (= n 1) 1
  (* n (factorial (1- n)))))

Since its only defined for positive whole numbers(integers in real math), that is all you should need to do.

Architect, Registered NC, VA, SC, & GA.
Message 6 of 11

john.uhden
Mentor
Mentor
@Anonymous
Whoa! I ain't never been too good with recursion.
Gotta study that and probably give you an A++.
I will do a time test, of course.

John F. Uhden

0 Likes
Message 7 of 11

dbroad
Mentor
Mentor

Thanks but time tests are probably irrelevant.  Factorial results increase faster than exponentially.  You probably won't want a number larger than 64 as input.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 8 of 11

john.uhden
Mentor
Mentor

Next question:

What's the largest integer that AutoLisp can handle?

 

John F. Uhden

0 Likes
Message 9 of 11

Jeff_M
Consultant
Consultant

@john.uhden wrote:

Next question:

What's the largest integer that AutoLisp can handle?

 


2147483647

https://help.autodesk.com/view/ACD/2015/ENU/?guid=GUID-EF6114FC-F1E4-4C71-91CC-07D01E6C8ABB

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 10 of 11

john.uhden
Mentor
Mentor
Hi, Jeff.
Well done, even though you cheated.
Didja notice it's one shy of (expt 2 31)?
which turns negative and (abs) can do nothing to it.

John F. Uhden

0 Likes
Message 11 of 11

martti.halminen
Collaborator
Collaborator

This one also works in Common Lisp:

CLO 17 > (factorial 92)
12438414054641307255475324325873553077577991715875414356840239582938137710983519518443046123837041347353107486982656753664000000000000000000000

 

- The trick here is that CL has bignums when it runs out of space on integers.

- Also demonstrates the problem with recursive functions, on 93 I get:

 

Stack overflow (stack size 15998).
[Condition of type CONDITIONS:STACK-OVERFLOW]

Restarts:
0: [CONTINUE] Extend stack by 50%.
1: [ABORT] Return to top loop level 0.
2: [ABORT] Quit process.

 

So if I really needed larger ones, I'd need to change to another algorithm (iterative or  recursive with memoizing or using an accumulator).

 

-- 

0 Likes