7 kyu

The wheat/rice and chessboard problem

2,153 of 9,317GiacomoSorbi

Description:

I assume most of you are familiar with the ancient legend of the rice (but I see wikipedia suggests wheat, for some reason) problem, but a quick recap for you: a young man asks as a compensation only 1 grain of rice for the first square, 2 grains for the second, 4 for the third, 8 for the fourth and so on, always doubling the previous.

Your task is pretty straightforward (but not necessarily easy): given an amount of grains, you need to return up to which square of the chessboard one should count in order to get at least as many.

As usual, a few examples might be way better than thousands of words from me:

0 grains need 0 cells
1 grain needs 1 cell
2 grains need 2 cells
3 grains need 2 cells
4 grains need 3 cells
and etc.

Input is always going to be valid/reasonable: ie: a non negative number; extra cookie for not using a loop to compute square-by-square (at least not directly) and instead trying a smarter approach [hint: some peculiar operator]; a trick converting the number might also work: impress me!

Mathematics
Recursion
Puzzles

Stats:

CreatedMay 29, 2018
PublishedMay 29, 2018
Warriors Trained15703
Total Skips223
Total Code Submissions28104
Total Times Completed9317
JavaScript Completions5657
C++ Completions611
Ruby Completions147
Python Completions2153
Crystal Completions18
Haskell Completions120
C# Completions383
C Completions208
Prolog Completions20
Groovy Completions27
TypeScript Completions149
Julia Completions29
COBOL Completions8
PHP Completions74
Dart Completions14
Lua Completions12
Java Completions111
Total Stars178
% of votes with a positive feedback rating89% of 1070
Total "Very Satisfied" Votes880
Total "Somewhat Satisfied" Votes139
Total "Not Satisfied" Votes51
Total Rank Assessments6
Average Assessed Rank
7 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • GiacomoSorbi Avatar
  • SagePtr Avatar
  • suic Avatar
  • docgunthrop Avatar
  • B1ts Avatar
  • rowcased Avatar
  • KataSideKick Avatar
  • monadius Avatar
  • hobovsky Avatar
  • cliffstamp Avatar
  • stellartux Avatar
  • user8436785 Avatar
  • ejini战神 Avatar
  • akar-0 Avatar
  • QKiryu Avatar
  • metatable Avatar
Ad