
-----------------------------------
Edward
Thu Jul 07, 2011 3:31 am

Math Problem
-----------------------------------
Hi guys,

First post here. I am writing a script in bash[linux] and I need to figure out the logic for this one problem.

Given a number x; round it up/down to the nearest multiple of 5[whole number]. x may have decimals.

i.e :

1) 13->15
2) 58->60

What is the logic for this?
What kind of exercises can I do to improve my ability to solve these problems?

-----------------------------------
Insectoid
Thu Jul 07, 2011 8:20 am

RE:Math Problem
-----------------------------------
This is an example of the pigeonhole principle. It can be solved with the formular ceil(a/b)*b where a is the given number and b is 5. 

ceil(13/5)*5 = 15
ceil (58/5)*5 = 60

I dunno if bash supports a ceil function, but you can probably hack one together if you must.

-----------------------------------
apython1992
Thu Jul 07, 2011 8:32 am

RE:Math Problem
-----------------------------------
Welcome! When encountering difficulties with problems you haven't seen before, try breaking them down into smaller steps, and even implement each smaller solution first:

-> How can you determine if a number is a multiple of 5?
-> How can you find the two closest multiples (below and above)?
-> How do you know whether to round up or down?

Try doing these in small steps.  Make it work first with just integers, and then you can work on floating point numbers. And if you have questions about any of these smaller problems, we can break those down into even smaller problems :)

-----------------------------------
crossley7
Thu Jul 07, 2011 10:16 am

RE:Math Problem
-----------------------------------
not sure if this is a built in function for you, but
round (x/5) * 5 
will give you the nearest multiple of 5.  Some languages have that built in, others may have just floor or ceil built in and you will have to do a bit of detection for what the decimal is

-----------------------------------
apython1992
Thu Jul 07, 2011 11:06 am

RE:Math Problem
-----------------------------------
It sounds like OP needs to implement this on his/her own.

-----------------------------------
apython1992
Thu Jul 07, 2011 11:08 am

RE:Math Problem
-----------------------------------
Also, @Insectoid, that only works for rounding up.  What about rounding down?  You would have to know when to use ceil/floor.

-----------------------------------
Edward
Thu Jul 07, 2011 12:02 pm

Re: Math Problem
-----------------------------------
This is NOT  a homework problem. This is a home project.

Thank you. I will look if bash has a floor/ceil function.

-----------------------------------
Insectoid
Thu Jul 07, 2011 12:52 pm

Re: RE:Math Problem
-----------------------------------
Also, @Insectoid, that only works for rounding up.  What about rounding down?  You would have to know when to use ceil/floor.

Oh, my bad. I thought he just wanted to round up. 

@OP, this can still be done with fairly basic math. Multiply by 2, round to the nearest 10, and divide by 2. Basically, you don't round to 5. You round something else to the nearest whole (well, the nearest 10 but we want to minimize float division).

-----------------------------------
apython1992
Thu Jul 07, 2011 1:01 pm

Re: Math Problem
-----------------------------------
This is NOT  a homework problem. This is a home project.

Thank you. I will look if bash has a floor/ceil function.
It's okay even if it was a homework problem, because you asked a properly formatted question and aren't just asking somebody to do it for you.

@Insectoid, maybe I'm thinking of this differently, but I don't see what the multiplying by 2 does.  You still have to round, no matter what.

-----------------------------------
Brightguy
Fri Jul 08, 2011 5:31 pm

Re: Math Problem
-----------------------------------
What is the logic for this?
The simplest thing is to compute x &minus; (x mod 5), where (x mod 5) lies in (&minus;2.5, 2.5] (the so-called symmetric range).

What kind of exercises can I do to improve my ability to solve these problems?
Not sure what kind of problems you mean.  For remainder calculations you should study modular arithmetic.

This is an example of the pigeonhole principle.http://img27.imageshack.us/img27/3014/waitwhata.jpg
Wait, what?

-----------------------------------
apython1992
Fri Jul 08, 2011 9:29 pm

Re: Math Problem
-----------------------------------
What is the logic for this?
The simplest thing is to compute x &#8722; (x mod 5), where (x mod 5) lies in (&#8722;2.5, 2.5] (the so-called symmetric range).


If I'm not mistaken, this would just floor it to the next lowest multiple of five, not round.  If x = 13, 13 - (13 mod 5) = 13 - 3 = 10.

-----------------------------------
Insectoid
Fri Jul 08, 2011 9:53 pm

RE:Math Problem
-----------------------------------
@brightguy, I misunderstood the question at first.

@apython1992, it's a lot easier to round to 10 than 5, using the built-in rounding function. Doubling it allows you to do this. Bear in mind that 'round to the nearest 10' is the same as 'round one tenth of this, to the nearest whole'. You could multiply by 0.2 and round, then divide by 0.2, if that's more clear (I dunno what kind of perfomance hit that would take). 

Alternatively, you could just use conditions. 
[code]
if (x mod 5 