all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bigger integers
@ 2004-09-02 22:28 Joe Corneli
  0 siblings, 0 replies; 11+ messages in thread
From: Joe Corneli @ 2004-09-02 22:28 UTC (permalink / raw)



This fails on big numbers like 12.

 (defun factorial (num)
   (if (<= num 0)
       1
     (* num (factorial (1- num)))))

Suggestions on how to make it work for bigger numbers?

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: bigger integers
       [not found] <mailman.1324.1094164438.1998.help-gnu-emacs@gnu.org>
@ 2004-09-02 23:10 ` Kevin Rodgers
  2004-09-03  2:11 ` Pascal Bourguignon
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Kevin Rodgers @ 2004-09-02 23:10 UTC (permalink / raw)


Joe Corneli wrote:
 > Suggestions on how to make it work for bigger numbers?

Get a bigger computer :-)

(defun factorial (num)
   (if (<= num 0)
       1.0
     (* num (factorial (1- num)))))

-- 
Kevin Rodgers

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: bigger integers
       [not found] <mailman.1324.1094164438.1998.help-gnu-emacs@gnu.org>
  2004-09-02 23:10 ` bigger integers Kevin Rodgers
@ 2004-09-03  2:11 ` Pascal Bourguignon
  2004-09-03 10:20 ` Phillip Lord
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Pascal Bourguignon @ 2004-09-03  2:11 UTC (permalink / raw)


Joe Corneli <jcorneli@math.utexas.edu> writes:

> This fails on big numbers like 12.
> 
>  (defun factorial (num)
>    (if (<= num 0)
>        1
>      (* num (factorial (1- num)))))
> 
> Suggestions on how to make it work for bigger numbers?

Run it on a Common-Lisp implementation instead of emacs lisp.

After all, emacs lisp is designed to count characters in a file, not
to compute factorials.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: bigger integers
       [not found] <mailman.1324.1094164438.1998.help-gnu-emacs@gnu.org>
  2004-09-02 23:10 ` bigger integers Kevin Rodgers
  2004-09-03  2:11 ` Pascal Bourguignon
@ 2004-09-03 10:20 ` Phillip Lord
  2004-09-04  8:18 ` Alan Mackenzie
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Phillip Lord @ 2004-09-03 10:20 UTC (permalink / raw)


>>>>> "Joe" == Joe Corneli <jcorneli@math.utexas.edu> writes:

  Joe> This fails on big numbers like 12.

  Joe>  (defun factorial (num)
  Joe>    (if (<= num 0)
  Joe>        1
  Joe>      (* num (factorial (1- num)))))

  Joe> Suggestions on how to make it work for bigger numbers?


I think calc can cope with arbitrary precision. 


http://www.synaptics.com/people/daveg/


Cheers

Phil

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: bigger integers
@ 2004-09-03 22:00 Joe Corneli
  0 siblings, 0 replies; 11+ messages in thread
From: Joe Corneli @ 2004-09-03 22:00 UTC (permalink / raw)


   After all, emacs lisp is designed to count characters in a file,
   not to compute factorials.

 "Emacs is the extensible, customizable, self-documenting real-time
 display editor."

If I can't extend or customize it to compute 12! then there seems to
be something missing.  Kevin's factorial works though so I guess
that's good enough for me... oh wait a second now it stopped working.

  Debugger entered--Lisp error: (error "Lisp nesting exceeds max-lisp-eval-depth")
  (factorial (1- num))
  (* num (factorial (1- num)))
  (if (<= num 0) 1.0 (* num (factorial ...)))
  factorial(7)
  (* num (factorial (1- num)))
  (if (<= num 0) 1.0 (* num (factorial ...)))
  factorial(8)
  (* num (factorial (1- num)))
  (if (<= num 0) 1.0 (* num (factorial ...)))
  factorial(9)
  (* num (factorial (1- num)))
  (if (<= num 0) 1.0 (* num (factorial ...)))
  factorial(10)
  (* num (factorial (1- num)))
  (if (<= num 0) 1.0 (* num (factorial ...)))
  factorial(11)
  (* num (factorial (1- num)))
  (if (<= num 0) 1.0 (* num (factorial ...)))
  factorial(12)
  eval((factorial 12))

(This same computation was fine just a minute ago, then I tried to
compute 100! and now it doesn't want to do 12! any more.)

   I think calc can cope with arbitrary precision.

I guess I'll check that out.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: bigger integers
       [not found] <mailman.1438.1094249173.1998.help-gnu-emacs@gnu.org>
@ 2004-09-03 23:29 ` Pascal Bourguignon
  2004-09-07 17:46 ` Kevin Rodgers
  1 sibling, 0 replies; 11+ messages in thread
From: Pascal Bourguignon @ 2004-09-03 23:29 UTC (permalink / raw)


Joe Corneli <jcorneli@math.utexas.edu> writes:

>    After all, emacs lisp is designed to count characters in a file,
>    not to compute factorials.
> 
>  "Emacs is the extensible, customizable, self-documenting real-time
>  display editor."
> 
> If I can't extend or customize it to compute 12! then there seems to
> be something missing.  Kevin's factorial works though so I guess
> that's good enough for me... oh wait a second now it stopped working.

Oh yes, it IS extensible and customizable.
The question is: is it worth the effort to extend and customize it?

And what does 12! have to do with editing files anyway?

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: bigger integers
       [not found] <mailman.1324.1094164438.1998.help-gnu-emacs@gnu.org>
                   ` (2 preceding siblings ...)
  2004-09-03 10:20 ` Phillip Lord
@ 2004-09-04  8:18 ` Alan Mackenzie
  2004-09-04 11:09   ` Neil Woods
  2004-09-06  0:00 ` Miles Bader
  2004-09-17 19:45 ` giacomo boffi
  5 siblings, 1 reply; 11+ messages in thread
From: Alan Mackenzie @ 2004-09-04  8:18 UTC (permalink / raw)


Joe Corneli <jcorneli@math.utexas.edu> wrote on Thu, 02 Sep 2004 17:28:17
-0500:

> This fails on big numbers like 12.

>  (defun factorial (num)
>    (if (<= num 0)
>        1
>      (* num (factorial (1- num)))))

> Suggestions on how to make it work for bigger numbers?

Why on earth do you want to write a program for a calculation than just
about anybody could do in his head?

12! is, of course, 478,002,600.  ;-) 

-- 
Alan Mackenzie (Munich, Germany)
Email: aacm@muuc.dee; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: bigger integers
  2004-09-04  8:18 ` Alan Mackenzie
@ 2004-09-04 11:09   ` Neil Woods
  0 siblings, 0 replies; 11+ messages in thread
From: Neil Woods @ 2004-09-04 11:09 UTC (permalink / raw)


>>>>> "Alan" == Alan Mackenzie <acm@muc.de> writes:

    >> Suggestions on how to make it work for bigger numbers?

    Alan> Why on earth do you want to write a program for a calculation
    Alan> than just about anybody could do in his head?

    Alan> 12! is, of course, 478,002,600.  ;-)

ITYM 479,001,600.

Besides, as has been already mentioned, the calc package does all this
and more.

-- 
,---------------------------------------------------------------.
|   Neil Woods   |   Do you ever get that feeling of deja vu?   |
`---------------------------------------------------------------'

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: bigger integers
       [not found] <mailman.1324.1094164438.1998.help-gnu-emacs@gnu.org>
                   ` (3 preceding siblings ...)
  2004-09-04  8:18 ` Alan Mackenzie
@ 2004-09-06  0:00 ` Miles Bader
  2004-09-17 19:45 ` giacomo boffi
  5 siblings, 0 replies; 11+ messages in thread
From: Miles Bader @ 2004-09-06  0:00 UTC (permalink / raw)


Joe Corneli <jcorneli@math.utexas.edu> writes:
> Suggestions on how to make it work for bigger numbers?

You might want to look into the calc/math package (it's part of emacs in
CVS), which implements its own bignum support.  It doesn't seem to have
all that much in the way of clean programmatic interfaces for external
use, but the functionality is there:

E.g.

   (defun factorial-work (num)
     (if (math-lessp num '(float 1 0))
         '(float 1 0)
       (math-mul num (factorial (math-sub num '(float 1 0))))))

Works if you use its bignum notation:

   (factorial-work '(float 12 0))
   => (float (bigpos 16 790 4) 2)

I'm not sure what you need to require to use these routines; maybe
(require 'calc) would be enough?

I looked for conversion routines, e.g., to/from strings, for the calc
bignum format; they surely exist, as calc must use them for its own I/O,
but I couldn't find them in my brief search (calc's code is
unfortunately a bit hard to follow in many places).

-Miles
-- 
[|nurgle|]  ddt- demonic? so quake will have an evil kinda setting? one that
            will  make every christian in the world foamm at the mouth?
[iddt]      nurg, that's the goal

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: bigger integers
       [not found] <mailman.1438.1094249173.1998.help-gnu-emacs@gnu.org>
  2004-09-03 23:29 ` Pascal Bourguignon
@ 2004-09-07 17:46 ` Kevin Rodgers
  1 sibling, 0 replies; 11+ messages in thread
From: Kevin Rodgers @ 2004-09-07 17:46 UTC (permalink / raw)


Joe Corneli wrote:
 >    After all, emacs lisp is designed to count characters in a file,
 >    not to compute factorials.
 >
 >  "Emacs is the extensible, customizable, self-documenting real-time
 >  display editor."
 >
 > If I can't extend or customize it to compute 12! then there seems to
 > be something missing.  Kevin's factorial works though so I guess
 > that's good enough for me... oh wait a second now it stopped working.
 >
 >   Debugger entered--Lisp error: (error "Lisp nesting exceeds max-lisp-eval-depth")

The obvious pseudo-fix is something like:

(setq max-lisp-eval-depth (* 42 max-lisp-eval-depth))

 >   (factorial (1- num))
 >   (* num (factorial (1- num)))
 >   (if (<= num 0) 1.0 (* num (factorial ...)))
 >   factorial(7)
 >   (* num (factorial (1- num)))
 >   (if (<= num 0) 1.0 (* num (factorial ...)))
 >   factorial(8)
 >   (* num (factorial (1- num)))
 >   (if (<= num 0) 1.0 (* num (factorial ...)))
 >   factorial(9)
 >   (* num (factorial (1- num)))
 >   (if (<= num 0) 1.0 (* num (factorial ...)))
 >   factorial(10)
 >   (* num (factorial (1- num)))
 >   (if (<= num 0) 1.0 (* num (factorial ...)))
 >   factorial(11)
 >   (* num (factorial (1- num)))
 >   (if (<= num 0) 1.0 (* num (factorial ...)))
 >   factorial(12)
 >   eval((factorial 12))
 >
 > (This same computation was fine just a minute ago, then I tried to
 > compute 100! and now it doesn't want to do 12! any more.)

That's weird, it sounds like the stack has been corrupted.

-- 
Kevin Rodgers

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: bigger integers
       [not found] <mailman.1324.1094164438.1998.help-gnu-emacs@gnu.org>
                   ` (4 preceding siblings ...)
  2004-09-06  0:00 ` Miles Bader
@ 2004-09-17 19:45 ` giacomo boffi
  5 siblings, 0 replies; 11+ messages in thread
From: giacomo boffi @ 2004-09-17 19:45 UTC (permalink / raw)


Joe Corneli <jcorneli@math.utexas.edu> writes:

> This fails on big numbers like 12.
>
>  (defun factorial (num)
>    (if (<= num 0)
>        1
>      (* num (factorial (1- num)))))
>
> Suggestions on how to make it work for bigger numbers?

from my *scratch*:

 (defun factorial (num) 
   (if (<= num 0) 
       1 
     (* num (factorial (1- num))))) 

factorial
(factorial 12)

479001600

ah, yes, (insert (emacs-version))

XEmacs 21.5 (beta17) "chayote" (+CVS-20040721) [Lucid] (i686-pc-linux)
of Thu Sep 16 2004 on boffi95

-- 
ATTACKED BY SALESMEN. TORPEDOS. LEAVE BOAT. U-941.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2004-09-17 19:45 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.1324.1094164438.1998.help-gnu-emacs@gnu.org>
2004-09-02 23:10 ` bigger integers Kevin Rodgers
2004-09-03  2:11 ` Pascal Bourguignon
2004-09-03 10:20 ` Phillip Lord
2004-09-04  8:18 ` Alan Mackenzie
2004-09-04 11:09   ` Neil Woods
2004-09-06  0:00 ` Miles Bader
2004-09-17 19:45 ` giacomo boffi
     [not found] <mailman.1438.1094249173.1998.help-gnu-emacs@gnu.org>
2004-09-03 23:29 ` Pascal Bourguignon
2004-09-07 17:46 ` Kevin Rodgers
2004-09-03 22:00 Joe Corneli
  -- strict thread matches above, loose matches on Subject: below --
2004-09-02 22:28 Joe Corneli

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.