Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Compare two numbers N1 = abc, N2 = def by constructing a function f(a,b,c,d,e,f) that:

  • returns 1 if N1 > N2
  • returns -1 if N1 < N2

Note: You are not required to return any value for any other relation between N1 and N2. e.g. when they are equal or when their relation is undefined (complex numbers).

other constrains:

  • all numbers are integers
  • a,b,c,d,e,f may be positive or negative but not zero.
  • |a|,|d| < 1000
  • |b|,|c|,|e|,|f| < 1010
  • running time less than few seconds

Examples:

f(100,100,100,50,100,100) = 1
f(-100,100,100,50,100,100) = 1
f(-100,99,100,50,100,100) = -1
f(100,-100,-100, -1, 3, 100) = 1
f(535, 10^9, 10^8, 443, 10^9, 10^9) = -1

This is code golf. Shortest code wins.

share|improve this question
3  
What about if they're equal, should it return 0? Or you assuming that there's no way that N1 will equal N2? –  Jonathan M Davis Feb 12 '11 at 2:28
    
Can we get some sample input/outputs? –  Dogbert Feb 12 '11 at 9:37
    
@Jonathan: I'm not specifying the "being equal" case on purpose. Do as you please. You may even assume that they are never equal. –  Eelvex Feb 12 '11 at 10:29
    
@Dogbert: done. –  Eelvex Feb 12 '11 at 10:42
    
|b|,|c|,|e|,|f| < 10^10 seems to contradict your last example –  belisarius Feb 12 '11 at 14:28
show 13 more comments

5 Answers

up vote 3 down vote accepted

Mathematica, 110 chars

z[a_,b_,c_,d_,e_,f_]:=With[{g=Sign[a]^(b^c),h=Sign[d]^(e^f)},If[g!=h,g,g*Sign[Log[Abs[a]]b^c-Log[Abs[d]]e^f]]]
share|improve this answer
    
What kind of Mathematica do you use there and what magic incantation has to be used to actually get this to work? Putting the above into Mathematica 8 just yields »Syntax::bktwrn: "z(a_,b_,c_,d_,e_,f_)" represents multiplication; use "z[a_,b_,c_,d_,e_,f_]" to represent a function.« and »Syntax::sntxf: "z(a_" cannot be followed by ",b_,c_,d_,e_,f_):=sgn(ln(abs a)b^c-ln(abs d)e^f)".« –  Joey Mar 27 '11 at 14:01
    
Fails the testcase 3,-3,3,-4,1,1, if I'm not completely mistaken (don't have Mathematica here, but Wolfram Alpha seems to agree). –  Ventero Mar 27 '11 at 14:02
    
Ok, got it to work now with z[a_,b_,c_,d_,e_,f_]:=Sign[Log[Abs[a]]b^c-Log[Abs[d]]e^f] which is considerably longer than what you have there, though. I probably am missing something here. –  Joey Mar 27 '11 at 14:08
    
@Joey, I don't actually have Mathematica, so I was testing with the Wolfram Alpha interface. It appears that it's much more generous with what it accepts. Ah well - the priority is that @Ventero correctly points out a bug with the logic. –  Peter Taylor Mar 27 '11 at 14:13
    
Will it "run in less than a few seconds" for z[535, 10^9, 10^8, 443, 10^9, 10^9]? –  Eelvex Mar 27 '11 at 16:41
show 3 more comments

Ruby 1.9, 280 227 characters

z=->a,b,c,d,e,f{l=->a{Math.log a}
u=->a,b,c{b&1<1&&c>0?a.abs: a}
b=u[b,c,1];e=u[e,f,1]
a=u[a,b,c];d=u[d,e,f]
a|b|d|e>0?(l[l[a]]+c*l[b])<=>(l[l[d]]+f*l[e]):a<0?d>0?-1:-z[-a,b,c,-d,e,f]:d<0?1:b<0?e>0?-1:-z[a,-b,c,d,-e,f]:e<0?1:0}

I know this is a bit longer than the other solutions, but at least this approach should work without calculating abc or bc.

Edit:

  • (279 -> 280) Fixed a bug when a**b**c < 0 and d = 1.
  • (280 -> 227) Removed an unnecessary check for a special case.

Testcases:

z[100, 100, 100, 50, 100, 100] == 1
z[-100, 100, 100, 50, 100, 100] == 1
z[-100, 99, 100, 50, 100, 100] == -1
z[100, -100, -100, -1, 3, 100] == 1
z[535, 10**9, 10**8, 443, 10**9, 10**9] == -1
z[-1, -1, 1, 2, 2, 2] == -1
z[1, -5, -9, 2, -1, 2] == -1
z[1, -5, -9, 2, -1, 3] == 1
z[3, -3, 3, -4, 1, 1] == 1
z[-2, 1, 1, 1, 1, 1] == -1
z[1, 1, 1, -1, 1, 1] == 1
z[1, 1, 1, 2, 3, 1] == -1
z[1, 1, 1, 2, -3, 2] == -1
z[1, 1, 1, 2, -3, 1] == 1
z[-1, 1, 1, 1, 1, 1] == -1
z[2, 3, 1, 1, 1, 1] == 1
z[2, -3, 2, 1, 1, 1] == 1
z[2, -3, 1, 1, 1, 1] == -1
share|improve this answer
add comment

Python 2.6 (this doesn't actually work)

import cmath
g=cmath.log
f=lambda a,b,c,d,e,f:-1+2*((c*g(b)+g(g(a))-f*g(e)-g(g(d))).real>0)

today i learnt python has a complex log function. so, blindly double log both sides and look at the real component. works for 4 out of the 5 tests. not sure what's happening with the fourth one.

print f(100,100,100,50,100,100) == 1
print f(-100,100,100,50,100,100) == 1
print f(-100,99,100,50,100,100) == -1
print f(100,-100,-100, -1, 3, 100) == 1 # failure, sadness.
print f(535, 10^9, 10^8, 443, 10^9, 10^9) == -1
share|improve this answer
    
Well, it is that I messsed up the example that is wrong :/ Sorry ... fixing it... –  Eelvex Feb 14 '11 at 23:41
    
my code still returns -1 for the fourth example wrong when a=100 –  roobs Feb 14 '11 at 23:47
    
Comparing just the real part is not correct. –  Eelvex Feb 14 '11 at 23:57
    
yeah, that part was a stab in the dark. this is where i regret skipping that course in complex analysis –  roobs Feb 15 '11 at 9:06
add comment

Python (99)

from math import*
from numpy import*
l=log
def f(a,b,c,d,e,f):return sign(l(a)*l(b)*c-l(d)*l(e)*f)
share|improve this answer
8  
Fails on negatives. –  J B Feb 12 '11 at 8:53
add comment

Haskell, 44 characters

n True=1
n _=1-2
g a b c d e f=n$a^b^c>d^e^f

Runs under a second for all the test examples on my machine.

share|improve this answer
1  
Bullshit. You didn't test this code at all. –  eternalmatt Jul 20 '11 at 22:58
    
I have a supermachine from the future. –  Thomas Eding Jul 21 '11 at 19:04
    
Also the machine code is highly optimized. Inspecting the compiled code, it does logarithms and other operations. The THC (Trinithis Haskell Compiler) is one smart compiler!!! Who said I had to use GHC or Hugs? In fact, I can provide real source code for my compiler which has a GHC dependency. It will output fast code for this source code even on /your/ machine. What's more, it will compile ANY Haskell program to the same degree of accuracy as the GHC (GHC is the backend). –  Thomas Eding Jul 22 '11 at 19:23
    
@downvoters: I'll provide the whole source code this weekend (I won't be home for a while) for my compiler to prove to you that it runs quickly. Talking about language speed is nonsense, as it all boils down to the compiler/interpreter. –  Thomas Eding Jul 22 '11 at 19:45
    
And I wasn't even talking about bullshit speed/efficiency. I was referring to how (BEFORE you edited the post and changed the function b to n) the type of function f was given by f :: (Ord a, Num a, Integral b2, Integral (Bool -> t), Integral b, Integral b1) => a -> (Bool -> t) -> b -> a -> b1 -> b2 -> t Pretty strange stuff, huh? –  eternalmatt Jul 22 '11 at 22:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.