osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Challenge: find the first value where two functions differ


On Sat, Aug 5, 2017 at 4:03 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Fri, Aug 4, 2017 at 11:59 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> On Fri, Aug 4, 2017 at 11:50 AM, Chris Angelico <rosuav at gmail.com> wrote:
>>> My logic was that floating point rounding is easiest to notice when
>>> you're working with a number that's very close to something, and since
>>> we're working with square roots, "something" should be a perfect
>>> square. The integer square root of n**2 is n, the ISR of n**2+1 is
>>> also n, and the ISR of n**2-1 should be n-1. I actually wanted to
>>> start at 2**53, but being an odd power, that doesn't have an integer
>>> square root, so I started at 2**52, which has an ISR of 2**26.
>>
>> A slight irony here is that it actually would have taken your script a
>> *very* long time to get to 2**53 having started at 2**52, even only
>> iterating over the perfect squares.
>
> Never mind, I just tried it and it's a few seconds. I guess there are
> not as many perfect squares in that range as I thought.

There are about thirty million of them. Python can calculate square
roots in a handful of nanoseconds on my system, so "a few seconds"
sounds about right.

>>> math.sqrt(2**53) - math.sqrt(2**52)
27797401.62425156
>>> 2**26 * 0.41421356
27797401.46499584

ChrisA