Best search algorithm to find condition within a range

Ian Kelly ian.g.kelly at gmail.com
Tue Apr 7 19:44:57 CEST 2015


On Tue, Apr 7, 2015 at 11:07 AM,  <jonas.thornvall at gmail.com> wrote:
> Den tisdag 7 april 2015 kl. 18:34:32 UTC+2 skrev Dave Angel:
>> Once again, there's no point in doing a search, when a simple integer
>> divide can give you the exact answer.  And there's probably no point in
>> going left to right when right to left would yield a tiny, fast program.
>>
>> I haven't seen one line of Python from you yet, so perhaps you're just
>> yanking our chain.  I'm not here to optimize Javascript code.
>>
>> Using only Python 3.4 and builtin functions, this function can be
>> implemented straightforwardly in 7 lines, assuming number is nonnegative
>> integer, and base is positive integer.  It definitely could be done
>> smaller, but then the code might be more confusing.
>>
>> --
>> DaveA
>
> So you can tell me the first (higest) digit of the integer 2932903594368438384328325832983294832483258958495845849584958458435439543858588435856958650865490
>
> Using base 429496729?

>>> def to_base(number, base):
...     digits = []
...     while number > 0:
...         digits.append(number % base)
...         number //= base
...     return digits or [0]
...
>>> to_base(2932903594368438384328325832983294832483258958495845849584958458435439543858588435856958650865490, 429496729)
[27626525, 286159541, 134919277, 305018215, 329341598, 48181777,
79384857, 112868646, 221068759, 70871527, 416507001, 31]

> How long time did it take to find it?

About 15 microseconds.



More information about the Python-list mailing list