-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler.jl
More file actions
68 lines (56 loc) · 1.63 KB
/
Copy patheuler.jl
File metadata and controls
68 lines (56 loc) · 1.63 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
# Functions for Solving Project Euler Problems
function is_divisible(dividend, divisor)
# returns true if dividend is divisible by divisor
# if remainder == 0 then divisible
return dividend % divisor == 0
end
function is_prime(n::T) where {T<:Union{Int, BigInt}}
# Prime Number: natural number > 1 whose only divisors are 1 & itself
# check to see if n is divisible by any number <= sqrt(n)
if n == 2 || n == 3
return true
end
# even numbers are not prime
if is_divisible(n, 2)
return false
end
# Check to see whether it is divisible by any other odd number
# initialize a counter variable
i = 3
while i <= sqrt(n)
if is_divisible(n, i)
return false
end
i += 2
end
return true
end
function derivative(f)
# returns a function that computes the numerical derivative of f
# Source: Forio Julia Tutorial
# Reference: http://en.wikipedia.org/wiki/Numerical_differentiation#Practical_considerations_using_floating_point_arithmetic
return function(x)
# pick small value for h
h = x == 0 ? sqrt(eps(Float64)) : sqrt(eps(Float64))*x
# floating point arithmetic gymnastics
xph = x + h
dx = xph - x
# evaluate f at x + h
f1 = f(xph)
# evaluate f at x
f0 = f(x)
# divide the difference by h
return (f1 - f0) / dx
end
end
function eratosthenes(n)
primes = fill(true,n)
primes[1] = false
for p = 2:n
primes[p] || continue
for i = 2:div(n,p)
primes[p*i] = false
end
end
findall(primes)
end