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

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
-={ Challenge }=- Addition without Arithmetic Operators
« on: October 11, 2014, 12:04:48 PM »
The Challenge:

Construct a function returning the sum of two integers without using the addition operator ("+").



I'll get the trivial one out of the way:
Code - Auto/Visual Lisp: [Select]
  1. (defun _+ ( a b ) (- a (- b)))


Bonus points for originality  8-)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #1 on: October 11, 2014, 12:17:00 PM »
Doing this blind from my phone but this works in my hungover brain:

(apply '- (mapcar '- (list a b)))
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #2 on: October 11, 2014, 12:18:56 PM »
this works in my hungover brain:

(apply '- (mapcar '- (list a b)))

Indeed it does - nice variation MP :-)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #3 on: October 11, 2014, 12:56:36 PM »
Really, lol!!

This may work for positive ints:

Code: [Select]
(defun _+ ( a b / temp )
    (while (< 0 b)
        (setq
            temp (logand a b)
            a    (boole 6 a b)
            b    (lsh temp 1)
        )
    )
    a 
)

Fun challenge.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #4 on: October 11, 2014, 01:15:18 PM »
This may work for positive ints:
Code: [Select]
(defun _+ ( a b / temp )
    (while (< 0 b)
        (setq
            temp (logand a b)
            a    (boole 6 a b)
            b    (lsh temp 1)
        )
    )
    a 
)

Very good MP!  :-)

I'm glad you are enjoying the challenge!

Here's another bitwise approach, though processing the addition bit-by-bit (and should therefore work with negative ints also):
Code: [Select]
(defun _+ ( 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)
)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #5 on: October 11, 2014, 01:17:22 PM »
Just an idea if a integer > 0:
Code: [Select]
(defun _+ (a b)
  (repeat a (setq b (1+ b)))
)
Maybe it is a start for all conditions...

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #6 on: October 11, 2014, 01:21:02 PM »
Here is:
Code: [Select]
(defun _+ (a b)
  (if (> a 0)
    (repeat a (setq b (1+ b)))
    (repeat (abs a) (setq b (1- b)))
  )
)

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #7 on: October 11, 2014, 01:42:36 PM »
Code: [Select]
(defun _+ ( a b ) (distance (list 0 0) (polar (list a 0) 0 b)))for positive

oops i was dumb :)
Code: [Select]
(defun _+ ( a b ) (car (polar (list a 0) 0 b)))
« Last Edit: October 11, 2014, 02:02:16 PM by VovKa »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #8 on: October 11, 2014, 02:04:34 PM »
Here is:
Code: [Select]
(defun _+ (a b)
  (if (> a 0)
    (repeat a (setq b (1+ b)))
    (repeat (abs a) (setq b (1- b)))
  )
)

Nice one Marc  :-)

oops i was dumb :)
Code: [Select]
(defun _+ ( a b ) (car (polar (list a 0) 0 b)))

Very original VovKa - excellent  8-)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #9 on: October 11, 2014, 02:25:48 PM »
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)
        )
    )
)

EDIT: OK, So it doesn't quite meet the challenge criteria, but I'll keep it here for those spectating... :)
« Last Edit: October 13, 2014, 04:28:47 PM by Lee Mac »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #10 on: October 11, 2014, 02:32:56 PM »
Another simple math trick:

Code: [Select]
(defun _+ ( a b ) (log (* (exp a) (exp b))))

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #11 on: October 11, 2014, 03:01:27 PM »
Another variation on the trivial solution:

Code: [Select]
(defun _+ ( a b ) (- (1- a) (~ b)))

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #12 on: October 11, 2014, 03:50:32 PM »
Cool beans, nice job guys. :thumbsup:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #13 on: October 11, 2014, 05:04:24 PM »
Code: [Select]
(defun _+ ( a b ) (car (polar (list a 0) 0 b)))

very nice vovka!

Another simple math trick:
Code: [Select]
(defun _+ ( a b ) (log (* (exp a) (exp b))))

I saw that, but I wasn't fast enough... Here is another math trick
Code - Auto/Visual Lisp: [Select]
  1. (defun _+ (a b)
  2.   (if (= a b)
  3.       (* 2 a)
  4.       (/ (- (* a a) (* b b)) (- a b))
  5.   )
  6. )

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #14 on: October 11, 2014, 05:22:36 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun _+ (a b)
  2.   (if (zerop a) b (* a (1+ (/ b a)))
  3. )

Lee Mac

  • Seagull
  • Posts: 12914
  • 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: 12914
  • 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: 1453
  • 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: 1453
  • 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: 12914
  • 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: 1453
  • 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: 12914
  • 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: 12914
  • 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: 3274
  • 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: 1453
  • 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

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #30 on: October 15, 2014, 07:17:51 AM »
_+VK1 is correct, but :

Code: [Select]
Command: (fix 3e+9)
3.0e+009

Command: (type 3e+9)
REAL

Command: (type (fix 3e+9))
REAL
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #31 on: October 15, 2014, 07:34:02 AM »
A little test:
Code: [Select]
(setq n1 1000000000 n2 2000000000)

3,000,000,000 > 2,147,483,647

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #32 on: October 15, 2014, 08:03:24 AM »
A little test:
Code: [Select]
(setq n1 1000000000 n2 2000000000)

3,000,000,000 > 2,147,483,647
I just wanted to point out that some functions do not have the same behavior of the operator "+" that they are replacing.

About performance (test not exhaustive), maybe _+STF2?...
Code: [Select]
(defun c:+test ( / n1 n2)
(setq n1 1 n2 2)
(repeat 9
(princ "\nn1= ")(princ n1)(princ "  n2= ")(princ n2)(princ "\n")
(Benchmark '(
(_+Ale2  n1 n2)
(_+Lee1  n1 n2)
(_+Lee5  n1 n2)
(_+Stf2  n1 n2)
(_+Rib1  n1 n2)
)           )
(setq n1 (* 10 n1) n2 (* 10 n2))
))
(c:+test)
(princ)

Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved
n1= 1  n2= 2
Elapsed milliseconds / relative speed for 65536 iteration(s):
    (_+STF2 N1 N2).....1763 / 1.53 <fastest>
    (_+LEE5 N1 N2).....1794 / 1.5
    (_+RIB1 N1 N2).....1810 / 1.49
    (_+ALE2 N1 N2).....2106 / 1.28
    (_+LEE2 N1 N2).....2590 / 1.04
    (_+LEE1 N1 N2).....2699 / 1 <slowest>

n1= 10  n2= 20
Elapsed milliseconds / relative speed for 65536 iteration(s):
    (_+LEE1 N1 N2).....1747 / 2.02 <fastest>
    (_+STF2 N1 N2).....1763 / 2
    (_+LEE5 N1 N2).....1810 / 1.95
    (_+RIB1 N1 N2).....2278 / 1.55
    (_+ALE2 N1 N2).....2854 / 1.24
    (_+LEE2 N1 N2).....3526 / 1 <slowest>

n1= 100  n2= 200
Elapsed milliseconds / relative speed for 65536 iteration(s):
    (_+LEE1 N1 N2).....1731 / 4.41 <fastest>
    (_+STF2 N1 N2).....1778 / 4.29
    (_+LEE5 N1 N2).....1810 / 4.21
    (_+RIB1 N1 N2).....1810 / 4.21
    (_+ALE2 N1 N2).....2824 / 2.7
    (_+LEE2 N1 N2).....7629 / 1 <slowest>

n1= 1000  n2= 2000
Elapsed milliseconds / relative speed for 65536 iteration(s):
    (_+LEE1 N1 N2).....1747 / 4.17 <fastest>
    (_+RIB1 N1 N2).....1809 / 4.03
    (_+STF2 N1 N2).....1888 / 3.86
    (_+ALE2 N1 N2).....2121 / 3.43
    (_+LEE5 N1 N2).....3213 / 2.27
    (_+LEE2 N1 N2).....7285 / 1 <slowest>

n1= 10000  n2= 20000
Elapsed milliseconds / relative speed for 65536 iteration(s):
    (_+STF2 N1 N2).....1763 / 4.88 <fastest>
    (_+RIB1 N1 N2).....1825 / 4.71
    (_+LEE5 N1 N2).....2169 / 3.96
    (_+LEE1 N1 N2).....2262 / 3.8
    (_+ALE2 N1 N2).....2855 / 3.01
    (_+LEE2 N1 N2).....8595 / 1 <slowest>

n1= 100000  n2= 200000
Elapsed milliseconds / relative speed for 65536 iteration(s):
    (_+STF2 N1 N2)......1747 / 7.25 <fastest>
    (_+LEE1 N1 N2)......1779 / 7.12
    (_+RIB1 N1 N2)......1794 / 7.06
    (_+LEE5 N1 N2)......2184 / 5.8
    (_+ALE2 N1 N2)......3011 / 4.21
    (_+LEE2 N1 N2).....12667 / 1 <slowest>

n1= 1000000  n2= 2000000
Elapsed milliseconds / relative speed for 65536 iteration(s):
    (_+STF2 N1 N2)......1747 / 7.05 <fastest>
    (_+LEE5 N1 N2)......1809 / 6.8
    (_+RIB1 N1 N2)......1810 / 6.8
    (_+LEE1 N1 N2)......1888 / 6.52
    (_+ALE2 N1 N2)......2121 / 5.8
    (_+LEE2 N1 N2).....12308 / 1 <slowest>

n1= 10000000  n2= 20000000
Elapsed milliseconds / relative speed for 65536 iteration(s):
    (_+LEE1 N1 N2)......1731 / 6.36 <fastest>
    (_+STF2 N1 N2)......1748 / 6.3
    (_+LEE5 N1 N2)......1809 / 6.09
    (_+RIB1 N1 N2)......2698 / 4.08
    (_+ALE2 N1 N2)......2792 / 3.94
    (_+LEE2 N1 N2).....11014 / 1 <slowest>

n1= 100000000  n2= 200000000
Elapsed milliseconds / relative speed for 65536 iteration(s):
    (_+LEE1 N1 N2)......1732 / 7.43 <fastest>
    (_+STF2 N1 N2)......1747 / 7.37
    (_+LEE5 N1 N2)......1809 / 7.11
    (_+RIB1 N1 N2)......1826 / 7.05
    (_+ALE2 N1 N2)......2168 / 5.94
    (_+LEE2 N1 N2).....12870 / 1 <slowest>


Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved
n1= 1  n2= 2
Elapsed milliseconds / relative speed for 65536 iteration(s):
    (_+LEE1 N1 N2).....2075 / 1.27 <fastest>
    (_+LEE5 N1 N2).....2246 / 1.17
    (_+STF2 N1 N2).....2246 / 1.17
    (_+RIB1 N1 N2).....2246 / 1.17
    (_+ALE2 N1 N2).....2636 / 1 <slowest>

n1= 10  n2= 20
Elapsed milliseconds / relative speed for 32768 iteration(s):
    (_+STF2 N1 N2).....1107 / 1.25 <fastest>
    (_+RIB1 N1 N2).....1155 / 1.2
    (_+LEE5 N1 N2).....1170 / 1.19
    (_+LEE1 N1 N2).....1249 / 1.11
    (_+ALE2 N1 N2).....1389 / 1 <slowest>

n1= 100  n2= 200
Elapsed milliseconds / relative speed for 32768 iteration(s):
    (_+STF2 N1 N2).....1123 / 1.32 <fastest>
    (_+RIB1 N1 N2).....1154 / 1.28
    (_+LEE5 N1 N2).....1170 / 1.27
    (_+LEE1 N1 N2).....1326 / 1.12
    (_+ALE2 N1 N2).....1482 / 1 <slowest>

n1= 1000  n2= 2000
Elapsed milliseconds / relative speed for 32768 iteration(s):
    (_+LEE1 N1 N2).....1076 / 1.33 <fastest>
    (_+LEE5 N1 N2).....1123 / 1.28
    (_+STF2 N1 N2).....1139 / 1.26
    (_+RIB1 N1 N2).....1139 / 1.26
    (_+ALE2 N1 N2).....1435 / 1 <slowest>

n1= 10000  n2= 20000
Elapsed milliseconds / relative speed for 32768 iteration(s):
    (_+LEE1 N1 N2).....1061 / 1.32 <fastest>
    (_+LEE5 N1 N2).....1123 / 1.25
    (_+RIB1 N1 N2).....1124 / 1.25
    (_+STF2 N1 N2).....1170 / 1.2
    (_+ALE2 N1 N2).....1404 / 1 <slowest>

n1= 100000  n2= 200000
Elapsed milliseconds / relative speed for 32768 iteration(s):
    (_+LEE1 N1 N2).....1092 / 1.32 <fastest>
    (_+RIB1 N1 N2).....1279 / 1.12
    (_+LEE5 N1 N2).....1357 / 1.06
    (_+STF2 N1 N2).....1419 / 1.01
    (_+ALE2 N1 N2).....1436 / 1 <slowest>

n1= 1000000  n2= 2000000
Elapsed milliseconds / relative speed for 32768 iteration(s):
    (_+STF2 N1 N2).....1108 / 1.24 <fastest>
    (_+LEE1 N1 N2).....1139 / 1.21
    (_+LEE5 N1 N2).....1139 / 1.21
    (_+RIB1 N1 N2).....1170 / 1.17
    (_+ALE2 N1 N2).....1373 / 1 <slowest>

n1= 10000000  n2= 20000000
Elapsed milliseconds / relative speed for 32768 iteration(s):
    (_+STF2 N1 N2).....1092 / 1.26 <fastest>
    (_+LEE5 N1 N2).....1155 / 1.19
    (_+LEE1 N1 N2).....1170 / 1.17
    (_+RIB1 N1 N2).....1201 / 1.14
    (_+ALE2 N1 N2).....1372 / 1 <slowest>

n1= 100000000  n2= 200000000
Elapsed milliseconds / relative speed for 32768 iteration(s):
    (_+STF2 N1 N2).....1092 / 1.11 <fastest>
    (_+LEE5 N1 N2).....1139 / 1.07
    (_+LEE1 N1 N2).....1154 / 1.05
    (_+RIB1 N1 N2).....1170 / 1.04
    (_+ALE2 N1 N2).....1217 / 1 <slowest>

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #33 on: October 15, 2014, 08:18:52 AM »
Marc,

My second function STF2 fails if b is not a multiple of a, because of (/ b a).
Code: [Select]
(stf2 2 5) -> 6
This one is correct, but not so concise
Code - Auto/Visual Lisp: [Select]
  1. (defun _+Stf3 (a b)
  2.   (cond
  3.     ((zerop a) b)
  4.     ((and (= (type a) 'int)
  5.           (= (type b) 'int)
  6.           )
  7.      (fix (* a (1+ (/ b 1. a))))
  8.      )
  9.      ((* a (1+ (/ b a))))
  10.     )
  11.   )

Or this one, for integers only
Code - Auto/Visual Lisp: [Select]
  1. (defun _+Stf4 (a b)
  2.   (if (zerop a) b (fix (* a (1+ (/ b 1. a)))))
  3.   )
« Last Edit: October 16, 2014, 01:47:47 AM by Stefan »

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #34 on: October 15, 2014, 08:22:25 AM »
_+VK1 is correct, but :
Code: [Select]
Command: (fix 3e+9) 3.0e+009
Command: (type 3e+9) REAL
Command: (type (fix 3e+9)) REAL
you are right, about test  _+VK1 is in medium position.

Code: [Select]
n1= 10000000  n2= 20000000
Elapsed milliseconds / relative speed for 65536 iteration(s):

    (_+LEE1 N1 N2).....1997 / 1.05 <fastest>
    (_+STF2 N1 N2).....2029 / 1.03
    (_+VK1 N1 N2)......2059 / 1.02
    (_+LEE5 N1 N2).....2074 / 1.01
    (_+RIB1 N1 N2).....2091 / 1 <slowest>

n1= 100000000  n2= 200000000
Elapsed milliseconds / relative speed for 65536 iteration(s):

    (_+STF2 N1 N2).....2028 / 1.06 <fastest>
    (_+VK1 N1 N2)......2091 / 1.03
    (_+RIB1 N1 N2).....2091 / 1.03
    (_+LEE5 N1 N2).....2106 / 1.02
    (_+LEE1 N1 N2).....2153 / 1 <slowest>

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #35 on: October 15, 2014, 08:52:53 AM »
Also one note... My function isn't adequate for large integers... I've complicated unnecessary with (cond)... It should be just :

Code - Auto/Visual Lisp: [Select]
  1. (defun _+ ( a b )
  2.   (if (< (abs a) (abs b))
  3.     (- (* 2 a) (- a b))
  4.     (- (* 2 b) (- b a))
  5.   )
  6. )
  7.  

Regards...
« Last Edit: October 15, 2014, 09:14:59 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #36 on: October 15, 2014, 09:50:35 AM »
new one:
Code: [Select]
(defun _+Ale3 (a b)
      (or _+ (setq _+ (eval (read (chr 43)))))
      (_+ a b)
)
new test:
Code: [Select]
Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved


n1= 1  n2= 2
Elapsed milliseconds / relative speed for 65536 iteration(s):

    (_+ALE3 N1 N2).....1810 / 2.05 <fastest>
    (_+LEE5 N1 N2).....1918 / 1.94
    (_+STF3 N1 N2).....2044 / 1.82
    (_+STF4 N1 N2).....2496 / 1.49
    (_+LEE1 N1 N2).....2886 / 1.29
    (_+RIB2 N1 N2).....3027 / 1.23
    (_+VK1 N1 N2)......3713 / 1 <slowest>

n1= 20  n2= 40
Elapsed milliseconds / relative speed for 65536 iteration(s):

    (_+STF4 N1 N2).....1685 / 2.34 <fastest>
    (_+LEE1 N1 N2).....1919 / 2.06
    (_+VK1 N1 N2)......1965 / 2.01
    (_+RIB2 N1 N2).....2153 / 1.83
    (_+STF3 N1 N2).....2589 / 1.52
    (_+ALE3 N1 N2).....3604 / 1.09
    (_+LEE5 N1 N2).....3946 / 1 <slowest>

n1= 400  n2= 800
Elapsed milliseconds / relative speed for 65536 iteration(s):

    (_+ALE3 N1 N2).....1576 / 1.89 <fastest>
    (_+LEE1 N1 N2).....1591 / 1.87
    (_+LEE5 N1 N2).....1669 / 1.79
    (_+VK1 N1 N2)......1685 / 1.77
    (_+STF3 N1 N2).....2231 / 1.34
    (_+RIB2 N1 N2).....2247 / 1.33
    (_+STF4 N1 N2).....2980 / 1 <slowest>

n1= 8000  n2= 16000
Elapsed milliseconds / relative speed for 65536 iteration(s):

    (_+RIB2 N1 N2).....1654 / 1.86 <fastest>
    (_+STF4 N1 N2).....1700 / 1.81
    (_+STF3 N1 N2).....1997 / 1.54
    (_+LEE5 N1 N2).....2090 / 1.47
    (_+ALE3 N1 N2).....2699 / 1.14
    (_+VK1 N1 N2)......2870 / 1.07
    (_+LEE1 N1 N2).....3073 / 1 <slowest>

n1= 160000  n2= 320000
Elapsed milliseconds / relative speed for 65536 iteration(s):

    (_+ALE3 N1 N2).....1591 / 1.78 <fastest>
    (_+STF4 N1 N2).....1700 / 1.67
    (_+STF3 N1 N2).....1857 / 1.53
    (_+LEE5 N1 N2).....1966 / 1.44
    (_+VK1 N1 N2)......2372 / 1.2
    (_+RIB2 N1 N2).....2590 / 1.1
    (_+LEE1 N1 N2).....2839 / 1 <slowest>

n1= 3200000  n2= 6400000
Elapsed milliseconds / relative speed for 65536 iteration(s):

    (_+ALE3 N1 N2).....1592 / 1.62 <fastest>
    (_+LEE1 N1 N2).....1623 / 1.59
    (_+LEE5 N1 N2).....1684 / 1.53
    (_+STF4 N1 N2).....1700 / 1.51
    (_+STF3 N1 N2).....1919 / 1.34
    (_+RIB2 N1 N2).....2246 / 1.15
    (_+VK1 N1 N2)......2574 / 1 <slowest>

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #37 on: October 15, 2014, 12:26:44 PM »
A little test:
Code: [Select]
(setq n1 1000000000 n2 2000000000)

3,000,000,000 > 2,147,483,647
I just wanted to point out that some functions do not have the same behavior of the operator "+" that they are replacing.

However, note that the original challenge involved the computation of two integers, and is hence restricted by the upper-limit of the 32-bit signed integer used by AutoLISP. This is more a challenge for academia (and for fun!) as opposed to proposing a replacement of the "+" operator (which can accept any number of numerical arguments, integer or otherwise).

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #38 on: October 15, 2014, 12:48:34 PM »
A little test:
Code: [Select]
(setq n1 1000000000 n2 2000000000)

3,000,000,000 > 2,147,483,647
I just wanted to point out that some functions do not have the same behavior of the operator "+" that they are replacing.

However, note that the original challenge involved the computation of two integers, and is hence restricted by the upper-limit of the 32-bit signed integer used by AutoLISP. This is more a challenge for academia (and for fun!) as opposed to proposing a replacement of the "+" operator (which can accept any number of numerical arguments, integer or otherwise).
n1 and n2 are two integers only the sum is over the limit.
Maybe also the "+" AutoLisp function it would need a error control like:  (<= 2147483647 (+ n1 n2)) to return nil on error.
Code: [Select]
(setq n1 1  n2 2147483646) > 2147483646
(<= 2147483647 (+ n1 n2))  > T

(setq n1 1  n2 2147483647) > 2147483647
(<= 2147483647 (+ n1 n2))  > nil

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #39 on: October 15, 2014, 01:22:31 PM »
Example:
Code: [Select]
(defun _+Ale4 (a b / r)
  (or _+ (setq _+ (eval (read (chr 43)))))
  (if (<= 2147483647 (setq r (_+ a b))) r)
)
Code: [Select]
(setq n1 1  n2 2147483647) => 2147483647
(_+Ale4  n1 n2)            => nil

(setq n1 1  n2 2147483646) => 2147483646
(_+Ale4  n1 n2)            => 2147483647

pBe

  • Bull Frog
  • Posts: 402
Re: -={ Challenge }=- Addition without Arithmetic Operators
« Reply #40 on: October 15, 2014, 06:49:49 PM »
Quote
(_+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

 :D  Maximum <Max out>

. I guess the code <LM's mod> can be modified to accept real numbers, but then again, since im converting string to numbers and back.. well you guys know what i mean.
« Last Edit: October 15, 2014, 06:54:23 PM by pBe »