Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

A Technical Community for IT Professionals

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Highly Rated Users

Forum
No Posts Rated

Top 50
Given
Received

Forum Statistics

Users
Members:
1879
Members Online:
1
Guests Online:
34

Total Post History
Posts:
81448
Topics:
18714

7-Day Post History
New Posts:
0
New Topics:
0
Active Topics:
0

Our newest member
mwojcik

Other

FAQ
All times are UTC [ DST ]

Google Ads

Add without +

Mind Boggling Puzzles, to keep that grey matter in shape...
Forum rules
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
Please wait...

Add without +

Postby Emtucifor on Mon Sep 13, 2010 9:35 pm

Given the following:

  1. DECLARE
  2.    @a INT,
  3.    @b INT,
  4.    @c INT,
  5.    @d INT;
  6. SET @a = 10;
  7. SET @b = 15;
  8. SET @c = 6;
  9. SET @d = 12;
  10. SELECT @a + @b + @c + @d;
  11. SELECT ________________;

where a, b, c, and d are values between 0 and 15, fill in the blank in the second select statement to return the same answer, but without using + or -. You may use powers, exponents, logs, bit arithmetic, constants, modulus, multiplication, division, and pretty much anything. But you can't add or subtract. No custom functions, everything has to be inline.

You may use any programming language you like. I just happened to be working in SQL when I thought of the puzzle.
God cries a little bit every time someone builds a database.
User avatar
Emtucifor
Guru
Guru
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033
 
Posts: 2835
Joined: Fri May 30, 2008 9:30 pm
Location: Bellingham, WA
Unrated

Re: Add without +

Postby gmmastros on Mon Sep 13, 2010 9:42 pm

Are aggregates permissible?
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1630LTD Gold - Rating: 1630LTD Gold - Rating: 1630LTD Gold - Rating: 1630LTD Gold - Rating: 1630
LTD Gold - Rating: 1630LTD Gold - Rating: 1630LTD Gold - Rating: 1630LTD Gold - Rating: 1630LTD Gold - Rating: 1630
LTD Gold - Rating: 1630LTD Gold - Rating: 1630
 
Posts: 2367
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Re: Add without +

Postby SQLDenis on Mon Sep 13, 2010 9:44 pm

Based on George's hint


Code is hidden, SHOW
User avatar
SQLDenis
LTD Admin
LTD Admin
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
 
Posts: 21784
Joined: Wed Oct 10, 2007 6:43 pm
Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Unrated

Re: Add without +

Postby SQLDenis on Mon Sep 13, 2010 9:50 pm

another way :-)


Code is hidden, SHOW
User avatar
SQLDenis
LTD Admin
LTD Admin
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
 
Posts: 21784
Joined: Wed Oct 10, 2007 6:43 pm
Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Unrated

Re: Add without +

Postby Naomi on Mon Sep 13, 2010 9:53 pm

SQLDenis wrote:Based on George's hint


Code is hidden, SHOW


My thought exactly when I read this puzzle - but it's too simple?
Premature optimization is the root of all evil in programming.
Donald Knuth, repeating C. A. R. Hoare
Naomi
Senior Sage
Senior Sage
LTD Bronze - Rating: 234LTD Bronze - Rating: 234LTD Bronze - Rating: 234LTD Bronze - Rating: 234LTD Bronze - Rating: 234
 
Posts: 1754
Joined: Thu Mar 26, 2009 5:46 am
Location: WI
Unrated

Re: Add without +

Postby Emtucifor on Mon Sep 13, 2010 10:15 pm

Okay, I should have thought more carefully about sql's expressive capabilities. No aggregates. And Sum() breaks the rule of no addition or subtraction. Denis was more clever with Avg() * Count(), though... I like that. :)

But humor me and do it with just straight math, no aggregates, please!
God cries a little bit every time someone builds a database.
User avatar
Emtucifor
Guru
Guru
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033
 
Posts: 2835
Joined: Fri May 30, 2008 9:30 pm
Location: Bellingham, WA
Unrated

Re: Add without +

Postby Naomi on Mon Sep 13, 2010 10:31 pm

Code is hidden, SHOW
Premature optimization is the root of all evil in programming.
Donald Knuth, repeating C. A. R. Hoare
Naomi
Senior Sage
Senior Sage
LTD Bronze - Rating: 234LTD Bronze - Rating: 234LTD Bronze - Rating: 234LTD Bronze - Rating: 234LTD Bronze - Rating: 234
 
Posts: 1754
Joined: Thu Mar 26, 2009 5:46 am
Location: WI

Re: Add without +

Postby MHGlassman on Mon Sep 13, 2010 11:50 pm

Aw, shucks! :-s Naomi answered while I was working on the other puzzle of the day!
Code is hidden, SHOW

Mike
"The trouble with quotes on the Internet is that it's 
difficult to determine whether or not they are genuine."
-- Abraham Lincoln
User avatar
MHGlassman
Senior Apprentice
Senior Apprentice
LTD Bronze - Rating: 115LTD Bronze - Rating: 115LTD Bronze - Rating: 115
 
Posts: 72
Joined: Tue Jun 17, 2008 7:58 pm
Location: Englewood, CO, USA

Re: Add without +

Postby Naomi on Mon Sep 13, 2010 11:52 pm

MHGlassman wrote:Aw, shucks! :-s Naomi answered while I was working on the other puzzle of the day!
Code is hidden, SHOW

Mike


Have you been typing in about 1h.20 minutes? You must be a very slow typist then :)

Update. I think I misread your answer. I thought it said "while I was typing.."
Last edited by Naomi on Mon Sep 13, 2010 11:55 pm, edited 1 time in total.
Premature optimization is the root of all evil in programming.
Donald Knuth, repeating C. A. R. Hoare
Naomi
Senior Sage
Senior Sage
LTD Bronze - Rating: 234LTD Bronze - Rating: 234LTD Bronze - Rating: 234LTD Bronze - Rating: 234LTD Bronze - Rating: 234
 
Posts: 1754
Joined: Thu Mar 26, 2009 5:46 am
Location: WI
Unrated

Re: Add without +

Postby MHGlassman on Mon Sep 13, 2010 11:54 pm

Naomi wrote:Have you been typing in about 1h.20 minutes? You must be a very slow typist then :)

No, I typed my answer here and previewed it, but failed to submit it until after I came back from Denis' puzzles.
"The trouble with quotes on the Internet is that it's 
difficult to determine whether or not they are genuine."
-- Abraham Lincoln
User avatar
MHGlassman
Senior Apprentice
Senior Apprentice
LTD Bronze - Rating: 115LTD Bronze - Rating: 115LTD Bronze - Rating: 115
 
Posts: 72
Joined: Tue Jun 17, 2008 7:58 pm
Location: Englewood, CO, USA
Unrated

Re: Add without +

Postby Emtucifor on Tue Sep 14, 2010 12:14 am

Good work folks. I know how to do a product using exponents but I wouldn't have thought to add that way, the reverse operation.

There's another way exploiting the specific range of the numbers and some advanced hacks that would be familiar to expert bit-twiddlers. Want a hint?
God cries a little bit every time someone builds a database.
User avatar
Emtucifor
Guru
Guru
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033
 
Posts: 2835
Joined: Fri May 30, 2008 9:30 pm
Location: Bellingham, WA
Unrated

Re: Add without +

Postby Naomi on Tue Sep 14, 2010 1:02 am

I'm not too good with bit arithmetics. I thought about it first when I was trying to solve it...
Premature optimization is the root of all evil in programming.
Donald Knuth, repeating C. A. R. Hoare
Naomi
Senior Sage
Senior Sage
LTD Bronze - Rating: 234LTD Bronze - Rating: 234LTD Bronze - Rating: 234LTD Bronze - Rating: 234LTD Bronze - Rating: 234
 
Posts: 1754
Joined: Thu Mar 26, 2009 5:46 am
Location: WI
Unrated

Re: Add without +

Postby Emtucifor on Tue Sep 14, 2010 3:32 am

I think what I had in mind is too esoteric to reasonably expect anyone to get it.

In other news, I found this article to be very interesting reading. I would even say it was pretty nifty.
God cries a little bit every time someone builds a database.
User avatar
Emtucifor
Guru
Guru
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033
 
Posts: 2835
Joined: Fri May 30, 2008 9:30 pm
Location: Bellingham, WA
Unrated

Re: Add without +

Postby Emtucifor on Tue Sep 14, 2010 10:48 pm

Here's my answer:

Code is hidden, SHOW

I learned this trick from the Nifty Parallel Bit Count method.
God cries a little bit every time someone builds a database.
User avatar
Emtucifor
Guru
Guru
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033
 
Posts: 2835
Joined: Fri May 30, 2008 9:30 pm
Location: Bellingham, WA

Re: Add without +

Postby rizzo on Tue Dec 14, 2010 3:30 pm

Code is hidden, SHOW
User avatar
rizzo
Apprentice
Apprentice
LTD Bronze - Rating: 51LTD Bronze - Rating: 51
 
Posts: 23
Joined: Wed Jan 21, 2009 11:10 pm
Location: Devon. UK

Re: Add without +

Postby Thirster42 on Tue Dec 14, 2010 4:19 pm

why yall gotta complicate things?

Code is hidden, SHOW


edit: ahh, didn't see that he excluded - as well. silly me.
Ask three DBAs a question, and you’ll get four different answers.

-Brent Ozar
User avatar
Thirster42
Guru
Guru
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
 
Posts: 4646
Joined: Tue Nov 11, 2008 5:49 pm
Location: Jefferson City, Mo
Unrated

Re: Add without +

Postby Emtucifor on Fri Dec 17, 2010 2:41 am

rizzo wrote:
Code is hidden, SHOW


Technically, this violates the spirit of the challenge. :-p
God cries a little bit every time someone builds a database.
User avatar
Emtucifor
Guru
Guru
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033
 
Posts: 2835
Joined: Fri May 30, 2008 9:30 pm
Location: Bellingham, WA
Unrated

Re: Add without +

Postby phillip1882 on Mon Mar 14, 2011 11:11 pm

here's my bitwize version
Code is hidden, SHOW
User avatar
phillip1882
Apprentice
Apprentice
LTD Bronze - Rating: 22
 
Posts: 19
Joined: Mon Mar 14, 2011 11:05 pm
Location: florida

Re: Add without +

Postby Emtucifor on Tue Mar 15, 2011 1:08 am

phillip1882 wrote:here's my bitwize version
Code is hidden, SHOW

Nice! I like the thinking, a lot. It's interesting. Instead of "sliding" from right to left like we're all taught by adding in school, you did a kind of parallel divergence to two "streams" that together still faithfully represented the value, until one stream was empty.

You could have done this one bit at a time, with AND and OR still sliding from the right, but this way will be fewer steps, in some way relative to the number of zeroes in each number's binary expansion.

Full marks.
God cries a little bit every time someone builds a database.
User avatar
Emtucifor
Guru
Guru
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033
 
Posts: 2835
Joined: Fri May 30, 2008 9:30 pm
Location: Bellingham, WA

Re: Add without +

Postby phillip1882 on Tue Mar 15, 2011 8:31 pm

Thanks! Yes i liked this little challange. the primary downside with my algorithm is that while on average will take no more than roughly n/2 operations (n being the number of bits), there are some edge cases such as 31 +1 where it will take n.
(p.s. i hate to be nit picky but it's AND and XOR ;-) )
User avatar
phillip1882
Apprentice
Apprentice
LTD Bronze - Rating: 22
 
Posts: 19
Joined: Mon Mar 14, 2011 11:05 pm
Location: florida
Unrated

Re: Add without +

Postby Thirster42 on Tue Mar 15, 2011 8:46 pm

is this considered cheating?

Code is hidden, SHOW


more condensed version:

Code is hidden, SHOW
Ask three DBAs a question, and you’ll get four different answers.

-Brent Ozar
User avatar
Thirster42
Guru
Guru
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
 
Posts: 4646
Joined: Tue Nov 11, 2008 5:49 pm
Location: Jefferson City, Mo
Unrated

Re: Add without +

Postby Emtucifor on Tue Mar 15, 2011 8:59 pm

phillip1882 wrote:Thanks! Yes i liked this little challange. the primary downside with my algorithm is that while on average will take no more than roughly n/2 operations (n being the number of bits), there are some edge cases such as 31 +1 where it will take n.
(p.s. i hate to be nit picky but it's AND and XOR ;-) )

No, no, love to be nitpicky. You're absolutely right that it's XOR and that OR would NOT work.
God cries a little bit every time someone builds a database.
User avatar
Emtucifor
Guru
Guru
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033
 
Posts: 2835
Joined: Fri May 30, 2008 9:30 pm
Location: Bellingham, WA
Unrated

Re: Add without +

Postby Emtucifor on Tue Mar 15, 2011 9:04 pm

Thirster42 wrote:is this considered cheating?

Hmmm. I like exploiting the integer->date implicit conversion in SQL Server. But the function you used is still an Adding function. So I have to say it's cheating. :)
God cries a little bit every time someone builds a database.
User avatar
Emtucifor
Guru
Guru
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033
 
Posts: 2835
Joined: Fri May 30, 2008 9:30 pm
Location: Bellingham, WA
Unrated

Re: Add without +

Postby Emtucifor on Tue Mar 15, 2011 9:10 pm

P.S. Philip, technically your answer is cheating a bit because it's not inline. But if it was in SQL Server, it could be a CTE, so I let it slide. :)
God cries a little bit every time someone builds a database.
User avatar
Emtucifor
Guru
Guru
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033
 
Posts: 2835
Joined: Fri May 30, 2008 9:30 pm
Location: Bellingham, WA
Unrated

Re: Add without +

Postby Thirster42 on Tue Mar 15, 2011 10:12 pm

Emtucifor wrote:
Thirster42 wrote:is this considered cheating?

Hmmm. I like exploiting the integer->date implicit conversion in SQL Server. But the function you used is still an Adding function. So I have to say it's cheating. :)



bah. you said no + or -, and no aggregates, and must be inline. I DISAGREE!!!

;)
Ask three DBAs a question, and you’ll get four different answers.

-Brent Ozar
User avatar
Thirster42
Guru
Guru
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
 
Posts: 4646
Joined: Tue Nov 11, 2008 5:49 pm
Location: Jefferson City, Mo
Unrated

Next