loading...

re: Daily Challenge #152 - Strongest Number in an Interval VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Available since .NET Core 3.0 ... nice touch! Also in F# the Sequence generation is SO MUCH easier. In C# you'd be better off with a simple for-lo...
 

Well, my F# solution translated to C# would be something like this;

static int StrongestIn(int n, int m)
{
    return Enumerable.Range(n, (m-n))
        .Aggregate((a,b) => BitOperations.TrailingZeroCount(a) > BitOperations.TrailingZeroCount(b)
            ? a : b);
}

There is no built in "MaxBy" in the C#, so here I replaced it with an Aggregate. (The TrailingZeroCount method will use intrinsics (e.g. a CPU instruction) if its available, so I'm not worried about calling it repeatedly for the accumulator.)

Code of Conduct Report abuse