Looking for something similar to Python's bisect_right

Zeke lcarsos at lcarsos.com
Wed Oct 30 12:16:57 PDT 2013


On Wednesday, 30 October 2013 at 06:10:59 UTC, Ali Çehreli wrote:
> On 10/29/2013 11:04 PM, Zeke wrote:
>
> lowerBound and friends are related:
>
>   http://dlang.org/phobos/std_range.html#.lowerBound
>
> Ali

lowerBound returns a range with the last value being the sqrt, so 
I can't directly iterate over it because the squares of primes 
get into the primes array. My code looks like this now:

...
auto sqrt = cast(ulong)sqrt(cast(double)num);
auto sorted = assumeSorted(primes);
ulong upper = sorted.lowerBound(sqrt).length + 1;
foreach(ulong prime; sorted[0..upper]) {
...

Which has no speed improvement, but is using a standard library, 
so that accomplishes that.

I'd like to refactor so that the primes array is always a 
SortedRange, so I don't have to cast it for every call of 
is_prime(). What type would I need to change is_prime(ulong[] 
primes, ulong num) to so that I could give it the SortedRange?

Also how would I insert a found prime onto the back of a 
SortedRange?

Zeke


More information about the Digitalmars-d-learn mailing list