view algs4-c++/1.1.14/lg.cpp @ 0:17ee997bf124

Initial commit
author Eris Caffee <discordia@eldalin.com>
date Sat, 16 May 2015 12:02:55 -0500
parents
children
line source
1 // Write a static method lg() that takes an int value N as argument and returns the largest int not larger than the base-2 logarithm of N. Do not use Math.
3 // g++ lg.cpp -o lg
5 #include <cmath>
6 #include <climits>
7 #include <iostream>
9 int main( int argc, char **argv ) {
11 while ( ! std::cin.eof() ) {
12 int i;
13 std::cin >> i;
14 int lg = 0;
15 while ( (1 << lg) <= i ) {
16 lg++;
17 }
18 lg--;
19 std::cout << "lg(" << i << ") >= " << lg << " actual value is " << log2(i) << std::endl;
20 }
21 }