Author Topic: -={ Challenge }=- Addition without Arithmetic Operators  (Read 11648 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #15 on: October 11, 2014, 06:48:34 PM »
Good additions Stefan  :-)

I really liked the elegance of MP's solution, so thought I'd offer a recursive variant:
Code: [Select]
(defun _+ ( a b )
    (if (< 0 a) (_+ (lsh (logand a b) 1) (boole 6 a b)) b)
)

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #16 on: October 11, 2014, 07:07:22 PM »
Another simple one, positive ints only:

Code: [Select]
(defun _+ ( a b )
    (cond
        (   (= a b 0) 0)
        (   (< 0 a) (1+ (_+ (1- a) b)))
        (   (_+ b a))
    )
)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #17 on: October 12, 2014, 02:08:42 AM »
A different route:
Code: [Select]
(defun _+ ( a b / r s )
    (if (setq s (vla-getinterfaceobject (vlax-get-acad-object) "scriptcontrol"))
        (progn
            (setq r
                (vl-catch-all-apply
                   '(lambda nil
                        (vlax-put-property s 'language "vbscript")
                        (vlax-invoke s 'eval (strcat (itoa a) "+" (itoa b)))
                    )
                )
            )
            (vlax-release-object s)
            (if (not (vl-catch-all-error-p r)) r)
        )
    )
)
Very interesting... but you use the operator "+"?

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #18 on: October 12, 2014, 02:20:45 AM »
Is this valid?
Code: [Select]
(defun _+ (a b)
  ((eval (read (chr 43))) a b)
)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #19 on: October 12, 2014, 02:41:43 AM »
Code - Auto/Visual Lisp: [Select]
  1. (netload "MathStuff")
  2.  
  3. (Add 21 (Mult 3 7))    ;->> 42
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #20 on: October 12, 2014, 06:26:02 AM »
A different route:
Code: [Select]
(defun _+ ( a b / r s )
    (if (setq s (vla-getinterfaceobject (vlax-get-acad-object) "scriptcontrol"))
        (progn
            (setq r
                (vl-catch-all-apply
                   '(lambda nil
                        (vlax-put-property s 'language "vbscript")
                        (vlax-invoke s 'eval (strcat (itoa a) "+" (itoa b)))
                    )
                )
            )
            (vlax-release-object s)
            (if (not (vl-catch-all-error-p r)) r)
        )
    )
)
Very interesting... but you use the operator "+"?
Not as the AutoLISP addition operator but as a string, so I thought it would pass  :-)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #21 on: October 13, 2014, 08:12:36 AM »
...
Very interesting... but you use the operator "+"?
Not as the AutoLISP addition operator but as a string, so I thought it would pass  :)
Lee, You are a wonderful programmer mathematician, and too generous. But in this case I must make you see that your statement was this: "Construct a function returning the sum of two integers without using the addition operator ("+")." (generic operator)
not "AutoLISP addition operator".
 ;D :) :) :)

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #22 on: October 13, 2014, 04:26:47 PM »
...
Very interesting... but you use the operator "+"?
Not as the AutoLISP addition operator but as a string, so I thought it would pass  :)
Lee, You are a wonderful programmer mathematician, and too generous. But in this case I must make you see that your statement was this: "Construct a function returning the sum of two integers without using the addition operator ("+")." (generic operator)
not "AutoLISP addition operator".
 ;D :) :) :)

Good point Marc! -
I happily concede and withdraw this function from the challenge :-)

Thank you for your kind compliments, you are too kind my friend  :-)

pBe

  • Bull Frog
  • Posts: 402
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #23 on: October 14, 2014, 04:51:01 AM »
Here's my take, <List association>

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun pbe_+ (fn sn / table _lst a b c d e)
  3. (setq table
  4.            '(((0 0) 0) ((0 1) 1) ((0 2) 2) ((0 3) 3)
  5.              ((0 4) 4) ((0 5) 5) ((0 6) 6) ((0 7) 7)
  6.              ((0 8 )8 )((0 9) 9) ((0 10) 10) ((1 1) 2) ((1 2) 3)
  7.              ((1 3) 4) ((1 4) 5) ((1 5) 6) ((1 6) 7)
  8.              ((1 7) 8 )((1 8 )9) ((1 9) 10) ((1 10) 11)
  9.              ((2 2) 4)  ((2 3) 5) ((2 4) 6) ((2 5) 7) ((2 6) 8 )
  10.              ((2 7) 9) ((2 8 )10) ((2 9) 11) ((2 10) 12)((3 3) 6)
  11.              ((3 4) 7) ((3 5) 8 )((3 6) 9) ((3 7) 10)
  12.              ((3 8 )11) ((3 9) 12) ((3 10) 13) ((4 4) 8 )((4 5) 9)
  13.              ((4 6) 10) ((4 7) 11) ((4 8 )12) ((4 9) 13)((4 10) 14)
  14.              ((5 5) 10) ((5 6) 11) ((5 7) 12) ((5 8 )13)
  15.              ((5 9) 14) ((5 10) 15) ((6 6) 12) ((6 7) 13) ((6 8 )14)
  16.              ((6 9) 15) ((6 10) 16) ((7 7) 14) ((7 8 )15) ((7 9) 16)
  17.              ((7 10) 17) ((8 8 )16) ((8 9) 17) ((8 10) 18 ) ((9 9) 18 )((9 10) 19)
  18.              )
  19.              )
  20.  
  21. (setq c nil _lst (lambda (x y)
  22.                  (cadr (assoc (mapcar 'fix
  23.                                       (vl-sort
  24.                                             (mapcar 'distof (list x y))
  25.                                             '<))
  26.                               table))
  27.                  ))
  28.       (setq tmp (max fn sn)
  29.             sn (min fn sn)
  30.             fn tmp)
  31.       (foreach
  32.              itm  (list "fn" "sn")
  33.             (set (read itm)
  34.                  (reverse
  35.                        (mapcar 'chr
  36.                                (vl-string->list
  37.                                      (itoa (eval (read itm))))))))
  38.       (repeat (length fn)
  39.             (setq a (car fn)
  40.                   b (car sn))
  41.             (setq b (if b b "0"))
  42.             (setq d (_lst a b))
  43.             (setq c    (cons (if (and  (Cdr fn)
  44.                                        (setq e (>= d 10)))
  45.                                  (substr (itoa d) 2)
  46.                                  (itoa d))
  47.  
  48.                              c))
  49.             (if (cdr fn)
  50.             (setq fn (cdr fn)
  51.                   fn (if e (cons (itoa (_lst "1" (car fn))) (cdr fn)) fn)
  52.                   sn (cdr sn))
  53.             )
  54.             )
  55.       (atoi (apply 'strcat c))
  56.  
  57. )

_$ (pBe_+  6534 9243)
15777
_$ (pBe_+  54353 101010)
155363
_$

Original, me thinks :)

EDIT: Smileys get in the way... grr <numbers are missing>
BUG at 9 and 1 at "10"

Crashed with 99999 99999 <fixed>

Now i'm starting to think i'm missing something here, oh well, time to throw in the towel now ;D
« Last Edit: October 14, 2014, 10:16:13 AM by pBe »

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #24 on: October 14, 2014, 02:10:30 PM »
Excellent idea pBe! - Very original  :coolsmiley:

Perhaps this will fix the bugs:

Code - Auto/Visual Lisp: [Select]
  1. (defun pbe_+ ( m n / f g )
  2.     (defun f ( a b c / x y z )
  3.         (cond
  4.             (   c
  5.                 (setq x (g (f (list c)        (list (car  b)) nil))
  6.                       y (g (f (list (car  a)) (list (car  x)) nil))
  7.                       z (g (f (list (cadr x)) (list (cadr y)) nil))
  8.                 )
  9.                 (strcat (f (cdr a) (cdr b) (car z)) (car y))
  10.             )
  11.             (   (and (car a) (car b))
  12.                 (setq x
  13.                     (cdr
  14.                         (assoc (mapcar 'atoi (vl-sort (list (car a) (car b)) '<))
  15.                            '(
  16.                                 ((0 0) "0")     ((0 1) "1")     ((0 2) "2")     ((0 3) "3")     ((0 4) "4")
  17.                                 ((0 5) "5")     ((0 6) "6")     ((0 7) "7")     ((0 8) "8")     ((0 9) "9")
  18.                                 ((1 1) "2")     ((1 2) "3")     ((1 3) "4")     ((1 4) "5")     ((1 5) "6")
  19.                                 ((1 6) "7")     ((1 7) "8")     ((1 8) "9")     ((1 9) "0" "1") ((2 2) "4")
  20.                                 ((2 3) "5")     ((2 4) "6")     ((2 5) "7")     ((2 6) "8")     ((2 7) "9")
  21.                                 ((2 8) "0" "1") ((2 9) "1" "1") ((3 3) "6")     ((3 4) "7")     ((3 5) "8")
  22.                                 ((3 6) "9")     ((3 7) "0" "1") ((3 8) "1" "1") ((3 9) "2" "1") ((4 4) "8")
  23.                                 ((4 5) "9")     ((4 6) "0" "1") ((4 7) "1" "1") ((4 8) "2" "1") ((4 9) "3" "1")
  24.                                 ((5 5) "0" "1") ((5 6) "1" "1") ((5 7) "2" "1") ((5 8) "3" "1") ((5 9) "4" "1")
  25.                                 ((6 6) "2" "1") ((6 7) "3" "1") ((6 8) "4" "1") ((6 9) "5" "1") ((7 7) "4" "1")
  26.                                 ((7 8) "5" "1") ((7 9) "6" "1") ((8 8) "6" "1") ((8 9) "7" "1") ((9 9) "8" "1")
  27.                             )
  28.                         )
  29.                     )
  30.                 )
  31.                 (strcat (f (cdr a) (cdr b) (cadr x)) (car x))
  32.             )
  33.             (   (car a) (strcat (f (cdr a) b nil) (car a)))
  34.             (   (car b) (strcat (f (cdr b) a nil) (car b)))
  35.             (   ""   )
  36.         )
  37.     )
  38.     (defun g ( a )
  39.         (reverse (mapcar 'chr (vl-string->list a)))
  40.     )
  41.     (atoi (f (g (itoa (min m n))) (g (itoa (max m n))) nil))
  42. )

pBe

  • Bull Frog
  • Posts: 402
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #25 on: October 14, 2014, 10:00:40 PM »
Excellent idea pBe! - Very original  :coolsmiley:

Glad you like the idea LM. That is just me, bringing something different to the table. But really, i'm just trying to find a way around to avoid dealing with mathematics  ;D

The polar idea from Vovka is great.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #26 on: October 15, 2014, 01:27:45 AM »
My humble first attempt at anything lispy for a while, similar to others posted and simple :)

Code - Auto/Visual Lisp: [Select]
  1. (defun _+ ( a b )
  2.     (if (= a 0) b (_+ (1- a) (1+ b))))
  3.  
  4.  
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

ribarm

  • Gator
  • Posts: 3259
  • Marko Ribar, architect
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #27 on: October 15, 2014, 04:14:44 AM »
MickD, I think your isn't working well with negative...

Here is my humble...

Code - Auto/Visual Lisp: [Select]
  1. (defun _+ ( a b )
  2.   (cond
  3.     ( (= a 0) b )
  4.     ( (= b 0) a )
  5.     ( (= a b) (* 2 a) )
  6.     ( (< a b) (- (* 2 b) (- b a)) )
  7.     ( (> a b) (- (* 2 a) (- a b)) )
  8.   )
  9. )
  10.  
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #28 on: October 15, 2014, 04:49:25 AM »
MickD, I think your isn't working well with negative...


true dat, back to the drawing board :)
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #29 on: October 15, 2014, 06:14:38 AM »
A little test:
Code: [Select]
    (defun _+Lee1 ( a b ) (- a (- b)))
    (defun _+MP1 ( a b ) (apply '- (mapcar '- (list a b))))
    (defun _+MP2 ( a b / temp )
        (while (< 0 b)
            (setq
                temp (logand a b)
                a    (boole 6 a b)
                b    (lsh temp 1)
            )
        )
        a 
    )
    (defun _+Lee2 ( a b )
        (defun foo ( a b c / x y )
            (cond
                (   (= 0 a b) c)
                (   (setq x (boole 1 1 a)
                          y (boole 1 1 b)
                    )
                    (logior (boole 6 c (boole 6 x y))
                        (lsh
                            (foo
                                (lsh a -1)
                                (lsh b -1)
                                (logior
                                    (boole 1 c x)
                                    (boole 1 c y)
                                    (boole 1 x y)
                                )
                            )
                            1
                        )
                    )
                )
            )
        )
        (foo a b 0)
    )
    (defun _+Ale1 (a b)
      (if (> a 0)
        (repeat a (setq b (1+ b)))
        (repeat (abs a) (setq b (1- b)))
      )
    )
    (defun _+VK1 ( a b ) (car (polar (list a 0) 0 b)))
    (defun _+Lee3 ( a b / r s )
        (if (setq s (vla-getinterfaceobject (vlax-get-acad-object) "scriptcontrol"))
            (progn
                (setq r
                    (vl-catch-all-apply
                       '(lambda nil
                            (vlax-put-property s 'language "vbscript")
                            (vlax-invoke s 'eval (strcat (itoa a) "+" (itoa b)))
                        )
                    )
                )
                (vlax-release-object s)
                (if (not (vl-catch-all-error-p r)) r)
            )
        )
    )
    (defun _+Lee4 ( a b ) (log (* (exp a) (exp b))))
    (defun _+Lee5 ( a b ) (- (1- a) (~ b)))
    (defun _+Stf1 (a b)
         (if (= a b)
             (* 2 a)
             (/ (- (* a a) (* b b)) (- a b))
         )
    )
    (defun _+Stf2 (a b)
         (if (zerop a) b (* a (1+ (/ b a))))
    )
    (defun _+Lee6 ( a b )
        (if (< 0 a) (_+Lee6 (lsh (logand a b) 1) (boole 6 a b)) b)
    )
    (defun _+Lee7 ( a b )
        (cond
            (   (= a b 0) 0)
            (   (< 0 a) (1+ (_+Lee7 (1- a) b)))
            (   (_+Lee7 b a))
        )
    )
    (defun _+Ale2 (a b)
      ((eval (read (chr 43))) a b)
    )
    (defun pbe_+ ( m n / f g )
       (defun f ( a b c / x y z )
           (cond
               (   c
                   (setq x (g (f (list c)        (list (car  b)) nil))
                         y (g (f (list (car  a)) (list (car  x)) nil))
                         z (g (f (list (cadr x)) (list (cadr y)) nil))
                   )
                   (strcat (f (cdr a) (cdr b) (car z)) (car y))
               )
               (   (and (car a) (car b))
                   (setq x
                       (cdr
                           (assoc (mapcar 'atoi (vl-sort (list (car a) (car b)) '<))
                              '(
                                   ((0 0) "0")     ((0 1) "1")     ((0 2) "2")     ((0 3) "3")     ((0 4) "4")
                                   ((0 5) "5")     ((0 6) "6")     ((0 7) "7")     ((0 "8")     ((0 9) "9")
                                   ((1 1) "2")     ((1 2) "3")     ((1 3) "4")     ((1 4) "5")     ((1 5) "6")
                                   ((1 6) "7")     ((1 7) "8")     ((1 "9")     ((1 9) "0" "1") ((2 2) "4")
                                   ((2 3) "5")     ((2 4) "6")     ((2 5) "7")     ((2 6) "8")     ((2 7) "9")
                                   ((2 "0" "1") ((2 9) "1" "1") ((3 3) "6")     ((3 4) "7")     ((3 5) "8")
                                   ((3 6) "9")     ((3 7) "0" "1") ((3 "1" "1") ((3 9) "2" "1") ((4 4) "8")
                                   ((4 5) "9")     ((4 6) "0" "1") ((4 7) "1" "1") ((4 "2" "1") ((4 9) "3" "1")
                                   ((5 5) "0" "1") ((5 6) "1" "1") ((5 7) "2" "1") ((5 "3" "1") ((5 9) "4" "1")
                                   ((6 6) "2" "1") ((6 7) "3" "1") ((6 "4" "1") ((6 9) "5" "1") ((7 7) "4" "1")
                                   ((7 "5" "1") ((7 9) "6" "1") ((8 "6" "1") ((8 9) "7" "1") ((9 9) "8" "1")
                               )
                           )
                       )
                   )
                   (strcat (f (cdr a) (cdr b) (cadr x)) (car x))
               )
               (   (car a) (strcat (f (cdr a) b nil) (car a)))
               (   (car b) (strcat (f (cdr b) a nil) (car b)))
               (   ""   )
           )
       )
       (defun g ( a )
           (reverse (mapcar 'chr (vl-string->list a)))
       )
       (atoi (f (g (itoa (min m n))) (g (itoa (max m n))) nil))
    )
(defun _+MkD1 ( a b )
   (if (= a 0) b (_+MkD1 (1- a) (1+ b))))
   
(defun _+Rib1 ( a b )
 (cond
   ( (= a 0) b )
   ( (= b 0) a )
   ( (= a b) (* 2 a) )
   ( (< a b) (- (* 2 b) (- b a)) )
   ( (> a b) (- (* 2 a) (- a b)) )
 )
)   
Code: [Select]
(setq n1 1000000000 n2 2000000000)
(+  n1 n2)      -1294967296
(_+Lee1  n1 n2) -1294967296
(_+MP2   n1 n2) 713973248
(_+Lee2  n1 n2) -1294967296
(_+Ale1  n1 n2) > too slow
(_+VK1   n1 n2) 3.0e+009
(_+Lee3  n1 n2) > errore: Errore di automazione.
(_+Lee4  n1 n2) 1.#INF
(_+Lee5  n1 n2) -1294967296
(_+Stf1  n1 n2) 0
(_+Stf2  n1 n2) -1294967296
(_+Lee6  n1 n2) 713973248
(_+Lee7  n1 n2) > Errore irrecuperabile ***
(_+Ale2  n1 n2) -1294967296
(pbe_+   n1 n2) 2147483647
(_+MkD1  n1 n2) > Errore irrecuperabile ***
(_+Rib1  n1 n2) -1294967296