Author Topic: -={ Challenge }=- Addition without Arithmetic Operators  (Read 11675 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. )