I'm no mathematician, but I found this simple equation rather nifty!

Started by
8 comments, last by ClaudeFleming 12 years, 4 months ago
"To round off an integer i to the next largest even multiple of another integer j, the following formula can be used:
Next_multiple = i + j - i % j

For example, to round off 256 days to the next largest number of days evenly divisible by a week,
values of i = 256 and j = 7 can be substituted into the preceding formula as follows:
next_multiple = 256 + 7 - 256 %7."

Kochan, Stephen G. (2004-07-08). Programming in C (3rd Edition) (Kindle Locations 1311-1314). Pearson Education (US). Kindle Edition.

Where are books that teach you interesting and useful math problems like these. I know school texts are useful, but I like applied math also. Yes, school texts teach applied math, but I want a masterpiece of mathematics literature that teaches you equations like these. I can understand this one after a bit of thought. But I would like a book that teaches you the 'what', the 'how', and the 'why'.

Thanks,

Lee
Advertisement
OK, in C/C++, the order of operations is (in part): * / % + -

So, 256 + 7 - (256 % 7),
256 + 7 - (4),
263 - 4 = 259.
I don't know how % fits in the order of operations in C/C++, but doesn't i + j - i = j?


% has higher precedence.
[TheUnbeliever]

[quote name='MarkS' timestamp='1322850855' post='4889849']I don't know how % fits in the order of operations in C/C++, but doesn't i + j - i = j?


% has higher precedence.
[/quote]

Already edited. Should have Googled first...unsure.gif

"To round off an integer i to the next largest even multiple of another integer j, the following formula can be used:
Next_multiple = i + j - i % j

For example, to round off 256 days to the next largest number of days evenly divisible by a week,
values of i = 256 and j = 7 can be substituted into the preceding formula as follows:
next_multiple = 256 + 7 - 256 %7."

Kochan, Stephen G. (2004-07-08). Programming in C (3rd Edition) (Kindle Locations 1311-1314). Pearson Education (US). Kindle Edition.

Where are books that teach you interesting and useful math problems like these. I know school texts are useful, but I like applied math also. Yes, school texts teach applied math, but I want a masterpiece of mathematics literature that teaches you equations like these. I can understand this one after a bit of thought. But I would like a book that teaches you the 'what', the 'how', and the 'why'.

Thanks,

Lee


What do you mean by "next largest"?
Anyways, the algorithm finds the smallest multiple of j, that is bigger than i.
The algorithm is quite logical and straight forward.
If we want to find the biggest mutliple of j that is not larger than i, that means we're looking for a number
j*k <= i
k is an int, and so j*k is a multiple of j.
Our actual number i is either a multiple of j, or it is not a multiple of j, so:
i = j*k + rest
rest = 0 if our number is a multiple of j, else it has a value. In both cases rest = (i % j) - (this is what % does)
Now we have:
i = j*k + (i % j) <=> j*k = i - (i % j)
Remember j*k is the multiple of j that is smaller or equal to i.
If we want the smallest multiple of j that is larger than i, we simply have to add j:
answer = i + j - (i % j)


I consider this to be a more programming specific problem. What math can do is to teach you how solve such problems.
In my experience, I think they should start much earlier with things like these, rather than just memorize how to solve a specific problem.



What do you mean by "next largest"?

I mean just what the author says. I didn't come up with that equation. That is from the author's own book, his own words.
I think this is important for every person to know, simply because it is elegant and a pleasure to know. That's sad you consider it a programming-related problem.
Not all people are smart enough to come up with that equation on their own. I love solving and especially understanding problems like these. Every mathematics curriculum
should have more and more problems like these in them.
Numerical Recipes. Warning: while this book teaches all sorts of useful applications of math, particularly in a programming setting, it covers mostly more advanced topics. Still, in terms of your original question, it's the best I've found.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
What about books for beginner and intermediate learners?
I'm not really sure, to be honest. I haven't come across others.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Thanks, though :)

This topic is closed to new replies.

Advertisement