DEV Community

Discussion on: Daily Challenge #152 - Strongest Number in an Interval

Collapse
 
nickholmesde profile image
Nick Holmes

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.)