File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programming Diversions and the fly likes puzzle made me trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "puzzle made me trouble" Watch "puzzle made me trouble" New topic
Author

puzzle made me trouble

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

In an interview I have asked to get 125 while performing any kind of mathematical operation between five 0....

ie I have 0 0 0 0 0 and by doing any mathematical operation between them I need to get digit 125...


HINT :- factorial of 0 =1


No Kaustubh No Fun, Know Kaustubh Know Fun..
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

I think a better spot for this would be programming diversions...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

I can reach 120 easily enough. Not yet convinced 125 is possible!
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

yup 120 is kinda easy if you follow the hint:-

fact(fact(0)+fact(0)+fact(0)+fact(0)+fact(0))=120

but i think 125 is not possible
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
Kaustubh G Sharma wrote:yup 120 is kinda easy if you follow the hint:-

fact(fact(0)+fact(0)+fact(0)+fact(0)+fact(0))=120

but i think 125 is not possible


Is cubing a number allowed ?
cube (fact(0) + fact(0) + fact(0) + fact(0) + fact(0))


Joanne
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Joanne Neal wrote:Is cubing a number allowed ?
cube (fact(0) + fact(0) + fact(0) + fact(0) + fact(0))

I would have thought not - it ought to be 5^3, so you need a 3 from somewhere. But I suppose it shows lateral thinking.

In which case:
0 + 0 + 0 + 0 + 0 + one hundred and twenty five
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
Matthew Brown wrote:
Joanne Neal wrote:Is cubing a number allowed ?
cube (fact(0) + fact(0) + fact(0) + fact(0) + fact(0))

I would have thought not - it ought to be 5^3, so you need a 3 from somewhere.

But you could argue that the 3 is a mathematical operator (meaning raise to the third power) rather than a digit.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

if you look at the windows calculator (in scientific mode) there is a cube button, so I think Joanne's solution is spot-on!
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 557
    
    7

Or......

bin2dec (string2bin(( string(fact(0)) + string (0) + string(fact(0))) ^ + bin2dec(string2bin( string(fact(0)) + string(fact(0)))

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
I'm not sure concatenating strings or converting a number to a string can be considered mathematical operations.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
fred rosenberger wrote:if you look at the windows calculator (in scientific mode) there is a cube button, so I think Joanne's solution is spot-on!

Well, if you use the calculator argument, most calculators have mc, m+, mr for accessing a single memory register. With that, you can easily create any integer, with a suitable number of presses to the m+ key. You don't even need the factorial hint - cos(0) has the same result. I think allowing arbitrary calculator key makes things too easy.

More to the point, I really don't buy the argument that x^3 is not a digit. However it does seem probable that this is the solution that the interviewer expected, since none of us can come up with a better one. To me, that means this is an extremely flawed interview problem. It really needs to be better specified.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
Start with... 0 = 0
Multiply both sides by 125: 125 * 0 = 125 * 0
Evaluate the left side: 0 = 122 * 0
Divide both sides by 0: 0/0 = 125
Add 0: 0/0 + 0 = 125 + 0
Subtract 0: 0/0 + 0 - 0 = 125
Add 0 again: 0/0 + 0 - 0 + 0 = 125 + 0
Evaluate the right side: 0/0 + 0 - 0 + 0 = 125

(I'm sure there are more subtle ways to hide the division by zero.)
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
Heh. I actually like that argument better than the cubing solution.

Though I should add - should have added in the last post, really - that Joanne does deserve kudos for considering the cubing solution, rather than just rejecting it out of hand as others had (me, at least). I don't like it, but it is certainly worth considering the possibility that this is what the interviewer meant.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

Matthew Brown wrote:
Joanne Neal wrote:Is cubing a number allowed ?
cube (fact(0) + fact(0) + fact(0) + fact(0) + fact(0))

I would have thought not - it ought to be 5^3, so you need a 3 from somewhere. But I suppose it shows lateral thinking.

In which case:
0 + 0 + 0 + 0 + 0 + one hundred and twenty five



Not true because as Matthew told you need to generate cube by some mathematical operation or by this 5 zeros......
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

Ryan McGuire wrote:Start with... 0 = 0
Multiply both sides by 125: 125 * 0 = 125 * 0

//fine till here

Evaluate the left side: 0 = 122 * 0

// from where you've got this 122?
Divide both sides by 0: 0/0 = 125
//then how you get the above step?
Add 0: 0/0 + 0 = 125 + 0
Subtract 0: 0/0 + 0 - 0 = 125
Add 0 again: 0/0 + 0 - 0 + 0 = 125 + 0
Evaluate the right side: 0/0 + 0 - 0 + 0 = 125

(I'm sure there are more subtle ways to hide the division by zero.)
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
Kaustubh, obviously the 122 was a typo; he meant 125. Just pretend he wrote 125 there, and it makes perfect sense.

Well, as much as dividing by zero ever makes sense.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

Mike Simmons wrote:Kaustubh, obviously the 122 was a typo; he meant 125. Just pretend he wrote 125 there, and it makes perfect sense.

Well, as much as dividing by zero ever makes sense.




ohk then what about this line

Divide both sides by 0: 0/0 = 125 didn't get it... {Hope this will not throw any exception}
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
If the previous line was

    0 = 125 * 0

(corrected from 122)

and we divide both sides by 0 (pretending for a moment that this isn't an inherently horrible idea), we get (naively)

    0/0 = (125 * 0)/0

which simplifies to

    0/0 = 125
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

Mike Simmons wrote:If the previous line was

    0 = 125 * 0

(corrected from 122)

and we divide both sides by 0 (pretending for a moment that this isn't an inherently horrible idea), we get (naively)

    0/0 = (125 * 0)/0

which simplifies to

    0/0 = 125

ohk

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

Mike Simmons wrote:
Well, if you use the calculator argument, most calculators have mc, m+, mr for accessing a single memory register. With that, you can easily create any integer, with a suitable number of presses to the m+ key. You don't even need the factorial hint - cos(0) has the same result. I think allowing arbitrary calculator key makes things too easy.

I wouldn't consider m+ a mathematical function, although I think cos(0) would be valid.

and I agree 100% that interview questions like this are flawed. But they are fun puzzles!!!
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
But seriously we can't give up yet!

What can you do with one zero to get other numbers?

0! = 1
That adds 1 to our "single zero" set.

asin(1) = 90 degrees, pi/2 radians or 100

So we're up to 0, 1, 90, 100, and pi/2.

We can square root any of those numbers:
sqrt(90) = 9.someodd
sqrt(100) = 10
sqrt(pi/2) = 1.someodd

If we add int(x) (aka floor(x)) to bag of tricks, we can add 9 and 10 to the numbers we can get to starting with a single 0.

we can apply sqrt() and int() again to 9 and/or 10 to get a 3.

So a single 0 can give us, 0, 1, 3, 9, 10, 90, 100 (among others plus a bunch of irrational numbers).

As a reminder here's how we got each of those:
0 = 0
1 = cos(0)
90 = the angle, in degrees, that has a sin of 1 = asind(1) = asind(cos(0))
100 = the angle, in gradians, that has a sin of 1 = asing(1) = asing(cos(0))
9 = int(sqrt(90)) = int(sqrt(asind(cos(0))))
10 = sqrt(100) = sqrt(asing(cos(0)))
3 = sqrt(9) = sqrt(int(sqrt(asind(cos(0)))))

After a little fiddling around, I found a way to combine 5 of those numbers to come up with 125.

125 = 100 + 10 + 9 + 3 + 3

125 = asing(cos(0)) + sqrt(asing(cos(0))) + int(sqrt(asind(cos(0)))) + sqrt(int(sqrt(asind(cos(0))))) + sqrt(int(sqrt(asind(cos(0)))))

BUT...
It pains me to use both asing() and asind(). I'd like to restrict myself to either one or the other.

I know, how about...

125 = 90 + (9*(3 + 1)) - 1

125 = asind(cos(0)) + ( int(sqrt(asind(cos(0)))) * ( sqrt(int(sqrt(asind(cos(0))))) + cos(0) )) - 0!
...where asind(x) = arcsin(x) in degrees.

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

Ryan McGuire wrote:But seriously we can't give up yet!

What can you do with one zero to get other numbers?

0! = 1
That adds 1 to our "single zero" set.

asin(1) = 90 degrees, pi/2 radians or 100

So we're up to 0, 1, 90, 100, and pi/2.

We can square root any of those numbers:
sqrt(90) = 9.someodd
sqrt(100) = 10
sqrt(pi/2) = 1.someodd

If we add int(x) (aka floor(x)) to bag of tricks, we can add 9 and 10 to the numbers we can get to starting with a single 0.

we can apply sqrt() and int() again to 9 and/or 10 to get a 3.

So a single 0 can give us, 0, 1, 3, 9, 10, 90, 100 (among others plus a bunch of irrational numbers).

As a reminder here's how we got each of those:
0 = 0
1 = cos(0)
90 = the angle, in degrees, that has a sin of 1 = asind(1) = asind(cos(0))
100 = the angle, in gradians, that has a sin of 1 = asing(1) = asing(cos(0))
9 = int(sqrt(90)) = int(sqrt(asind(cos(0))))
10 = sqrt(100) = sqrt(asing(cos(0)))
3 = sqrt(9) = sqrt(int(sqrt(asind(cos(0)))))

After a little fiddling around, I found a way to combine 5 of those numbers to come up with 125.

125 = 100 + 10 + 9 + 3 + 3

125 = asing(cos(0)) + sqrt(asing(cos(0))) + int(sqrt(asind(cos(0)))) + sqrt(int(sqrt(asind(cos(0))))) + sqrt(int(sqrt(asind(cos(0)))))

BUT...
It pains me to use both asing() and asind(). I'd like to restrict myself to either one or the other.

I know, how about...

125 = 90 + (9*(3 + 1)) - 1

125 = asind(cos(0)) + ( int(sqrt(asind(cos(0)))) * ( sqrt(int(sqrt(asind(cos(0))))) + cos(0) )) - 0!
...where asind(x) = arcsin(x) in degrees.



It bounces over my head .I've not tried to read it fully because I have maths phobia . May be some one from good maths background can check it's correctness....
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

The math looks right to me, and it's pretty brilliant. However, it does bring into question what operations are permitted. Why should we allow square roots (i.e., x ^ 0.5), but not allow cubes (i.e. x ^ 3)? If we do allow cubes, then Joanne's solution is much simpler.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
Greg Charles wrote:The math looks right to me, and it's pretty brilliant. However, it does bring into question what operations are permitted. Why should we allow square roots (i.e., x ^ 0.5), but not allow cubes (i.e. x ^ 3)?

Yeah, it's unclear. But one possible answer is that you don't need a numeral 2 to write a square root sign, but you do need a numeral 3 to write a cube root.

On the other hand, I was considering using a % sign as a possible operation, when I was looking for solutions. That's pretty borderline even by my interpretation - depends if you're thinking of those two circles as being actual zeroes, or just part of the symbol.

Regardless, I haven't checked Ryan's solution carefully myself, but it looks good, and I'm pretty confident it's right if he says it is. So kudos to him for his solution.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

Greg Charles wrote:The math looks right to me, and it's pretty brilliant. However, it does bring into question what operations are permitted. Why should we allow square roots (i.e., x ^ 0.5), but not allow cubes (i.e. x ^ 3)? If we do allow cubes, then Joanne's solution is much simpler.


But we can't allow cubes Greg ...
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

For me it's damn difficult to do this....

Ryan McGuire wrote:But seriously we can't give up yet!

What can you do with one zero to get other numbers?

0! = 1
That adds 1 to our "single zero" set.

asin(1) = 90 degrees, pi/2 radians or 100

So we're up to 0, 1, 90, 100, and pi/2.

We can square root any of those numbers:
sqrt(90) = 9.someodd
sqrt(100) = 10
sqrt(pi/2) = 1.someodd

If we add int(x) (aka floor(x)) to bag of tricks, we can add 9 and 10 to the numbers we can get to starting with a single 0.

we can apply sqrt() and int() again to 9 and/or 10 to get a 3.

So a single 0 can give us, 0, 1, 3, 9, 10, 90, 100 (among others plus a bunch of irrational numbers).

As a reminder here's how we got each of those:
0 = 0
1 = cos(0)
90 = the angle, in degrees, that has a sin of 1 = asind(1) = asind(cos(0))
100 = the angle, in gradians, that has a sin of 1 = asing(1) = asing(cos(0))
9 = int(sqrt(90)) = int(sqrt(asind(cos(0))))
10 = sqrt(100) = sqrt(asing(cos(0)))
3 = sqrt(9) = sqrt(int(sqrt(asind(cos(0)))))

After a little fiddling around, I found a way to combine 5 of those numbers to come up with 125.

125 = 100 + 10 + 9 + 3 + 3

125 = asing(cos(0)) + sqrt(asing(cos(0))) + int(sqrt(asind(cos(0)))) + sqrt(int(sqrt(asind(cos(0))))) + sqrt(int(sqrt(asind(cos(0)))))

BUT...
It pains me to use both asing() and asind(). I'd like to restrict myself to either one or the other.

I know, how about...

125 = 90 + (9*(3 + 1)) - 1

125 = asind(cos(0)) + ( int(sqrt(asind(cos(0)))) * ( sqrt(int(sqrt(asind(cos(0))))) + cos(0) )) - 0!
...where asind(x) = arcsin(x) in degrees.

anirudh jagithyala
Ranch Hand

Joined: Dec 07, 2010
Posts: 41
Hi All,

Firstly look at the problem

In an interview I have asked to get 125 while performing any kind of mathematical operation between five 0....

ie I have 0 0 0 0 0 and by doing any mathematical operation between them I need to get digit 125...


HINT :- factorial of 0 =1


use of factorial function is valid ie use of mathematical functions is valid { factorial is not the basic mathematical operation but a specialised mathematical function}

So the solution given by Joanne
cube (fact(0) + fact(0) + fact(0) + fact(0) + fact(0))

is absolutely right....

Check out the definitions for mathematical operation and mathematical functions........
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
We seem to be going in circles now. I don't see anything new here. What "definitions for mathematical operation and mathematical functions" are you referring to? If we had any standard definition that folks agreed to, this would be a much better problem.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
anirudh jagithyala wrote:Hi All,
So the solution given by Joanne
cube (fact(0) + fact(0) + fact(0) + fact(0) + fact(0))

is absolutely right....

Check out the definitions for mathematical operation and mathematical functions........


Yeah, we get that. The issue (I hesitate to call it a "problem") with that solution is whether or not cube(x) is a "valid" basic mathematical operation / function for the sake of this problem.

If push comes to shove, I could define my own function that takes five arguments and returns 125:



Then all I need to do is point out that onetwentyfive(0, 0, 0, 0, 0) = 125 and declare the problem solved.

If you say that that function is invalid because it has the constant 125 embedded in it, then I could just as easily argue that cube(x) is invalid because it has an implicit 3 embedded in it:



I can see the counterargument that cube(x) could be written without the 3:



I would then counter by saying onetwentyfive() could also be written without any constants:


I admit that my own solution depends on int(x) qualifying as a valid mathematical operation / function. The criteria I used for determining the validity of a operation / function was whether or not I use any additional numbers when using the function in hand-written equations. When using the int() function, I use either "int()" or the "floor" operator, which looks like an uppercase "L" but taller and narrower plus the mirror image of that. In contrast, when cubing a number, I don't write out "cube"; I use a superscript "3". What about square root? There are a handful of notations for that: a bare radical sign, a radical with a small 2, a superscript "0.5" or maybe, caret and a 0.5 and the sqrt() function. I use the bare radical more often than the other options combined. Using my own personal practice as the standard, sqrt() and int() are valid, while sqr(), cube(), and cubert() are invalid.


Granted, in the absence of explicit instructions in the original post, using your own personal practice or even some some other criteria to determine valid functions/operators is as correct as anything I've done. However, I'm not convinced that using cube() is "absolutely correct".
anirudh jagithyala
Ranch Hand

Joined: Dec 07, 2010
Posts: 41
i looked at the requirements from my point of view......
This argument would continue endless until the requirement is exactly fixed as all of us are assuming our own assumptions.

It would be better for the person who posted originally kaustabh to give the fixed set of requirements and constraints.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
Agreed. I only objected because you were using terms like "absolutely" and repeating "use of mathematical functions" in bold, without acknowledging the ambiguities we've been talking about.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

anirudh jagithyala wrote:i looked at the requirements from my point of view......
This argument would continue endless until the requirement is exactly fixed as all of us are assuming our own assumptions.

It would be better for the person who posted originally kaustabh to give the fixed set of requirements and constraints.



What to do guys I only know the question and struggling with the answer that's why I posted it here....I think I have to find that interviewer who asked me this.....
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

Mike Simmons wrote:Agreed. I only objected because you were using terms like "absolutely" and repeating "use of mathematical functions" in bold, without acknowledging the ambiguities we've been talking about.


Because I was given the same parameter for the task...I am not very good with maths to figure out whether this is possible or not... that's why I posted it over here...Just too good to be true
Tak Ming Laio
Ranch Hand

Joined: Jun 10, 2003
Posts: 37
(.008)^(-(0!))

2 0's make an 8
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

Tak Ming Laio wrote:(.008)^(-(0!))

2 0's make an 8


What you mean by this?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
Look at the number 8. It is written with two loops, that look like two zeroes. Sort of.

It may or may not be a valid solution (based on rules that were never properly defined, after all) but at least it was creative.
 
wood burning stoves
 
subject: puzzle made me trouble
 
Similar Threads
how to use parseInt()
the world's smallest number?
Comparison of ArrayList and HashMap
WA #1.....word association
Expressing unsigned integers and longs