Calculate logarithm in Java [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
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

Logarithm in Java
To calculate a common logarithm in Java we can simply use the Math.log10() method:
System.out.println(Math.log10(100));
To calculate a natural logarithm in Java we use the Math.log() method:
System.out.println(Math.log(10));
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

Related

Method Math.random - how it work? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Some teacher today teach us how work Math.random in Java language. I don't understand what he told. Can some explain? I have some code:
import java.util.Random;
public class test {
public static void main(String[] args) {
Random random = new Random();
double r = Math.random() * 4.4 + 1.2;
System.out.println(r);
}
}
Next he ask us what is interval of this random. We don't know and he write:
<1.2, 5.6> --> <0, 4.4> + 1.2 --> double r = Math.random() * 4.4 + 1.2;
How can I calculate this, what is mathematical formula? <1.2, 5.6> --> <0, 4.4> + 1.2
Math.random() gives a random number between 0 and 1. Actually it's a pseudo random but that's a different story. So in order to have a random number for example between 0 and 100 you need to multiply Math.random() by 100.
That makes you range from 0..1 to 0..100 because 0x100=0 and 1x100=100.
By adding a number to the result you set the lower boundary of the range. For example if you want a number between 100 and 200 you can do:
Math.random()*100 + 100.
So in your case he multiplies it by 4.4 giving it a range 0->4.4 and then adds 1.2 giving it a lower boundary of 1.2 which makes the actual range 1.2->5.6

How Convert packages to units [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a question, I'm programming in Java if I have a decimal "3.02" (this is equivalent to 3 packages (1 package = 10 units) and 2 units) and want to convert to "32" (this is equivalent to 32 units ). Does anyone know how to do it? There is a feature that allows java?
If 3 represent the units and the decimal part is 02 then your math is not coherent as it must, since you need 10 units to increment 1 package, I will expected that 3.2 represents 3 packages and 2 units
anyways a code snipped that can solve your math can be:
double totalToConvert = 3.02;
int totalAsInteger = (int) totalToConvert;
System.out.println(totalAsInteger);
int decimalPart = (int)((double)(totalToConvert-totalAsInteger)*100);
int total = totalAsInteger*10 +decimalPart;
System.out.println(total
);

Take a result of two long numbers using an array in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to take a sum, quotient, remainder of two numbers using an array in java.
123456789012345+7654321, 123456789012345/7654321. What is a simplest way to calculate it using Java?(I am new to Java.)
Since you are new to java I recommend reading up on some tutorials. As it seems you are not familiar with java in general. An example, which I have not used myself, is http://www.javaworld.com/blog/java-101. It may be worth your time to read this over.
As for your actual question, you would create a variable in java. Then assign your first number to this variable. After doing this, you can perform some operations on the number.
An example in sudo code to give you an idea while not doing the work for you.
void method
var number = 100
number = number + 200
number = number / 20
print("result" . number)
If you plan to use an array its the same process in a loop.
http://www.tutorialspoint.com/java/java_loop_control.htm

Modular Arithmetic in programming [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can anyone explain me how modular arithmetic works in programming? I know it is used to operate on large values.
For example, to calculate the binomial coefficient of B(1000000, 2) using int data-type. i assume we couldn't multiply using int data-type, since it involves calculating factorials of big values like 1000000! which has millions of digits, which don't fit in an 32-bit or 64-bit integer.
I know modular arithmetic is used to these type of problems, But i don't understand exactly how that works.
The modulo operation is a simple operation that calculates the remainder of a division.
For instance
5 % 3 = 2 as dividing 5 by 3 will give you a a remainder of .
A common usecase for this is checking whether a number is even or odd.
number % 2 == 0 means the number is even.
For more information please check Wikipedia.

Find the reverse of a 4 digit integer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have to solve the following problem:
-find flowchart or algorithm and show it in java languange
"Write a Java program that prompts the user to input a four-digit positive integers between 1001 and 9999. The program then finds the reverse of that integers. For example, if the input integer i2 3245, its reverse is 5423."
My problem is that I don't know what formula to use. I have also asked my demonstrator, but he just said that the formula uses a percentage and divide. How should I approach a solution to this problem?
Since this is a learning assignment, I will give you only hints:
To get the last digit in base N, use x % N; for base ten, that would be x % 10
To drop the last digit in base N, integer-divide by N; for base ten, that would be x /= 10
Repeating this process four times and printing the digits in reverse order will give you the desired result. Since you know that the value has exactly four digits, you do not need a loop.
This might not be what the teacher accepts/wants/expects, but for educational purposes, this would be the easiest way to do it:
String input = "1234";
int result = Integer.parseInt(new StringBuilder(input).reverse().toString());
System.out.println(result):
prints
4321

Categories

Resources