Programming-cum-logic challenge

Need help troubleshooting your PC? We have a couple of geeks in the forum who can help you
Forum rules
Please read the forum rules carefully before you post.

If you like AnimorphsFanForum.com, please consider making a donation. Any donation will go towards the cost of the hosting, the domain and any other running costs.
User avatar
AneeshTheGreat
Donator
Donator
Posts: 1947
Joined: Sat Apr 11, 2009 9:01 am
Gender: [Male][/Male]
Favourite Animorph: Marco
Location: not Mumbai, India :(

Programming-cum-logic challenge

Post by AneeshTheGreat » Fri Sep 17, 2010 11:13 am

I'm not sure where this would thread would be most appropriately placed... Someone please shift it if necessary...

The other day i was kinda bored so i asked my friend for some challenging program to write. He gave me this one. Apparently it's been asked in some Microsoft interview.

The question is: Write a program to find the larger of two positive integers, without using the >,<,>= or <= operators.

Now i've asked two other people, each gave a different solution, with varying efficiency, so i thought it'd be nice to post it on the internet and get as many different solutions as possible.

Here are the three solutions i know:

Mine:
Spoiler:
place both the numbers in a loop. Decrement each no. by one in every iteration. Stop the loop as soon as one of them hits zero. That one is the smaller no. Of course, you'll need temporary variables to save the original values...
Another one:
Spoiler:
Divide one no. by the other. Convert the quotient to an integer (useful for floating points). If a/b is a zero, b is greater. Else, vice versa.
And the best one yet:
Spoiler:
Find out the sum and difference between the two numbers. Add the sum to the difference. Divide that number by two. The result will always be the greater no. (Works with floating points and negatives too)
So that's that. Post your solution. In a spoiler, so that others can still be creative and original.
There are 10 types of people in this world. Those who understand Binary, and those who don't. Now 1/4th a Computer Scientist (or Computer Science Engineer, depending on my mood), i can safely say i fall into the first category.

User avatar
Master Crayak
Commander
Commander
Posts: 440
Joined: Fri Feb 26, 2010 12:37 pm
Gender: [Male][/Male]
Favourite Animorph: Rachel

Re: Programming-cum-logic challenge

Post by Master Crayak » Fri Sep 17, 2010 2:21 pm

You lost me at "so i asked my friend for some challenging program to write."
The beatings will continue until morale improves.
http://imgs.xkcd.com/comics/atheists.png" onclick="window.open(this.href);return false;
http://www.smbc-comics.com/index.php?db ... =172#comic" onclick="window.open(this.href);return false;

User avatar
Current
Eldritch Abomination
Eldritch Abomination
Posts: 1780
Joined: Sat Jul 25, 2009 10:20 pm
Favourite Animorph: Rachel
Location: Southwestern quartersphere

Re: Programming-cum-logic challenge

Post by Current » Fri Sep 17, 2010 4:58 pm

Hmmm. My knowledge of programming is extremely limited, so I'm not sure how workable anything I suggest is. Probably overcomplicated, but hey.
Spoiler:
If the square root of a-b is a real number, a is the greater number, otherwise b. Taking the logarithm works in the same way.
Spoiler:
If lim x->∞ (a/b)^x is 0, a is the smaller number, otherwise b. Yes, I thought it was a reasonable idea to use limits to find the smaller of two integers.
Spoiler:
Take the average, add 1/2. If that's one of the numbers, stop, that is the greater number. Otherwise, increase by one until you reach one of the numbers. That is the greater number. Come to think of it, you could simply add the absolute value of a-b divided by two, but then that's exactly the same as the third suggestion, only in a different order.
And the first one I think of that's relatively simple:
Spoiler:
If the absolute value of a-b equals a-b, a is the greater number, otherwise b.
What is not the answer to this question?

User avatar
AneeshTheGreat
Donator
Donator
Posts: 1947
Joined: Sat Apr 11, 2009 9:01 am
Gender: [Male][/Male]
Favourite Animorph: Marco
Location: not Mumbai, India :(

Re: Programming-cum-logic challenge

Post by AneeshTheGreat » Fri Sep 17, 2010 7:15 pm

The third one is probably the only one that'll work in pretty much any language, as some languages don't have any library functions to evaluate limits, or to check for real nos or absolute values.

Still, it's quite interesting to see other people's thought processes.
There are 10 types of people in this world. Those who understand Binary, and those who don't. Now 1/4th a Computer Scientist (or Computer Science Engineer, depending on my mood), i can safely say i fall into the first category.

User avatar
Ellimist
Administrator
Administrator
Posts: 4949
Joined: Thu Nov 22, 2007 4:48 am
Gender: [Male][/Male]
Favourite Animorph: Jake

Re: Programming-cum-logic challenge

Post by Ellimist » Fri Sep 17, 2010 8:16 pm

Spoiler:
[code=php]<?php $a=99; $b=7; if($a == $b) echo 'Numbers are equal'; else if(floor($a / $b) == 0) echo 'a is less than b'; else echo 'a is greater than b'; ?>[/code]
Edit: I see that this is the second solution.
Edit 2: Current's third solution is the most elegant of them all. Awesome thinking, Current. :good:
Powered by chocolate and Dvorak

User avatar
AneeshTheGreat
Donator
Donator
Posts: 1947
Joined: Sat Apr 11, 2009 9:01 am
Gender: [Male][/Male]
Favourite Animorph: Marco
Location: not Mumbai, India :(

Re: Programming-cum-logic challenge

Post by AneeshTheGreat » Fri Sep 17, 2010 9:40 pm

Ellimist wrote: Edit 2: Current's third solution is the most elegant of them all. Awesome thinking, Current. :good:
... It doesn't work for negative numbers. Sure, that's not part of the question....
There are 10 types of people in this world. Those who understand Binary, and those who don't. Now 1/4th a Computer Scientist (or Computer Science Engineer, depending on my mood), i can safely say i fall into the first category.

User avatar
Current
Eldritch Abomination
Eldritch Abomination
Posts: 1780
Joined: Sat Jul 25, 2009 10:20 pm
Favourite Animorph: Rachel
Location: Southwestern quartersphere

Re: Programming-cum-logic challenge

Post by Current » Mon Sep 20, 2010 4:04 pm

No absolute values pretty limits the simple ways to check...
Depending, you could just replace every time I used absolute values with
Spoiler:
square then take the square root, but I have no idea whether the usual practice with square roots is returning both values, or only the positive one. Similarly, square root then squaring depends on how handling imaginary numbers is usually done.
I could really use some programming knowledge right now, it's pretty obvious I'm thinking this in purely mathematical terms.
What is not the answer to this question?

Pansophist
Gedd
Posts: 8
Joined: Wed Sep 22, 2010 4:30 pm
Gender: [Male][/Male]
Favourite Animorph: Tobias

Re: Programming-cum-logic challenge

Post by Pansophist » Wed Sep 22, 2010 4:49 pm

You're thinking in mathematical terms stumped my programming knowledge...even though it is quite limited, the solutions I came up with before looking at the spoilers pretty much consisted of the ez-mode solutions inside the spoilers I would use for sample programs in a class, not actual applications :(

Fantastic thinking :)

User avatar
AneeshTheGreat
Donator
Donator
Posts: 1947
Joined: Sat Apr 11, 2009 9:01 am
Gender: [Male][/Male]
Favourite Animorph: Marco
Location: not Mumbai, India :(

Re: Programming-cum-logic challenge

Post by AneeshTheGreat » Fri Sep 24, 2010 6:35 am

Current wrote:No absolute values pretty limits the simple ways to check...
Depending, you could just replace every time I used absolute values with
Spoiler:
square then take the square root, but I have no idea whether the usual practice with square roots is returning both values, or only the positive one. Similarly, square root then squaring depends on how handling imaginary numbers is usually done.
Actually, same problem, square roots require library functions. Not that that's a problem, it's still fantastic that you could think of so many different ways.
Current wrote: I could really use some programming knowledge right now, it's pretty obvious I'm thinking this in purely mathematical terms.
That's perfectly fine. All programming knowledge does is limit the number of feasible solutions your otherwise free mind would think of. In fact some of us programmers (or atleast just me) find it far more difficult to think up algorithms in real world logic, preferring to first write the program directly, and then reverse engineer the algorithm when it is required (i'm talking about at college exams).
There are 10 types of people in this world. Those who understand Binary, and those who don't. Now 1/4th a Computer Scientist (or Computer Science Engineer, depending on my mood), i can safely say i fall into the first category.

User avatar
Ellimist
Administrator
Administrator
Posts: 4949
Joined: Thu Nov 22, 2007 4:48 am
Gender: [Male][/Male]
Favourite Animorph: Jake

Re: Programming-cum-logic challenge

Post by Ellimist » Fri Sep 24, 2010 6:41 am

Okay. I asked this to one of my friends in college and he instantly came up with a solution (positive numbers only) :
Spoiler:
[code=php]int a=99, b=6; for(int i=0 ; ; i++) {      if(i == a)      {           printf("%d is the greater number", b);           break;      }      else if(i == b)      {           printf("%d is the greater number", a);           break;      } } [/code]
Powered by chocolate and Dvorak