removing binaries + new crystal solutions

master
evan hemsley 2018-02-21 15:07:31 -08:00
parent f678044b9b
commit 9426f02642
13 changed files with 59 additions and 26 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
.DS_Store
c/binaries
*/bin/*
*.dwarf

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,26 +0,0 @@
#wrong approach, should just do recursive trial division...
def eratosthenes_sieve(n)
sieve = Array.new(n + 1, true)
sieve[0] = false
sieve[1] = false
2.step(to: Math.sqrt(n)) do |i|
if sieve[i]
(i * i).step(to: n, by: i) do |j|
sieve[j] = false
end
end
end
result = sieve.map_with_index { |b, i| b ? i : nil }.compact
end
def prime_factors(n)
nums = eratosthenes_sieve(n).select { |i| n % i == 0 }
nums = nums + nums.map { |i| n / i }
end
puts eratosthenes_sieve(600851475143)
puts prime_factors(600851475143).max

Binary file not shown.

23
crystal/euler.cr Normal file
View File

@ -0,0 +1,23 @@
# miscellaneous stuff
module Euler
def self.eratosthenes_sieve(n)
sieve = Array.new(n + 1, true)
sieve[0] = false
sieve[1] = false
2.step(to: Math.sqrt(n)) do |i|
if sieve[i]
(i * i).step(to: n, by: i) do |j|
sieve[j] = false
end
end
end
result = sieve.map_with_index { |b, i| b ? i : nil }.compact
end
def self.palindrome?(x)
x.to_s.reverse == x.to_s
end
end

27
crystal/euler003.cr Normal file
View File

@ -0,0 +1,27 @@
alias NumType = Int32 | Int64 | UInt32 | UInt64
def trial_division(n : NumType)
factors = [] of NumType
check = ->(p: NumType) {
q, r = n.divmod(p)
while r.zero?
factors << p
n = q
q, r = n.divmod(p)
end
}
check.call(2)
check.call(3)
p = 5
while p * p <= n
check.call(p)
p += 2
check.call(p)
p += 4
end
factors << n if n > 1
factors
end
puts trial_division(600851475143).max

7
crystal/euler004.cr Normal file
View File

@ -0,0 +1,7 @@
require "./euler"
def products_of_three_digit_nums
(100..999).to_a.combinations(2).map { |p| p.product }
end
puts products_of_three_digit_nums.select { |x| Euler.palindrome?(x) }.max