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 Basic maths (May be simple) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Basic maths (May be simple)" Watch "Basic maths (May be simple)" New topic
Author

Basic maths (May be simple)

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 411

I have a number a let's say 390. I want to find smallest number which is greater than 390 and divisible by 180. Could't think of a good way feeling dumb


Pawan Chopra
SCJP - DuMmIeS mInD
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
Sounds like you need a loop that starts a count at 0, and then increments the count by 180 until it is larger than X.


Ping & DNS - my free Android networking tools app
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 411

That is what I thought but could not convince
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
So you don't think it could be made to work? Why not?
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 411

It will work for sure but i think interviewer was looking for some tricky answer. He said loop is not good.
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 411

I think this is better

(Input +divider) - ( input mod divider)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

pawan chopra wrote:I have a number a let's say 390. I want to find smallest number which is greater than 390 and divisible by 180. Could't think of a good way feeling dumb


<putting on my math geek hat>
In terms of available tricks, the number 180 is the product of 9 times 20. From this, we can say that whatever is divisible by 180, is also ...

* divisible by 20. Checking if something is divisible by 20 is simple -- just make sure the units digit is zero and the tens digit is even.

* divisible by 9. Checking if something is divisible by 9 is also simple -- just sum up all the digits, and the sum should be 9. And if the sum is more than one digit, repeat the test, by summing the digits of the sum.


So, I would start a loop at 400 (which is the smallest number greater than 390 that is divisible by 20), check if it is divisible by 9. If it is divisible by 9, you're done. If not, then add 20 and try again.


Doing this mentally... 400, 420, 440, 460, 480, 500, 520, 540... done. The answer is 540.

<taking off my math geek hat>

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

pawan chopra wrote:I think this is better

(Input +divider) - ( input mod divider)


Well, if you are going to do it with mod math, then in pseudo code ...

if (390 ^ 180 is zero) return 390
else return ((390 / 180) +1) * 180;

[EDIT: Just noticed that the result has to be greater than 390, so modified the pseudo code above.]

Henry
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2402
    
  28

Well, if someone asked me that specific question. I will say the answer is


return 540;


If the want to know the answer for any given number, the answer is


return n/180 + n%180==0?0:1;
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
i think interviewer was looking for some tricky answer. He said loop is not good.

This is a terrible interview question. If he was serious about looking for something like Henry suggested then that guy has no clue how to identify good developers, and you're better off not working there.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
It doesn't seem that bad. Using mod arithmetic is not really tricky, and, well, it is an O(1) solution rather than an O(n) solution.

Coding problems in interviews are often problematic - it's difficult to create a good problem that is really representative of the skills most important in a developer. But they can still be of some use for screening and for observing how a developer thinks. And to see how careful they are about details. I could see using this problem for a small part of an interview, letting the developer code a solution while I watched, to see how they do. I'd be most interested to see if they write tests to verify the corner cases, for example. If they gave a solution using a loop, I'd be OK with that, and if we still had time, I'd ask: what if the starting number was around a billion, rather than something like 390? That's a legitimate way to see if a mod-based solution occurs to the candidate. If they don't see it, well, it's not too big a deal. Accuracy is more important.

Here's an improved version, in the sense that it works correctly for a few more values than Henry's solution did. (Acknowledging that I shamelessly stole Henry's code as a starting point.)

This is still not entirely correct though. So, a challenge: in what cases is the above code still incorrect? How can it be fixed?
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1010
    
    3
Mike Simmons wrote:...And to see how careful they are about details.


Exactly. Once the candidate has a solution that works for F(390, 180), do they consider F(360, 180), F(0, 180), F(-390, 180), F(390, -180), F(390, 0), etc.?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Basic maths (May be simple)