Skip to content

Commit 09d10ac

Browse files
ramansahasimaibin
authored andcommitted
BAEL-1076 How to get a number of digits in an int? (eugenp#2457)
* BAEL-1076 How to get a number of digits in an int? Six different ways to find the number of digits in an integer. * BAEL-1076 How to get a number of digits in an int? * Update NumberOfDigits.java
1 parent d492c6f commit 09d10ac

4 files changed

Lines changed: 355 additions & 0 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.baeldung.numberofdigits;
2+
3+
import static com.baeldung.designpatterns.util.LogerUtil.LOG;;
4+
5+
public class Benchmarking {;
6+
7+
private static final int LOWER_BOUND = 1;
8+
private static final int UPPER_BOUND = 999999999;
9+
10+
11+
public static void main(String[] args) {
12+
LOG.info("Testing all methods...");
13+
14+
long length = test_stringBasedSolution();
15+
LOG.info("String Based Solution : " + length);
16+
17+
length = test_logarithmicApproach();
18+
LOG.info("Logarithmic Approach : " + length);
19+
20+
length = test_repeatedMultiplication();
21+
LOG.info("Repeated Multiplication : " + length);
22+
23+
length = test_shiftOperators();
24+
LOG.info("Shift Operators : " + length);
25+
26+
length = test_dividingWithPowersOf2();
27+
LOG.info("Dividing with Powers of 2 : " + length);
28+
29+
length = test_divideAndConquer();
30+
LOG.info("Divide And Conquer : " + length);
31+
32+
}
33+
34+
private static long test_stringBasedSolution() {
35+
36+
long startTime, stopTime, elapsedTime;
37+
startTime = System.currentTimeMillis();
38+
39+
int total = 0;
40+
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
41+
total += NumberOfDigits.stringBasedSolution(i);
42+
}
43+
44+
stopTime = System.currentTimeMillis();
45+
elapsedTime = stopTime - startTime;
46+
47+
return elapsedTime;
48+
}
49+
50+
private static long test_logarithmicApproach() {
51+
52+
long startTime, stopTime, elapsedTime;
53+
startTime = System.currentTimeMillis();
54+
55+
int total = 0;
56+
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
57+
total += NumberOfDigits.logarithmicApproach(i);
58+
}
59+
60+
stopTime = System.currentTimeMillis();
61+
elapsedTime = stopTime - startTime;
62+
63+
return elapsedTime;
64+
}
65+
66+
private static long test_repeatedMultiplication() {
67+
68+
long startTime, stopTime, elapsedTime;
69+
startTime = System.currentTimeMillis();
70+
71+
int total = 0;
72+
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
73+
total += NumberOfDigits.repeatedMultiplication(i);
74+
}
75+
76+
stopTime = System.currentTimeMillis();
77+
elapsedTime = stopTime - startTime;
78+
79+
return elapsedTime;
80+
}
81+
82+
private static long test_shiftOperators() {
83+
84+
long startTime, stopTime, elapsedTime;
85+
startTime = System.currentTimeMillis();
86+
87+
int total = 0;
88+
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
89+
total += NumberOfDigits.shiftOperators(i);
90+
}
91+
92+
stopTime = System.currentTimeMillis();
93+
elapsedTime = stopTime - startTime;
94+
95+
return elapsedTime;
96+
}
97+
98+
private static long test_dividingWithPowersOf2() {
99+
100+
long startTime, stopTime, elapsedTime;
101+
startTime = System.currentTimeMillis();
102+
103+
int total = 0;
104+
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
105+
total += NumberOfDigits.dividingWithPowersOf2(i);
106+
}
107+
108+
stopTime = System.currentTimeMillis();
109+
elapsedTime = stopTime - startTime;
110+
111+
return elapsedTime;
112+
}
113+
114+
private static long test_divideAndConquer() {
115+
116+
long startTime, stopTime, elapsedTime;
117+
startTime = System.currentTimeMillis();
118+
119+
int total = 0;
120+
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
121+
total += NumberOfDigits.divideAndConquer(i);
122+
}
123+
124+
stopTime = System.currentTimeMillis();
125+
elapsedTime = stopTime - startTime;
126+
127+
return elapsedTime;
128+
}
129+
130+
131+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.baeldung.numberofdigits;
2+
3+
public class NumberOfDigits {
4+
public static int stringBasedSolution(int number) {
5+
int length = String.valueOf(number).length();
6+
return length;
7+
}
8+
9+
public static int logarithmicApproach(int number) {
10+
int length = (int) Math.log10(number) + 1;
11+
return length;
12+
}
13+
14+
public static int repeatedMultiplication(int number) {
15+
int length = 0;
16+
long temp = 1;
17+
while(temp <= number) {
18+
length++;
19+
temp *= 10;
20+
}
21+
return length;
22+
}
23+
24+
public static int shiftOperators(int number) {
25+
int length = 0;
26+
long temp = 1;
27+
while(temp <= number) {
28+
length++;
29+
temp = (temp << 3) + (temp << 1);
30+
}
31+
return length;
32+
}
33+
34+
public static int dividingWithPowersOf2(int number) {
35+
int length = 1;
36+
if (number >= 100000000) {
37+
length += 8;
38+
number /= 100000000;
39+
}
40+
if (number >= 10000) {
41+
length += 4;
42+
number /= 10000;
43+
}
44+
if (number >= 100) {
45+
length += 2;
46+
number /= 100;
47+
}
48+
if (number >= 10) {
49+
length += 1;
50+
}
51+
return length;
52+
}
53+
54+
public static int divideAndConquer(int number) {
55+
if (number < 100000){
56+
// 5 digits or less
57+
if (number < 100){
58+
// 1 or 2
59+
if (number < 10)
60+
return 1;
61+
else
62+
return 2;
63+
}else{
64+
// 3 to 5 digits
65+
if (number < 1000)
66+
return 3;
67+
else{
68+
// 4 or 5 digits
69+
if (number < 10000)
70+
return 4;
71+
else
72+
return 5;
73+
}
74+
}
75+
} else {
76+
// 6 digits or more
77+
if (number < 10000000) {
78+
// 6 or 7 digits
79+
if (number < 1000000)
80+
return 6;
81+
else
82+
return 7;
83+
} else {
84+
// 8 to 10 digits
85+
if (number < 100000000)
86+
return 8;
87+
else {
88+
// 9 or 10 digits
89+
if (number < 1000000000)
90+
return 9;
91+
else
92+
return 10;
93+
}
94+
}
95+
}
96+
}
97+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.numberofdigits;
2+
3+
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
4+
5+
public class NumberOfDigitsDriver {
6+
public static void main(String[] args) {
7+
LOG.info("Testing all methods...");
8+
9+
long length = NumberOfDigits.stringBasedSolution(602);
10+
LOG.info("String Based Solution : " + length);
11+
12+
length = NumberOfDigits.logarithmicApproach(602);
13+
LOG.info("Logarithmic Approach : " + length);
14+
15+
length = NumberOfDigits.repeatedMultiplication(602);
16+
LOG.info("Repeated Multiplication : " + length);
17+
18+
length = NumberOfDigits.shiftOperators(602);
19+
LOG.info("Shift Operators : " + length);
20+
21+
length = NumberOfDigits.dividingWithPowersOf2(602);
22+
LOG.info("Dividing with Powers of 2 : " + length);
23+
24+
length = NumberOfDigits.divideAndConquer(602);
25+
LOG.info("Divide And Conquer : " + length);
26+
}
27+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.baeldung.numberofdigits;
2+
3+
import org.junit.Assert;
4+
import org.junit.Assume;
5+
import org.junit.experimental.theories.DataPoints;
6+
import org.junit.experimental.theories.Theories;
7+
import org.junit.experimental.theories.Theory;
8+
import org.junit.runner.RunWith;
9+
10+
@RunWith(Theories.class)
11+
public class NumberOfDigitsIntegrationTest {
12+
13+
@DataPoints
14+
public static int[][] lowestIntegers()
15+
{
16+
return new int[][]{
17+
{1, 1},
18+
{2, 10},
19+
{3, 100},
20+
{4, 1000},
21+
{5, 10000},
22+
{6, 100000},
23+
{7, 1000000},
24+
{8, 10000000},
25+
{9, 100000000},
26+
{10, 1000000000}
27+
};
28+
}
29+
30+
@DataPoints
31+
public static int[][] highestIntegers()
32+
{
33+
return new int[][]{
34+
{1, 9},
35+
{2, 99},
36+
{3, 999},
37+
{4, 9999},
38+
{5, 99999},
39+
{6, 999999},
40+
{7, 9999999},
41+
{8, 99999999},
42+
{9, 999999999},
43+
{10, Integer.MAX_VALUE}
44+
};
45+
}
46+
47+
@DataPoints
48+
public static int[][] randomIntegers()
49+
{
50+
return new int[][]{
51+
{1, 1},
52+
{2, 14},
53+
{3, 549},
54+
{4, 1136},
55+
{5, 25340},
56+
{6, 134321},
57+
{7, 1435432},
58+
{8, 54234129},
59+
{9, 113683912},
60+
{10, 1534031982}
61+
};
62+
}
63+
64+
@Theory
65+
public void givenDataPoints_whenStringBasedSolutionInvoked_thenAllPointsMatch(final int[] entry) {
66+
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
67+
Assert.assertEquals(entry[0], NumberOfDigits.stringBasedSolution(entry[1]));
68+
}
69+
70+
@Theory
71+
public void givenDataPoints_whenLogarithmicApproachInvoked_thenAllPointsMatch(final int[] entry) {
72+
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
73+
Assert.assertEquals(entry[0], NumberOfDigits.logarithmicApproach(entry[1]));
74+
}
75+
76+
@Theory
77+
public void givenDataPoints_whenRepeatedMultiplicationInvoked_thenAllPointsMatch(final int[] entry) {
78+
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
79+
Assert.assertEquals(entry[0], NumberOfDigits.repeatedMultiplication(entry[1]));
80+
}
81+
82+
@Theory
83+
public void givenDataPoints_whenShiftOperatorsInvoked_thenAllPointsMatch(final int[] entry) {
84+
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
85+
Assert.assertEquals(entry[0], NumberOfDigits.shiftOperators(entry[1]));
86+
}
87+
88+
@Theory
89+
public void givenDataPoints_whenDividingWithPowersOf2Invoked_thenAllPointsMatch(final int[] entry) {
90+
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
91+
Assert.assertEquals(entry[0], NumberOfDigits.dividingWithPowersOf2(entry[1]));
92+
}
93+
94+
@Theory
95+
public void givenDataPoints_whenDivideAndConquerInvoked_thenAllPointsMatch(final int[] entry) {
96+
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
97+
Assert.assertEquals(entry[0], NumberOfDigits.divideAndConquer(entry[1]));
98+
}
99+
100+
}

0 commit comments

Comments
 (0)