aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--julia/002-even_fibonacci_numbers.jl21
-rw-r--r--julia/003-largest_prime_factor.jl3
-rw-r--r--julia/004-largest_palindrome_product.jl16
-rw-r--r--julia/006-sum_square_difference.jl9
-rw-r--r--julia/007-10001st_prime.jl24
-rw-r--r--julia/008-largest_product_in_a_series.jl16
-rw-r--r--julia/009-special_pythagorean_triplet.jl12
-rw-r--r--julia/010-summation_of_primes.jl8
8 files changed, 48 insertions, 61 deletions
diff --git a/julia/002-even_fibonacci_numbers.jl b/julia/002-even_fibonacci_numbers.jl
index 052d20d..2b4b562 100644
--- a/julia/002-even_fibonacci_numbers.jl
+++ b/julia/002-even_fibonacci_numbers.jl
@@ -24,17 +24,12 @@ function fib(n)
end
# https://stackoverflow.com/questions/56137634/function-chaining-in-julia
-# need curried functions
-# ( (fib(n) for n in 1:1_000_000)
-# |> takewhile(<(4_000_000))
-# )
-println(sum(
- Iterators.filter(
- x -> x % 2 == 0,
- takewhile(
- <(4_000_000),
- fib(n) for n in 1:1_000_000
- )
- )
-))
+result = (
+ (fib(n) for n in 1:1_000_000)
+ |> r -> takewhile(<(4_000_000), r)
+ |> r -> Iterators.filter(x -> (x % 2) == 0, r)
+ |> sum
+)
+
+println(result)
diff --git a/julia/003-largest_prime_factor.jl b/julia/003-largest_prime_factor.jl
index ef2252c..0fa7bff 100644
--- a/julia/003-largest_prime_factor.jl
+++ b/julia/003-largest_prime_factor.jl
@@ -22,4 +22,5 @@ function factors(n)
factors
end
-println(maximum(factors(NUMBER)))
+result = factors(NUMBER) |> maximum
+println(result)
diff --git a/julia/004-largest_palindrome_product.jl b/julia/004-largest_palindrome_product.jl
index 32be760..308ce48 100644
--- a/julia/004-largest_palindrome_product.jl
+++ b/julia/004-largest_palindrome_product.jl
@@ -7,20 +7,10 @@
# Find the largest palindrome made from the product of two 3-digit numbers.
###
-function is_palindrom(n)
+function is_palindrom(n::Integer)::Bool
s = string(n)
s == reverse(s)
end
-
-top = -1
-
-for x in 100:999
- for y in 100:999
- if is_palindrom(x * y)
- global top = max(top, x * y)
- end
- end
-end
-
-println(top)
+result = maximum(x * y for x in 100:999, y in 100:999 if is_palindrom(x * y))
+println(result)
diff --git a/julia/006-sum_square_difference.jl b/julia/006-sum_square_difference.jl
index 39e5df8..fd5829d 100644
--- a/julia/006-sum_square_difference.jl
+++ b/julia/006-sum_square_difference.jl
@@ -12,9 +12,6 @@
# numbers and the square of the sum.
###
-const count = 100
-
-sum_of_squares = sum(x ^ 2 for x in 1:count)
-square_of_sum = sum(1:count) ^ 2
-
-println(square_of_sum - sum_of_squares)
+const COUNT = 100
+result = sum(1:COUNT) ^ 2 - sum(x ^ 2 for x in 1:COUNT)
+println(result)
diff --git a/julia/007-10001st_prime.jl b/julia/007-10001st_prime.jl
index 6928484..70bf4e3 100644
--- a/julia/007-10001st_prime.jl
+++ b/julia/007-10001st_prime.jl
@@ -10,6 +10,8 @@
using Printf
using Base.Iterators
+# Can't use eratosthenes sieve since we don't know when to stop (no predefined list of numbers)
+# A algorithm that caches the previous found primes should be faster
function is_prime(n)
if n == 2 || n == 3 || n == 5
return true
@@ -17,7 +19,7 @@ function is_prime(n)
if n % 2 == 0 || n % 3 == 0 || n % 5 == 0
return false
end
- for d in 6:6:Int64(ceil(sqrt(n)) + 1)
+ for d in 6:6:ceil(Integer, √n) + 1
if n % (d - 1) == 0 || n % (d + 1) == 0
return false
end
@@ -25,13 +27,13 @@ function is_prime(n)
true
end
-counter = 0
-for n in Iterators.countfrom(2)
- if is_prime(n)
- global counter += 1
- @printf "%5d: %d\n" counter n
- end
- if counter == 10_001
- break
- end
-end
+const PRIME_INDEX = 10_001
+
+result = (
+ Iterators.countfrom(2)
+ |> x -> Iterators.filter(is_prime, x)
+ |> x -> Iterators.take(x, PRIME_INDEX)
+ |> collect
+)[end]
+
+println(result)
diff --git a/julia/008-largest_product_in_a_series.jl b/julia/008-largest_product_in_a_series.jl
index 7a67206..c2811be 100644
--- a/julia/008-largest_product_in_a_series.jl
+++ b/julia/008-largest_product_in_a_series.jl
@@ -29,7 +29,7 @@
###
-const NUMBER_STRING = join(split("
+const NUMBER_STRING = join(split("""
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
@@ -50,12 +50,16 @@ const NUMBER_STRING = join(split("
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
-"))
+"""))
const CHUNK_SIZE = 13
+const NUMBERS = [parse(Int, c) for c in NUMBER_STRING]
-const NUMBERS = [parse(Int, c) for c in NUMBER_STRING]
-const NUMBER_CHUNKS = zip([NUMBERS[start:end] for start in 1:CHUNK_SIZE]...)
-const RESULT = maximum(cumprod(chunk)[end] for chunk in NUMBER_CHUNKS)
+result = (
+ (NUMBERS[start:end] for start in 1:CHUNK_SIZE)
+ |> x -> zip(x...)
+ |> x -> map(prod, x)
+ |> maximum
+)
-println(RESULT)
+println(result)
diff --git a/julia/009-special_pythagorean_triplet.jl b/julia/009-special_pythagorean_triplet.jl
index 65404be..6723e66 100644
--- a/julia/009-special_pythagorean_triplet.jl
+++ b/julia/009-special_pythagorean_triplet.jl
@@ -13,14 +13,10 @@
using Base.Iterators
-for c in countfrom(1)
- for b in 1:(c - 1)
- for a in 1:(b - 1)
- if a ^ 2 + b ^ 2 == c ^ 2 && a + b + c == 1000
- println(a * b * c)
- exit(0)
- end
- end
+for c in countfrom(1), b in 1:(c - 1), a in 1:(b - 1)
+ if a ^ 2 + b ^ 2 == c ^ 2 && a + b + c == 1000
+ println(a * b * c)
+ break
end
end
diff --git a/julia/010-summation_of_primes.jl b/julia/010-summation_of_primes.jl
index 1e93a4f..5918d36 100644
--- a/julia/010-summation_of_primes.jl
+++ b/julia/010-summation_of_primes.jl
@@ -12,12 +12,14 @@ function eratosthenes_sieve(stop)
while true
prime = pop!(ns)
push!(primes, prime)
- if prime > ceil(sqrt(stop))
+ if prime > ceil(Integer, √stop)
return append!(primes, ns)
break
end
- ns = filter(n -> n % prime != 0, ns)
+ filter!(n -> n % prime != 0, ns)
end
end
-println(sum(eratosthenes_sieve(2_000_000 - 1)))
+result = eratosthenes_sieve(2_000_000 - 1) |> sum
+
+println(result)