BAEL-1076 How to get a number of digits in an int? (#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
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
package com.baeldung.numberofdigits;
|
||||
|
||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;;
|
||||
|
||||
public class Benchmarking {;
|
||||
|
||||
private static final int LOWER_BOUND = 1;
|
||||
private static final int UPPER_BOUND = 999999999;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
LOG.info("Testing all methods...");
|
||||
|
||||
long length = test_stringBasedSolution();
|
||||
LOG.info("String Based Solution : " + length);
|
||||
|
||||
length = test_logarithmicApproach();
|
||||
LOG.info("Logarithmic Approach : " + length);
|
||||
|
||||
length = test_repeatedMultiplication();
|
||||
LOG.info("Repeated Multiplication : " + length);
|
||||
|
||||
length = test_shiftOperators();
|
||||
LOG.info("Shift Operators : " + length);
|
||||
|
||||
length = test_dividingWithPowersOf2();
|
||||
LOG.info("Dividing with Powers of 2 : " + length);
|
||||
|
||||
length = test_divideAndConquer();
|
||||
LOG.info("Divide And Conquer : " + length);
|
||||
|
||||
}
|
||||
|
||||
private static long test_stringBasedSolution() {
|
||||
|
||||
long startTime, stopTime, elapsedTime;
|
||||
startTime = System.currentTimeMillis();
|
||||
|
||||
int total = 0;
|
||||
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
|
||||
total += NumberOfDigits.stringBasedSolution(i);
|
||||
}
|
||||
|
||||
stopTime = System.currentTimeMillis();
|
||||
elapsedTime = stopTime - startTime;
|
||||
|
||||
return elapsedTime;
|
||||
}
|
||||
|
||||
private static long test_logarithmicApproach() {
|
||||
|
||||
long startTime, stopTime, elapsedTime;
|
||||
startTime = System.currentTimeMillis();
|
||||
|
||||
int total = 0;
|
||||
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
|
||||
total += NumberOfDigits.logarithmicApproach(i);
|
||||
}
|
||||
|
||||
stopTime = System.currentTimeMillis();
|
||||
elapsedTime = stopTime - startTime;
|
||||
|
||||
return elapsedTime;
|
||||
}
|
||||
|
||||
private static long test_repeatedMultiplication() {
|
||||
|
||||
long startTime, stopTime, elapsedTime;
|
||||
startTime = System.currentTimeMillis();
|
||||
|
||||
int total = 0;
|
||||
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
|
||||
total += NumberOfDigits.repeatedMultiplication(i);
|
||||
}
|
||||
|
||||
stopTime = System.currentTimeMillis();
|
||||
elapsedTime = stopTime - startTime;
|
||||
|
||||
return elapsedTime;
|
||||
}
|
||||
|
||||
private static long test_shiftOperators() {
|
||||
|
||||
long startTime, stopTime, elapsedTime;
|
||||
startTime = System.currentTimeMillis();
|
||||
|
||||
int total = 0;
|
||||
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
|
||||
total += NumberOfDigits.shiftOperators(i);
|
||||
}
|
||||
|
||||
stopTime = System.currentTimeMillis();
|
||||
elapsedTime = stopTime - startTime;
|
||||
|
||||
return elapsedTime;
|
||||
}
|
||||
|
||||
private static long test_dividingWithPowersOf2() {
|
||||
|
||||
long startTime, stopTime, elapsedTime;
|
||||
startTime = System.currentTimeMillis();
|
||||
|
||||
int total = 0;
|
||||
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
|
||||
total += NumberOfDigits.dividingWithPowersOf2(i);
|
||||
}
|
||||
|
||||
stopTime = System.currentTimeMillis();
|
||||
elapsedTime = stopTime - startTime;
|
||||
|
||||
return elapsedTime;
|
||||
}
|
||||
|
||||
private static long test_divideAndConquer() {
|
||||
|
||||
long startTime, stopTime, elapsedTime;
|
||||
startTime = System.currentTimeMillis();
|
||||
|
||||
int total = 0;
|
||||
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
|
||||
total += NumberOfDigits.divideAndConquer(i);
|
||||
}
|
||||
|
||||
stopTime = System.currentTimeMillis();
|
||||
elapsedTime = stopTime - startTime;
|
||||
|
||||
return elapsedTime;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package com.baeldung.numberofdigits;
|
||||
|
||||
public class NumberOfDigits {
|
||||
public static int stringBasedSolution(int number) {
|
||||
int length = String.valueOf(number).length();
|
||||
return length;
|
||||
}
|
||||
|
||||
public static int logarithmicApproach(int number) {
|
||||
int length = (int) Math.log10(number) + 1;
|
||||
return length;
|
||||
}
|
||||
|
||||
public static int repeatedMultiplication(int number) {
|
||||
int length = 0;
|
||||
long temp = 1;
|
||||
while(temp <= number) {
|
||||
length++;
|
||||
temp *= 10;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
public static int shiftOperators(int number) {
|
||||
int length = 0;
|
||||
long temp = 1;
|
||||
while(temp <= number) {
|
||||
length++;
|
||||
temp = (temp << 3) + (temp << 1);
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
public static int dividingWithPowersOf2(int number) {
|
||||
int length = 1;
|
||||
if (number >= 100000000) {
|
||||
length += 8;
|
||||
number /= 100000000;
|
||||
}
|
||||
if (number >= 10000) {
|
||||
length += 4;
|
||||
number /= 10000;
|
||||
}
|
||||
if (number >= 100) {
|
||||
length += 2;
|
||||
number /= 100;
|
||||
}
|
||||
if (number >= 10) {
|
||||
length += 1;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
public static int divideAndConquer(int number) {
|
||||
if (number < 100000){
|
||||
// 5 digits or less
|
||||
if (number < 100){
|
||||
// 1 or 2
|
||||
if (number < 10)
|
||||
return 1;
|
||||
else
|
||||
return 2;
|
||||
}else{
|
||||
// 3 to 5 digits
|
||||
if (number < 1000)
|
||||
return 3;
|
||||
else{
|
||||
// 4 or 5 digits
|
||||
if (number < 10000)
|
||||
return 4;
|
||||
else
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 6 digits or more
|
||||
if (number < 10000000) {
|
||||
// 6 or 7 digits
|
||||
if (number < 1000000)
|
||||
return 6;
|
||||
else
|
||||
return 7;
|
||||
} else {
|
||||
// 8 to 10 digits
|
||||
if (number < 100000000)
|
||||
return 8;
|
||||
else {
|
||||
// 9 or 10 digits
|
||||
if (number < 1000000000)
|
||||
return 9;
|
||||
else
|
||||
return 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.numberofdigits;
|
||||
|
||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
||||
|
||||
public class NumberOfDigitsDriver {
|
||||
public static void main(String[] args) {
|
||||
LOG.info("Testing all methods...");
|
||||
|
||||
long length = NumberOfDigits.stringBasedSolution(602);
|
||||
LOG.info("String Based Solution : " + length);
|
||||
|
||||
length = NumberOfDigits.logarithmicApproach(602);
|
||||
LOG.info("Logarithmic Approach : " + length);
|
||||
|
||||
length = NumberOfDigits.repeatedMultiplication(602);
|
||||
LOG.info("Repeated Multiplication : " + length);
|
||||
|
||||
length = NumberOfDigits.shiftOperators(602);
|
||||
LOG.info("Shift Operators : " + length);
|
||||
|
||||
length = NumberOfDigits.dividingWithPowersOf2(602);
|
||||
LOG.info("Dividing with Powers of 2 : " + length);
|
||||
|
||||
length = NumberOfDigits.divideAndConquer(602);
|
||||
LOG.info("Divide And Conquer : " + length);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user