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

This topic is 2878 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

"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

##### Share on other sites
OK, in C/C++, the order of operations is (in part): * / % + -

So, 256 + 7 - (256 % 7),
256 + 7 - (4),
263 - 4 = 259.

##### Share on other sites
I don't know how % fits in the order of operations in C/C++, but doesn't i + j - i = j?

% has higher precedence.

##### Share on other sites

[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]

##### Share on other sites

"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.

##### Share on other sites

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.

##### Share on other sites
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.

##### Share on other sites
What about books for beginner and intermediate learners?

##### Share on other sites
I'm not really sure, to be honest. I haven't come across others.

Thanks, though

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 23
• 11
• 25