Commit 8b50a90e authored by Junpeng's avatar Junpeng
Browse files

unfinished ex0 and finished ex1

parent 9a50c3e1
# PT1_2021
:q# PT1_2021
Submission of Junpeng's exercise
\ No newline at end of file
Submission of Junpeng's exercise
.DS_Store
*.out
*~
*.a
*.aux
*.dat
*.log
*.o
*.out
*.synctex.gz
*.pyc
build/
install/
*.swp
File added
#include <iostream>
int recur_gcd(int a, int b){
if (b == 0){
return a;
}
else{
return recur_gcd(b, a % b);
}
}
int loop_gcd(int a, int b){
int t;
while (b != 0){
t = b;
b = a % b;
a = t;
}
return a;
}
int main(){
int a,b;
a = recur_gcd(18,12);
b = loop_gcd(18,12);
printf("%i \n %i\n", a,b);
return 0;
}
#include <iostream>
int recur_Fibo(int m){
if (m == 0){
return 0;
} else if (m == 1){
return 1;
} else{
return recur_Fibo(m-1) + recur_Fibo(m-2);
}
}
int loop_Fibo(int m){
int k = 0;
int a = 0; // F_{n-2}
int b = 1; // F_{n-1}
if (m == 0){
k = 0;
} else if (m == 1){
k = 1;
} else{
for (int i = 1; i < m; i++){
a = b;
b = a;
k = a + b;
}
}
return k;
}
int main(){
int a, b;
a = recur_Fibo(82);
b = loop_Fibo(82);
printf("%i \n %i \n", a, b);
return 0;
}
File added
#include <iostream>
float machine_precision(){
float epsilon = 1.0;
while (1.0 + 0.5*epsilon != 1.0) {
epsilon = 0.5 * epsilon;
}
return epsilon;
}
int main(){
float a;
a = machine_precision();
std::cout << a << std::endl;
return a;
}
#include <stdio.h>
#include <math.h>
#include <iostream>
int simpson_integrate(double a, double b){
int N = 200;
double x = a;
double delta = (b-a)/N;
double integ = 0;
for (int i = 0; i < N; i++){
integ += delta/6*(sin(x) + 4*sin(x+delta/2) + sin(x+delta));
x += delta;
}
return integ;
}
int main(){
double integ = simpson_integrate(0.0, M_PI);
printf("%f\n" ,integ);
return integ;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment