Skip to content

Commit 818b522

Browse files
giordanomcabbott
andcommitted
Simplify Julia implementation
Co-authored-by: Michael Abbott <[email protected]>
1 parent ad09d5f commit 818b522

File tree

1 file changed

+2
-22
lines changed

1 file changed

+2
-22
lines changed

src/leibniz.jl

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,2 @@
1-
struct SignVector <: AbstractVector{Float64}
2-
len::Int
3-
end
4-
Base.size(s::SignVector) = (s.len,)
5-
Base.getindex(::SignVector, i::Int) = Float64((-1)^iseven(i))
6-
7-
function f(rounds)
8-
xs = SignVector(rounds + 2)
9-
pi = 1.0
10-
11-
@simd for i in 2:(rounds + 2)
12-
x = xs[i]
13-
pi += x / (2 * i - 1)
14-
end
15-
16-
return pi*4
17-
end
18-
19-
@static if abspath(PROGRAM_FILE) == @__FILE__
20-
rounds = parse(Int64, readchomp("rounds.txt"))
21-
print(f(rounds))
22-
end
1+
f(n) = sum(i -> 4/(4i-2n-3), 1:n)
2+
abspath(PROGRAM_FILE) == @__FILE__ && print(f(parse(Int, readchomp("rounds.txt"))))

0 commit comments

Comments
 (0)