-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathFunctions.java
More file actions
117 lines (94 loc) · 2.72 KB
/
MathFunctions.java
File metadata and controls
117 lines (94 loc) · 2.72 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
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
import java.math.BigInteger;
public class MathFunctions {
public static void main(String[] args) {
}
public static boolean isPrime(int n) {
if (n < 2) return false;
int sqrt = (int) Math.sqrt(n);
for (int i = 2; i <= sqrt; i++) {
if (n % i == 0) return false;
}
return true;
}
public static void fibonacci(int n) {
java.math.BigInteger[] fib = new BigInteger[n + 1];
fib[0] = BigInteger.valueOf(0);
fib[1] = BigInteger.valueOf(1);
for (int i = 2; i <= n; i++) {
fib[i] = fib[i - 1].add(fib[i - 2]);
}
for(BigInteger value : fib) {
System.out.println(value);
}
}
//7.3
//check if two lines will intersect
public class Line {
double epsilon = 0.000001;
double slope;
double yInt;
public Line(double s, double y) {
slope = s;
yInt = y;
}
public boolean intersect(Line line2) {
return Math.abs(slope - line2.slope) > epsilon || Math.abs(yInt - line2.yInt) < epsilon;
}
}
//7.4
//Math operations only using addition
public static int subtract(int a, int b) {
return a + negate(b);
}
public static int negate(int x) {
int neg = 0;
int opposite = x < 0 ? 1 : -1;
while (x != 0) {
neg += opposite;
x += opposite;
}
return neg;
}
public static int multiply(int a, int b) {
if (a < b) return multiply(b, a); //faster if a > b
int sum = 0;
for (int i = absolute(b); i > 0; i--) {
sum += a;
}
if (b < 0) sum = negate(sum);
return sum;
}
public static int absolute(int x) {
if (x < 0) return negate(x);
else return x;
}
public int divide(int a, int b) throws java.lang.ArithmeticException {
if (b == 0) {
throw new java.lang.ArithmeticException("Divide by zero");
}
int absA = absolute(a);
int absB = absolute(b);
int product = 0;
int x = 0;
while (product + absB <= absA) {
product += absB;
x++;
}
if ((a < 0 && b < 0) || (a > 0 && b > 0)) return x;
else return negate(x);
}
public static java.util.List<Integer> primeFactors(int numbers) {
int n = numbers;
java.util.List<Integer> factors = new java.util.ArrayList<Integer>();
for (int i = 2; i <= n / i; i++) {
while (n % i == 0) {
factors.add(i);
n /= i;
}
}
if (n > 1) {
factors.add(n);
}
return factors;
}
}