Reply to topic  [ 3 posts ] 
Why are some challenges so difficult to grasp 
Author Message
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
Trying to develop a rounding system that would always round up to the nearest x (50 for example) unless it was a multiple of 50

In a couple of mins I had 3 different solutions and identified the fastest.... yet the dev who it was for still couldnt grasp how it was done

best solution I had:

Code:
if(x % minmultiple ==0)
{
     return x;
}
else
{
     return x + minmultiple - (x % minmultiple)
}

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Thu Aug 18, 2011 10:11 am
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
If you're always rounding up then that's by far the quickest way.

if x = 50 then return = 50.

if x = 51 then return = 51 + 50 - (51 modulo 50)

i.e. 101 - 1 = 100.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Aug 18, 2011 10:17 am
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
Fogmeister wrote:
If you're always rounding up then that's by far the quickest way.


thought so, also allows for passing a bit in an enum of rounding state (forceup/forcedown/default) then can extend into the realms of the if block there :)

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Thu Aug 18, 2011 10:49 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.