-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNumbers.java
136 lines (122 loc) · 2.81 KB
/
Numbers.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Recursion Assignment
* @author
*
* Remove the NotImplemented line from any methods you write
* DO NOT change the method signatures
* You MAY write helper classes
*/
public class Numbers {
/*
* #N0 Factorial (0pt)
* F(n ) = n * F(n-1) (if n > 0)
* F(0) = 1
*/
static int factorial(int num) {
if(num<=0) return 1;
return num * factorial(num-1);
}
/*
* #N1 Summation (1pt)
* S(n ) = n + S(n-1) (if n > 0)
* S(0) = 0
*/
static int summation(int n) {
throw new NotImplemented();
}
/*
* #N2 Fibonacci (1pt)
* F(0) = 0
* F(1) = 1
* F(n ) = F(n-1) + F(n-2) (if n > 0)
*/
static int fibonacci(int n) {
throw new NotImplemented();
}
/*
* #N3 Sum of a number's digits (1pt)
* S(n ) = n (if n < 10)
* S(n ) = S(n/10) + n mod 10 (if n>= 10)
*/
static int sumDigits(int n) {
throw new NotImplemented();
}
/*
* #N4 Product of a number's digits (1pt)
* S(n ) = n (if n < 10)
* S(n ) = S(n/10) * n mod 10 (if n>= 10)
*/
static int productDigits(int n) {
throw new NotImplemented();
}
/*
* #N5 Product of two whole numbers (1pt)
* P(a,b) = a + P(a,b-1) (if b > 0)
* P(a,0) = 0
*/
static int product(int a, int b) {
throw new NotImplemented();
}
/*
* #N6 Sum over a range of numbers (1pt)
* S(a,b) = a (if a = b)
* S(a,b) = b + S(a,b-1)
*/
static int sumRange(int a, int b) {
throw new NotImplemented();
}
/*
* #N7 Reverse a number's digits (2pt)
* R(n,v) = n + 10 * v (if n < 10)
* R(n,v) = R(n/10, 10*v + n mod 10))
* (v begins at 0)
*/
static int reverseDigits(int n) {
throw new NotImplemented();
}
/*
* #N8 Euclid's Algorithm for GCD (2pt)
* GCD(x,y) = y (if y <= x & x mod y=0)
* GCD(x,y) = GCD(y,x mod y)
*/
static int gcd(int x, int y) {
throw new NotImplemented();
}
/*
* #N9 Compound interest balance (2pt)
* B(p,r,t) = P (if t = 0)
* B(p,r,t) = (1+r)*B(p,r,t-1)
*/
static double compound(double p, double r, int t) {
throw new NotImplemented();
}
/*
* #N10 Newton's algorithm for square root (3pt)
* SR(n,p,e) = e (if | e^2 - n | < p)
* SR(n,p,e) = SR(n,p,(e+n/e)/2)
* (e begins at n)
*/
static double sqrtNewton(double n, double p) {
throw new NotImplemented();
}
/*
* #N11 Bisection method for square root (3pt)
* SR(n,p,h,l) = e (if | e^2 - n | <= p)
* SR(n,p,h,l) = SR(n,p,e,l) (if e^2 > n)
* SR(n,p,h,l) = SR(n,p,h,e)
* (where e=(h+l)/2, h begins at n, and l begins at 0)
*/
static double sqrtBisection(double n, double p) {
throw new NotImplemented();
}
/*
* #N12 Combinations (2pt)
* C(n,k) = n (if k = 1)
* C(n,k) = 0 (if k > n)
* C(n,k) = 1 (if k = n)
* C(n,k) = C(n-1,k) + C(n-1,k-1)
*/
static int combinations(int n, int k) {
throw new NotImplemented();
}
}