[OT] Algorithm question

MysticZach via Digitalmars-d digitalmars-d at puremagic.com
Wed May 3 06:04:41 PDT 2017


On Tuesday, 2 May 2017 at 23:09:54 UTC, MysticZach wrote:
> On Tuesday, 2 May 2017 at 21:00:36 UTC, Timon Gehr wrote:
>> On 02.05.2017 22:42, MysticZach wrote:
>>> On Tuesday, 2 May 2017 at 13:44:03 UTC, MysticZach wrote:
>>>    for (;;) {
>>>       if (P(a[i]))
>>>          return i;
>>>       ++count;
>>>       if (count == length)
>>>          return -1;
>>>       i += hop;
>>>       if (i < length)
>>>          continue;
>>>       if (i < skipLength)
>>>          i += hop;
>>
>> (I guess this should be 'while'.)
>
> skipLength is determined modulo hop, thus it can't be more than 
> one hop away.

Actually, I did botch the implementation. The hopping interval 
must be based on the prime rather than on n. But you could still 
short circuit the while loop, I think. `if (prime - n > hop) { 
skipLength = n + ((prime - n) % hop); }




More information about the Digitalmars-d mailing list