← Back to challenges

Halve and Halve Again

PythonHardmathnumbers

Instructions

Given two integers a and b, return how many times a can be halved while still being greater than b.

Examples

halve_count(1324, 98) ➞ 3
# (1324 -> 662 -> 331 -> 165.5)

halve_count(624, 8) ➞ 6
# (624 -> 312 -> 156 -> 78 -> 39 -> 19.5 -> 9.75)

halve_count(1000, 3) ➞ 8
# (1000 -> 500 -> 250 -> 125 -> 62.5 -> 31.25 -> 15.625 -> 7.8125 -> 3.90625)

Notes

Integer a will always be able to be halved at least once in every test case.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Minimal VI: Ternary Operator