Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Math Problem
Index -> General Programming
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Edward




PostPosted: Thu Jul 07, 2011 3:31 am   Post subject: 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?
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Thu Jul 07, 2011 8:20 am   Post subject: 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




PostPosted: Thu Jul 07, 2011 8:32 am   Post subject: 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 Smile
crossley7




PostPosted: Thu Jul 07, 2011 10:16 am   Post subject: 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




PostPosted: Thu Jul 07, 2011 11:06 am   Post subject: RE:Math Problem

It sounds like OP needs to implement this on his/her own.
apython1992




PostPosted: Thu Jul 07, 2011 11:08 am   Post subject: 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




PostPosted: Thu Jul 07, 2011 12:02 pm   Post subject: 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




PostPosted: Thu Jul 07, 2011 12:52 pm   Post subject: Re: RE:Math Problem

apython1992 @ Thu Jul 07, 2011 11:08 am wrote:
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).
Sponsor
Sponsor
Sponsor
sponsor
apython1992




PostPosted: Thu Jul 07, 2011 1:01 pm   Post subject: Re: Math Problem

Edward @ Thu Jul 07, 2011 12:02 pm wrote:
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




PostPosted: Fri Jul 08, 2011 5:31 pm   Post subject: Re: Math Problem

Edward @ Thu Jul 07, 2011 3:31 am wrote:
What is the logic for this?

The simplest thing is to compute x − (x mod 5), where (x mod 5) lies in (−2.5, 2.5] (the so-called symmetric range).

Edward @ Thu Jul 07, 2011 3:31 am wrote:
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.

Insectoid @ Thu Jul 07, 2011 8:20 am wrote:
This is an example of the pigeonhole principle.
Posted Image, might have been reduced in size. Click Image to view fullscreen.
Wait, what?
apython1992




PostPosted: Fri Jul 08, 2011 9:29 pm   Post subject: Re: Math Problem

Brightguy @ Fri Jul 08, 2011 5:31 pm wrote:
Edward @ Thu Jul 07, 2011 3:31 am wrote:
What is the logic for this?

The simplest thing is to compute x − (x mod 5), where (x mod 5) lies in (−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




PostPosted: Fri Jul 08, 2011 9:53 pm   Post subject: 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 <2.5)  then
     x-= x mod 5
else
    x += (5-x mod 5)


At least, it should look something like that.
apython1992




PostPosted: Fri Jul 08, 2011 10:34 pm   Post subject: RE:Math Problem

I was thinking in terms of the OP implementing this on his own instead of using a built-in function, but thanks for clearing that up. The conditions would make that work correctly.
Insectoid




PostPosted: Fri Jul 08, 2011 11:02 pm   Post subject: RE:Math Problem

Another way to do it (which applies to any increment) is the following;

y = round (x/k)*k where x is the number to round, and k is the increment. In OP's situation,

y = round (x/5)*5

(Wiki)
Brightguy




PostPosted: Sat Jul 09, 2011 3:15 pm   Post subject: Re: Math Problem

apython1992 @ Fri Jul 08, 2011 9:29 pm wrote:
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.

You missed the second half of my sentence, where I specify the value for the modulo operation (there are really infinitely many values in the congruence class).

Insectoid @ Fri Jul 08, 2011 9:53 pm wrote:
@brightguy, I misunderstood the question at first.

Ah... that remark really threw me for a loop.

Insectoid @ Fri Jul 08, 2011 11:02 pm wrote:
y = round (x/5)*5

BTW, crossley already posted this...
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: