Task 1

Calculate an approximation of $\pi$ using the "shotgun" approach from https://arxiv.org/abs/1404.1499 . Due to high risks involved with using actual shotguns, substitute the shotgun with the rand function. How many "shots" do you need to achieve an accuracy of 4 digits?


In [1]:
n = 10000000;
k = 1;
for i = 1:n
    x = rand(2);
    if norm(x) <= 1
        k = k + 1;
    end
end
k/n*4


Out[1]:
3.1420424

Task 2

Another approach for approximating π is to evaluate the sum $$ \pi \approx \pi_n = \sqrt{12} \sum_{k=0}^n \frac{(-3)^{-k}}{2k+1} $$ Determine the smallest value for $n$ such that $|\pi - \pi_n| < 10^{-15}$.


In [3]:
x = 0;
k = 0;
while abs(x-pi) >= 1e-15
    x = x + sqrt(12)*(-3.0)^-k / (2k+1)
    k = k + 1;
end
k-1


Out[3]:
28

Task 3

Solve the first problem of project Euler, see https://projecteuler.net/problem=1 .


In [4]:
s = 0;
for i = 1:999
    if i % 3 == 0 || i % 5 == 0
        s = s + i
    end
end
s


Out[4]:
233168