-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogFinder.java
50 lines (46 loc) · 1.01 KB
/
LogFinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.lang.Math;
public class LogFinder
{
// Compute factorial of a given integer.
public static double factorial(int N)
{
if (N==1) return 1;
return N * factorial(N-1);
}
// Approximate natural logarithm of given double.
public static double ln(double X)
{
int N = 0;
boolean found_log = false;
while (!found_log)
{
if (Math.exp(N) < X)
{
N += 1;
}
else found_log = true;
}
return N;
}
// Find the natural log of the factorial of user-provided integers.
public static void main(String[] args)
{
int input = Integer.parseInt(args[0]);
double output = ln(factorial(input));
StdOut.println(output - 1 + " <= ln(" + input + "!) <= " + output);
}
// Find integer above which a double is required to represent int!
public static void find_overflow_factorial(String[] args)
{
int N = 1;
boolean is_small = true;
while (is_small) {
double F = factorial(N);
if (F < 1000000000.0) {
StdOut.println(N + "! = " + F);
N += 1;
}
else is_small = false;
}
}
}