-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an iterative rational implementation for exact results #13
base: master
Are you sure you want to change the base?
Add an iterative rational implementation for exact results #13
Conversation
Performance on this is decent, tho the numerators and denominators get big fast, which starts to be a bottle neck in performance to maintain exactness. I can calculate all expected values for n up to 1,000 in ~12.5s on my MacBook. There's a cool way to structure this concurrently that I may implement, which should optimize the runtime a bit, tho it'll be the same time complexity. |
Adds an implementation in golang using rational number types to get exact results, and an iterative algorithm for performance improvement compared to recursive approaches. Outputs to CSV for easy data integration with tooling.
beca90d
to
363ab58
Compare
Hey, Matt's never going to merge any of these PRs, but figured I'd review some of them anyway. I did some math that I shared over on Issue #1. If performance is your goal, you can get much better performance if you use the equation at the bottom of the image that I posted. Even with Python, I'm seeing ~0.05 seconds to generate all of the expected values between 0 and 1000, with no recursion, no concurrency, and no caching. time python3 - <<EOF
def expected_hops(pads):
if pads <= 0: return 0
return sum((1/n for n in range(1, pads+1)))
x = [expected_hops(i) for i in range(1001)]
print('Expected hops for 1:', x[1])
print('Expected hops for 10:', x[10])
print('Expected hops for 100:', x[100])
print('Expected hops for 1000:', x[1000])
EOF
# Expected hops for 1: 1.0
# Expected hops for 10: 2.9289682539682538
# Expected hops for 100: 5.187377517639621
# Expected hops for 1000: 7.485470860550343
#
# real 0m0.042s
# user 0m0.034s
# sys 0m0.009s |
Realizing that precision is also your goal, I did the same thing using Python's Again, this is Python, which is nowhere near Go in terms of performance. time python3 - <<EOF
import decimal
def expected_hops(pads):
if pads <= 0: return 0
return sum((decimal.Decimal(1) / decimal.Decimal(n) for n in range(1, pads+1)))
x = [expected_hops(i) for i in range(1001)]
print(x[1000])
EOF
# 7.485470860550344912656518202
#
# real 0m0.314s
# user 0m0.306s
# sys 0m0.008s |
Thanks for the suggestion! Went ahead and updated the algorithm, and it's much quicker, 10ms or so for frog(1000)
|
Adds an implementation in golang using rational number types to
get exact results, and an iterative algorithm for performance
improvement compared to recursive approaches.
Outputs to CSV for easy data integration with tooling.