All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Algorithms
Another Man's Floor (Posted on 2005-11-23) Difficulty: 4 of 5
Let a, b, and m be positive whole numbers. Required is a fast algorithm for evaluating Σk=0..m floor(ka/b), floor(x) being the greatest integer that does not exceed the real number x.

See The Solution Submitted by Richard    
Rating: 4.5000 (2 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
Solution My best solution | Comment 6 of 11 |

I found an identity which takes most of the work out of finding the solution. This may be written as

(1) t (a, b) = Sum (for k =0 to b-1) Floor[ k a / b] = (a b - a - b + GCD(a, b)) / 2

This means that the problem may be written as

(2) s (a, b, m) = t (a, b) + Sum (for k =b to m) Floor[ k a / b]

We can improve the efficiency of this by letting r = Floor [ (m+1) / b] so (2) becomes

(3) s (a, b, m) = t (r a, r b) + Sum (for k =r b to m) Floor[ k a / b]

Finally we minimise the residual calculation by ensuring that (a, b) = (a / GCD(a, b), b / GCD(a, b))

Edited on December 29, 2005, 6:44 pm

Edited on December 29, 2005, 7:03 pm
  Posted by goFish on 2005-12-29 18:38:06

Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (6)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information