Count the Digit in Java
The challenge
Take an integer n (n >= 0)
and a digit d (0 <= d <= 9)
as an integer. Square all numbers k (0 <= k <= n)
between 0 and n. Count the numbers of digits d
used in the writing of all the k**2
. Call nb_dig
(or nbDig or …) the function taking n
and d
as parameters and returning this count.
Examples:
|
|
Note that 121
has twice the digit 1
.
The solution in Java code
Option 1:
|
|
Option 2:
|
|
Option 3:
|
|
Test cases to validate our solution
|
|