-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
152 lines (129 loc) · 3.26 KB
/
Copy pathfunctions.cpp
File metadata and controls
152 lines (129 loc) · 3.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <cmath>
using namespace std;
//Evaluate the polynomial at the point indicated by using Horner’s algorithm.
//Ex: p(x) = x^5 − 3x^3 − 4x^2 + 10 at x = 2
vector<int> Hornor(vector<int> p, int x){
vector<int> res;
int temp = p[0];
res.push_back(p[0]);
for(int i = 1; i < p.size(); ++i){
temp = x * temp + p[i];
res.push_back(temp);
}
return res;
}
//correctly to specific decimal places (rounded) for numerator and denominator respectively.
double Tay_natural(double x, int digits){
int factorial = 1;
double errorTerm = 1;
int i = 1;
for(;; ++i){
factorial *= i;
errorTerm = pow(x, i) / factorial;
if(abs(errorTerm) < (pow(10, -1 * digits) / 2)) break;
}
double actualTerm = 1;
factorial = 1;
for(int j = 1; j < i + 1; ++j){
factorial *= j;
actualTerm += pow(x, j) / factorial;
}
return actualTerm;
}
double Tay_cos(double x, int digits){
int factorial = 1;
double errorTerm = 1;
int i = 1;
for(;; i = i + 2){
factorial = factorial * i * (i + 1);
errorTerm = pow(x, 2 * i) / factorial;
if(abs(errorTerm) < (pow(10, -1 * digits) / 2)) break;
}
double actualTerm = 1;
factorial = 1;
int sign = 1;
for(int j = 1; j < (i + 1) / 2; j = j + 2){
factorial = factorial * j * (j + 1);
if(sign % 2 == 1) {
actualTerm -= pow(x, j + 1) / factorial;
} else {
actualTerm += pow(x, j + 1) / factorial;
}
++sign;
}
return actualTerm;
}
double Tay_sin(double x, int digits){
int factorial = 1;
double errorTerm = x;
int i = 1;
for(;; i = i + 2){
factorial = factorial * (i + 1) * (i + 2);
errorTerm = pow(x, 2 * i + 1) / factorial;
if(abs(errorTerm) < (pow(10, -1 * digits) / 2)) break;
}
double actualTerm = x;
factorial = 1;
int sign = 1;
for(int j = 1; j < (i + 1) / 2; j = j + 2){
factorial = factorial * (j + 1) * (j + 2);
if(sign % 2 == 1) {
actualTerm -= pow(x, j + 2) / factorial;
} else {
actualTerm += pow(x, j + 2) / factorial;
}
++sign;
}
return actualTerm;
}
//Greatest common divisor
int gcd(int a, int b){
if (b == 0) return a;
if (b > a){
int temp = a;
a = b;
b = temp;
}
return gcd(b, a % b);
}
//find x and y, s.t. ex + phiy = 1;
void EuclideanAlgo(int e, int phi){
if (gcd(e,phi) != 1) {
cout << "They are not coprime." << endl;
return;
}
int x = 1;
int y = 1;
while(e * x - phi * y != 1){
if( e * x - phi * y > 0) {
++y;
} else {
++x;
}
}
cout << "x = " << x << ", y = " << y;
return;
}
//find (base^power) (mod num)
int expMod(int base, int power, int mod){
int res = 1;
base = base % mod;
if (base == 0) return 0;
while (power > 0){
if (power % 2 == 1) res = (res * base) % mod;
power = power / 2;
base = (base * base) % mod;
}
return res;
}
int twoMod(int a, int b, int mod){
return (a * b) % mod;
}
int main(){
return 0;
}