aboutsummaryrefslogtreecommitdiff
path: root/julia
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-06-18 21:50:31 +0200
committerCharles Cabergs <me@cacharle.xyz>2021-06-18 21:50:31 +0200
commit93b4c2a215556153ed948260678cd8e840493b39 (patch)
tree393edc821a5cd00db7971b4612c98855ff762945 /julia
parentc0aeed4578bdaea39ddbed1b55896948f56b23f3 (diff)
downloadproject_euler-93b4c2a215556153ed948260678cd8e840493b39.tar.gz
project_euler-93b4c2a215556153ed948260678cd8e840493b39.tar.bz2
project_euler-93b4c2a215556153ed948260678cd8e840493b39.zip
problem 3 4 5 6 6 in julia
Diffstat (limited to 'julia')
-rw-r--r--julia/003-largest_prime_factor.jl25
-rw-r--r--julia/004-largest_palindrome_product.jl26
-rw-r--r--julia/005-smallest_multiple.jl26
-rw-r--r--julia/006-sum_square_difference.jl20
-rw-r--r--julia/007-10001st_prime.jl37
5 files changed, 134 insertions, 0 deletions
diff --git a/julia/003-largest_prime_factor.jl b/julia/003-largest_prime_factor.jl
new file mode 100644
index 0000000..01e71cf
--- /dev/null
+++ b/julia/003-largest_prime_factor.jl
@@ -0,0 +1,25 @@
+###
+# Largest prime factor
+# Problem 3
+#
+# The prime factors of 13195 are 5, 7, 13 and 29.
+# What is the largest prime factor of the number 600851475143 ?
+###
+
+const NUMBER = 600851475143
+
+function factors(n)
+ factors = []
+ while n > 1
+ for d in 2:n
+ if n % d == 0
+ n = Int64(n / d)
+ push!(factors, d)
+ break
+ end
+ end
+ end
+ factors
+end
+
+println(maximum(factors(NUMBER)))
diff --git a/julia/004-largest_palindrome_product.jl b/julia/004-largest_palindrome_product.jl
new file mode 100644
index 0000000..32be760
--- /dev/null
+++ b/julia/004-largest_palindrome_product.jl
@@ -0,0 +1,26 @@
+###
+# Largest palindrome product
+# Problem 4
+#
+# A palindromic number reads the same both ways. The largest palindrome made from the
+# product of two 2-digit numbers is 9009 = 91 × 99.
+# Find the largest palindrome made from the product of two 3-digit numbers.
+###
+
+function is_palindrom(n)
+ 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)
diff --git a/julia/005-smallest_multiple.jl b/julia/005-smallest_multiple.jl
new file mode 100644
index 0000000..e938baf
--- /dev/null
+++ b/julia/005-smallest_multiple.jl
@@ -0,0 +1,26 @@
+###
+# Smallest multiple
+# Problem 5
+#
+# 2520 is the smallest number that can be divided by each of the numbers from 1 to 10
+# without any remainder.
+# What is the smallest positive number that is evenly divisible by all of the numbers from
+# 1 to 20?
+###
+
+using Base.Iterators
+
+for n in Iterators.countfrom(2, 2)
+ # any(d -> n % d != 0, reverse(2:20)) && continue
+ found = true
+ for d in reverse(2:20)
+ if n % d != 0
+ found = false
+ break
+ end
+ end
+ if found
+ println(n)
+ break
+ end
+end
diff --git a/julia/006-sum_square_difference.jl b/julia/006-sum_square_difference.jl
new file mode 100644
index 0000000..39e5df8
--- /dev/null
+++ b/julia/006-sum_square_difference.jl
@@ -0,0 +1,20 @@
+###
+# Sum square difference
+# Problem 6
+#
+# The sum of the squares of the first ten natural numbers is,
+# $$1^2 + 2^2 + ... + 10^2 = 385$$
+# The square of the sum of the first ten natural numbers is,
+# $$(1 + 2 + ... + 10)^2 = 55^2 = 3025$$
+# Hence the difference between the sum of the squares of the first ten natural numbers and
+# the square of the sum is $3025 - 385 = 2640$.
+# Find the difference between the sum of the squares of the first one hundred natural
+# 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)
diff --git a/julia/007-10001st_prime.jl b/julia/007-10001st_prime.jl
new file mode 100644
index 0000000..6928484
--- /dev/null
+++ b/julia/007-10001st_prime.jl
@@ -0,0 +1,37 @@
+###
+# 10001st prime
+# Problem 7
+#
+# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th
+# prime is 13.
+# What is the 10 001st prime number?
+###
+
+using Printf
+using Base.Iterators
+
+function is_prime(n)
+ if n == 2 || n == 3 || n == 5
+ return true
+ end
+ if n % 2 == 0 || n % 3 == 0 || n % 5 == 0
+ return false
+ end
+ for d in 6:6:Int64(ceil(sqrt(n)) + 1)
+ if n % (d - 1) == 0 || n % (d + 1) == 0
+ return false
+ end
+ end
+ 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