Digits
Counting is a common problem in computer programming. I recently had a need for an algorithm to compute the number of base 10 digits of an integer (for example, there are 3 digits in the number 569). Here's what I worked out.
module type S=sig (**Count the number of base 10 digits of an integer*) val digits : int -> int (**Prepend with zeroes as necessary to get the 'right' number of columns *) val digits_10 : int -> int -> string end module A : S = struct let digits n = let rec loop acc n = let k = n / 10 in if k = 0 then acc else loop (acc + 1) k in loop 1 n let digits_10 i n = let l = digits n in let s = string_of_int (abs n) in let maybe_sign = if n < 0 then "-" else "" in if l >= i then (maybe_sign^s) else let lz=string_of_int 0 in let rec aux acc j = if j = 0 then acc else aux (lz^acc) (j - 1) in maybe_sign ^ aux s (i - l) end module Test_io = struct let test_0 () = let print_line i = Printf.printf "There are %d digits in %d\n" (A.digits i) i in let rec loop i n = if i = n then () else let x = int_of_float (10. ** (float_of_int i)) in print_line x; loop (i + 1) n in (*Range over 0 to to 10^19 *) loop 0 19 let test_1 () = let print_line i = Printf.printf "%s\n" i in let rec loop i n = if i = n then () else let x = A.digits_10 3 i in print_line x; loop (i + 1) n in (*Range over 0 to 100, 3 columns*)loop 0 100 let run () = test_0 (); test_1 () end let () = Test_io.run ()The program outputs
There are 1 digits in 1 There are 2 digits in 10 There are 3 digits in 100 There are 4 digits in 1000 There are 5 digits in 10000 There are 6 digits in 100000 There are 7 digits in 1000000 There are 8 digits in 10000000 There are 9 digits in 100000000 There are 10 digits in 1000000000 There are 11 digits in 10000000000 There are 12 digits in 100000000000 There are 13 digits in 1000000000000 There are 14 digits in 10000000000000 There are 15 digits in 100000000000000 There are 16 digits in 1000000000000000 There are 17 digits in 10000000000000000 There are 18 digits in 100000000000000000 There are 19 digits in 1000000000000000000 000 001 002 003 004 005 006 . . . 097 098 099Fun, fun, fun!