<- function(n){
fact1 if(n<0) return(NA)
<- 1
result while(n > 0){
<- result * n
result <- n - 1
n
}
result
}fact1(5)
[1] 120
Factorial:
Using While loop
<- function(n){
fact1 if(n<0) return(NA)
<- 1
result while(n > 0){
<- result * n
result <- n - 1
n
}
result
}fact1(5)
[1] 120
Using repeat
<- function(n){
fact2 if(n<0) return(NA)
<- 1
result repeat{
if(n < 2) break
<- result * n
result <- n - 1
n
}
result
}fact2(6)
[1] 720
Using For-loop
<- function(n){
fact3 if(n<0) return(NA)
<- 1
result for(i in seq_len(n)){
<- result * i
result
}
result
}fact3(4)
[1] 24
Displaying the Multiplication Table
{for(i in seq(9)) cat("\t", i)
cat("\n")
for(i in seq_len(9)){
cat(i)
for(j in seq_len(9)){
cat("\t", i*j)
}cat("\n")
} }
1 2 3 4 5 6 7 8 9
1 1 2 3 4 5 6 7 8 9
2 2 4 6 8 10 12 14 16 18
3 3 6 9 12 15 18 21 24 27
4 4 8 12 16 20 24 28 32 36
5 5 10 15 20 25 30 35 40 45
6 6 12 18 24 30 36 42 48 54
7 7 14 21 28 35 42 49 56 63
8 8 16 24 32 40 48 56 64 72
9 9 18 27 36 45 54 63 72 81
grade(x)
<- function(x){
grade if(x >= 90) 'A'
else if(x >= 80) 'B'
else if(x >= 70) 'C'
else if(x >= 60) 'D'
else if(x >= 50) 'E'
else 'F'
}grade(93)
[1] "A"
grades(x)
<- function(x){
grades for(i in x) cat(grade(i), " ")
}grades(c(73, 92, 80, 49))
C A B F
\(\pi\) estimation:
<- 0
result for(i in 0:200000){
<- result + 4*(-1)^i/(i*2+1)
result
}sprintf("%.30f", result)
[1] "3.141597653564761838396179882693"
sprintf("%.30f", pi)
[1] "3.141592653589793115997963468544"
\(\pi\) estimation:
<- 2*sqrt(2)/9801
const <- 0
res for(k in 0:2){
<- res + factorial(4*k)*(1103 + 26390*k)/factorial(k)^4/396^(4*k)
res
}1/const/res
[1] 3.141593
sprintf("%.30f", pi)
[1] "3.141592653589793115997963468544"
sprintf("%.30f", 1/const/res)
[1] "3.141592653589792671908753618482"
sqrt_avg
<- function(x){
sqrt_avg = 0
begin = x
end repeat{
<- (begin + end)/2
mid <- x - mid * mid
err if(abs(err) < 1e-8) break
if(err > 0) begin <- mid
else end <- mid
}
mid
}sqrt_avg(2)
[1] 1.414214
sqrt_heron
<- function(x){
sqrt_heron <- 1
x0 repeat{
<- (x0 + x/x0)/2
x_new if(abs(x_new - x0) < 1e-8) break
<- x_new
x0
}
x0
}sqrt_heron(2)
[1] 1.414214
Fibonacci:
<- function(x){
fib1 = 0
a = 1
b while(x>1){
<- a
temp <- b
a <- b + temp
b <- x - 1
x
}
b
}fib1(10)
[1] 55
GCD
<- function(a, b){
gcd if(b == 0) a
else gcd(b, a%%b)
}
LCM
<- function(a, b){
lcm * b /gcd(a, b)
a }
Taylor Series:
exp(x) : Will correctly approximate values from\(-11< x < 55\). Notice that for numbers outside this interval, we need more iterations.
<- function(x){
my_exp <- fact <- i <- 1 #taken the first term as result
result while(i <= 100){
<- result + x^i/fact
result <- i + 1
i <- fact * i
fact
}
result }
log(x) : Used the first 50 terms. Only approximates \(0.2\leq x\leq2\) . The error is huge for numbers outside this interval
<- function(x){
my_log_restricted if(x>2 | x<0.2) return(NA)
<- 1
i <- 0
result while(i <= 100){
<- result + (-1)^(i-1) * (x-1)^i/i
result <- i + 1
i
}
result }
sin(x) : \(-\infty <x<\infty\)
<- function(x){
my_sin <- x %% (2*pi)
x <- i <- j <- 1
fact <- 0
result while(i <= 50){
<- result + (-1)^(i-1)*x^(2*i-1)/fact
result <- i + 1
i <- fact * (2*i - 1 - 1) * (2*i - 1)
fact
}
result }
Significant Digits:
<- function(x, base){
signif_digits <- abs(x)
x if(round(x,6) >= 1 & x < base) 0
else {
<- (-1)^(x < 1)
pow + signif_digits(x/base^pow, base)
pow
} }
<- function(x, base){
signif_digits_1 <- abs(x)
x <- 0
result <- if(x < 1) -1 else 1
pow while(round(x, 6) < 1 | x >= base){
<- result + pow
result <- x / base^pow
x
}
result }
Digit Sum
<- function(n){
digit_sum <- abs(n)
n if(n < 10) return(n)
%% 10 + Recall(n%/%10)
n }
Digital Root
<- function(n){
digit_root if(abs(n)>10) digit_root(digit_sum(n))
else n
}
Logarithms:
<- function(x){
my_log_using10 <- 2.302585092994045901094
const <- signif_digits(x, 10)
b <- x/10^b
a if(a>2) {
<- a/10
a <- b + 1
b
}my_log_restricted(a) + b*const
}
<- function(x){
my_log_using2 <- 0.6931471805599452862268
const <- signif_digits(x, 2)
b <- x/2^b
a my_log_restricted(a) + b*const
}
<-function(x){
my_log2 if(x==1) return(0)
<- 1
w if(x<1) {
<- 1/x
x <- -1
w
}<- 0
res <- 0
n for(i in 1:20){
<- 0
m while(x<2){
<- m+1
m <- x**2
x
}<- x/2
x <- m + n
n <- res + 2^-n
res
}* res
w }
Optimization
<- 1
x repeat{
<- x^2*exp(3*x) - 10
f <- 2*x*exp(3*x) + 3*x^2*exp(6*x)
f_prime <- x - f/f_prime
x_new if(abs(x_new - x)<1e-8) break
<- x_new
x
} x
[1] 0.8645552
^2*exp(3*x) x
[1] 10
<- 1
x repeat{
<- x^3 + 8
f <- 3*x^2
f_prime <- x - f/f_prime
x_new if(abs(x_new - x)<1e-8) break
<- x_new
x
} x
[1] -2
^3 x
[1] -8
<- function(x){
my_sqrt <- 1
y repeat{
<- y^2 - x
f <- 2*y
f_prime <- y - f/f_prime
y_new if(abs(y_new - y)<1e-8) break
<- y_new
y
}
y
}my_sqrt(49)
[1] 7
my_sqrt(2)
[1] 1.414214
<- function(x){
my_log_newton if(x==1)return(0)
<- 1
y repeat{
<- my_exp(y) - x
f <- my_exp(y)
f_prime <- y - f/f_prime
y_new if(abs(y_new - y)<1e-16) break
<- y_new
y
}
y }
<- function(x0, f, fprime){
my_optim repeat{
<- x0 - f(x0)/fprime(x0)
x if(abs(x - x0)<1e-8) break
<- x
x0
}
x0
}
my_optim(1, function(z) z^3 + 8, function(x)3*x^2)
[1] -2
Numerical Differentiation/Derivatives
<- function(x, f){
my_derive <- 1e-8
h f(x+h) - f(x))/h
( }
<- function(f){
my_optim2 <- 1
x repeat{
<- x - f(x)/my_derive(x, f)
x_new if(abs(x_new - x)<1e-8) break
<- x_new
x
}
x
}
my_optim2(function(x)x^2*exp(3*x)-10)
[1] 0.8645552
Better Fibonacci Recursion
<- function(x, start = 0, end = 1){
my_fib if(x == 1)end
else Recall(x - 1, end, start + end)
}my_fib(30) # Compare the times with the previous fib function
[1] 832040
my_fib(200)# Do not run fib(200)
[1] 2.805712e+41
Tower of Hanoi
<- function(height, from, to, via){
tower_of_hanoi if(height == 1)
cat("Move disk",height,"from", from, "to", to, "\n")
else {
tower_of_hanoi(height-1, from, via, to)
cat("Move disk",height,"from", from, "to", to, "\n")
tower_of_hanoi(height-1, via, to, from)
}
}tower_of_hanoi(4, "A", "B", "C")
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
Move disk 4 from A to B
Move disk 1 from C to B
Move disk 2 from C to A
Move disk 1 from B to A
Move disk 3 from C to B
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B