Problema
Repeatedly divide n by 2, dropping any remainder (integer division), until it becomes 1. Count how many divisions that takes.
Implement halve_until_one(n).
Input
n: A positive integer.
Output
Return the number of divisions.
Vincoli
1 ≤ n ≤ 1,000,000,000,000
Esempi
Esempio 1
Input:
n = 20
Output atteso:
expected = 4
Input:
long long n = 20;
Output atteso:
long long expected = 4;
Input:
long n = 20;
Output atteso:
long expected = 4;
Spiegazione
20 → 10 → 5 → 2 → 1 takes 4 divisions.
Esempio 2
Input:
n = 1
Output atteso:
expected = 0
Input:
long long n = 1;
Output atteso:
long long expected = 0;
Input:
long n = 1;
Output atteso:
long expected = 0;
Spiegazione
n is already 1, so no divisions are needed.
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.