Browse Source

removing binaries + new crystal solutions

master
evan hemsley 4 years ago
parent
commit
9426f02642
  1. 2
      .gitignore
  2. BIN
      crystal/001/euler001
  3. BIN
      crystal/001/euler001.dwarf
  4. BIN
      crystal/002/euler002
  5. BIN
      crystal/002/euler002.dwarf
  6. BIN
      crystal/003/euler003
  7. 26
      crystal/003/euler003.cr
  8. BIN
      crystal/003/euler003.dwarf
  9. 23
      crystal/euler.cr
  10. 0
      crystal/euler001.cr
  11. 0
      crystal/euler002.cr
  12. 27
      crystal/euler003.cr
  13. 7
      crystal/euler004.cr

2
.gitignore

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

BIN
crystal/001/euler001

BIN
crystal/001/euler001.dwarf

BIN
crystal/002/euler002

BIN
crystal/002/euler002.dwarf

BIN
crystal/003/euler003

26
crystal/003/euler003.cr

@ -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

BIN
crystal/003/euler003.dwarf

23
crystal/euler.cr

@ -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

0
crystal/001/euler001.cr → crystal/euler001.cr

0
crystal/002/euler002.cr → crystal/euler002.cr

27
crystal/euler003.cr

@ -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

@ -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
Loading…
Cancel
Save