inspired by Diego Hdez' Python solution, but with a for-loop instead of a recursion and in C#
public static int Squaresneeded(ulong grains) { int tile = 0; for(;grains > 0;++tile) grains = grains >> 1; return tile; }
My original solution shifted the other way round building a potential and comparing that with the number of grains.
public static int Squaresneeded(ulong grains) { int tile = 1; ulong potential = 1; for (;potential < grains;++tile) { potential = (potential << 1) + 1; } return tile; }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
inspired by Diego Hdez' Python solution, but with a for-loop instead of a recursion and in C#
My original solution shifted the other way round building a potential and comparing that with the number of grains.