-1

How would I calculate logarithm in Java? How would I extend it to calculate for a sequence? I am trying to find solution for following:-

N different apps have different user growth rates. At a given time t, measured in days, the number of users using an app is g^t .

After how many full days will we have 1 billion total users across the N apps?

Example 1

Input: N = 1, growthRates = [1.5]

output = 52

Example 2

Input: N = 3, growthRates = [1.1, 1.2, 1.3]

output = 79

huytc
  • 1,074
  • 7
  • 20
mobileDev
  • 1,358
  • 2
  • 14
  • 34
  • 1
    Have you considered `Math.log()` and friends? Hard to see how this is a duplicate of [this](https://stackoverflow.com/questions/13831150/logarithm-algorithm). – user207421 Nov 09 '20 at 03:24
  • Does this answer your question? [Logarithm Algorithm](https://stackoverflow.com/questions/13831150/logarithm-algorithm) – pradeexsu Nov 09 '20 at 03:52

1 Answers1

2

Logarithm in Java

  1. To calculate a common logarithm in Java we can simply use the Math.log10() method:
System.out.println(Math.log10(100));
  1. To calculate a natural logarithm in Java we use the Math.log() method:
System.out.println(Math.log(10));
  1. To calculate a logarithm with custom base in Java (logb(n) = loge(n) / loge), we can do it by defining a method as below:
private static double customLog(double base, double logNumber) {
    return Math.log(logNumber) / Math.log(base);
}

a details about mathematic and Logarithm can be found here

Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36