Greatest common denominator

Discussion in 'AutoCAD' started by Greg McLandsborough, Nov 18, 2004.

  1. Does anyone know if it is possible and how to get the greatest common
    denominated of more than two integers

    Cheers

    Greg
     
    Greg McLandsborough, Nov 18, 2004
    #1
  2. Greg McLandsborough

    MP Guest

    MP, Nov 18, 2004
    #2
  3. I thougth that was a simple question, but its not :<(
     
    Greg McLandsborough, Nov 18, 2004
    #3
  4. Greg McLandsborough

    Jürg Menzi Guest

    Hi Greg

    Maybe somone has a more elegant solution, but this sould work:
    Code:
    (defun GetGCDfromList (Lst / TmpLst)
    (foreach a Lst
    (foreach b Lst
    (setq TmpLst (cons (GetGCD a b) TmpLst))
    )
    )
    (apply 'min TmpLst)
    )
    
    (defun GetGCD (Fst Nxt)
    (cond
    ((zerop Nxt) Nxt)
    ((zerop (rem Fst Nxt)) Nxt)
    ((GetGCD Nxt (rem Fst Nxt)))
    )
    )
    
    Use:
    _$ (GetGCDfromList '(4 32 16 256 8))
    4

    Cheers
     
    Jürg Menzi, Nov 18, 2004
    #4
  5. Greg McLandsborough

    David Bethel Guest

    (gcd n n n)

    -David
     
    David Bethel, Nov 18, 2004
    #5
  6. Greg McLandsborough

    Dann Guest

    Error: To many arguments.


     
    Dann, Nov 18, 2004
    #6
  7. Greg McLandsborough

    Jürg Menzi Guest

    Hi David
    If you are working with integers and values >0, you can use:
    Code:
    (defun GetGCDfromList (Lst / TmpLst)
    (foreach a Lst
    (foreach b Lst
    (setq TmpLst (cons (gcd a b) TmpLst))
    )
    )
    (apply 'min TmpLst)
    )
    [code]
    otherwhise, you've to work with my 'GetGCD'...
    
    Cheers
     
    Jürg Menzi, Nov 18, 2004
    #7
  8. Greg McLandsborough

    David Bethel Guest

    R12 thru R14 didn't do that. ??? -David
     
    David Bethel, Nov 18, 2004
    #8
  9. 2005 help file reads "between two integers"
     
    Jason Piercey, Nov 18, 2004
    #9
  10. Greg McLandsborough

    Jürg Menzi Guest

    Hi David

    Customization Guide R12-R14:
    'Returns the gcd of two integers'
    dig up my old books out the dust...;

    Cheers
     
    Jürg Menzi, Nov 18, 2004
    #10
  11. Greg McLandsborough

    David Bethel Guest

    Hmmm.. It doesn't error out. Must have been a bug. -David
     
    David Bethel, Nov 18, 2004
    #11
  12. Greg McLandsborough

    Paul Turvill Guest

    It was, apparently, an error in the docs before R2000. They fixed it by
    changing the command rather than the docs. I always thought it was kinda
    cool to be able to find the lcd of more than two integers at a time. Oh,
    well.
    ___
     
    Paul Turvill, Nov 18, 2004
    #12
  13. Jurg,

    Your a champion, I layed awake all last night trying to think of how to do
    this. Thank you Very Much, thats twice in two days you've helped me.

    Cheers

    Greg
     
    Greg McLandsborough, Nov 19, 2004
    #13
  14. Jurg,

    This works just the same...

    (defun GetGCDfromList (Lst / TmpLst)
    (foreach a Lst
    (foreach b Lst
    (setq TmpLst (cons (gcd a b) TmpLst))
    )
    )
    (apply 'min TmpLst)
    )


    Cheers

    Greg
     
    Greg McLandsborough, Nov 19, 2004
    #14
  15. Greg McLandsborough

    Jürg Menzi Guest

    Glad to help you...¦-)

    Cheers
     
    Jürg Menzi, Nov 19, 2004
    #15
  16. Greg McLandsborough

    Jürg Menzi Guest

    Hi Greg

    With integers and values >0 yes, otherwise you've to apply my 'GetGCD'.

    Cheers
     
    Jürg Menzi, Nov 19, 2004
    #16
  17. Ah, I knew you'd have a reason.
     
    Greg McLandsborough, Nov 25, 2004
    #17
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.