forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultadd.java
More file actions
44 lines (39 loc) · 1.26 KB
/
Copy pathMultadd.java
File metadata and controls
44 lines (39 loc) · 1.26 KB
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
public class Multadd {
public static double multadd(double a, double b, double c) {
return a * b + c;
}
public static void main(String[] args) {
System.out.println(multadd(2, 4, 3));
System.out.println(multadd(1, 2, 3));
System.out.println(multadd(0.5, Math.cos(Math.PI / 4), Math.sin(Math.PI / 4)));
System.out.println(multadd(1, Math.log(10), Math.log(20)));
System.out.println(oddSum(5));
}
public static double expSum(double x) {
double exp = Math.exp(-x);
return multadd(x, exp, Math.sqrt(1 - exp));
}
public static int oddSum(int n) {
if (n % 2 == 0 || n <= 0) {
System.err.println("Input to oddSum must be an odd integer");
return 0;
}
if (n == 1) {
return n;
} else {
return n + oddSum(n - 2);
}
}
public static int ack(int m, int n) {
if (m == 0 && n >= 0) {
return n + 1;
} else if (m > 0 && n == 0) {
return ack(m - 1, 1);
} else if (m > 0 && n > 0) {
return ack(m - 1, ack(m, n - 1));
} else {
System.err.println("Inputs to ack must be positive integers");
return 0;
}
}
}