Skip to content

Instantly share code, notes, and snippets.

View lune-sta's full-sized avatar

lune* lune-sta

  • Rabbit Lab LLC
  • Japan
View GitHub Profile
@lune-sta
lune-sta / gist:3311369
Created August 10, 2012 05:36
Project Euler 19
y = 1901
m = 1
d = 1
w = 1 # 0..6 -> Sun..Sat
count = 0
def m31? (m)
[1,3,5,7,8,10,12].each do |i|
if m == i
return true
@lune-sta
lune-sta / gist:3311530
Created August 10, 2012 05:59
Project Euler 19-2
require 'date'
st = Date::new(1901, 1, 1)
en = Date::new(2000, 12, 31)
count = 0
while st != en
count += 1 if st.day == 1 && st.wday == 0
st += 1
end
p count # 171
@lune-sta
lune-sta / gist:3318223
Created August 10, 2012 21:34
Project Euler 20
puts (1..100).inject(1){|a, b| a * b}.to_s.split(//).map(&:to_i).inject(0){|a, b| a + b} # 648
@lune-sta
lune-sta / gist:3330516
Created August 12, 2012 07:41
Project Euler 21
require 'prime'
arr = Array.new(10001){1}
sum = 0
2.upto(10000) do |i|
i.prime_division.each do |j|
a = 0
(j[1]+1).times do |k|
a += j[0]**k
end
arr[i] *= a
@lune-sta
lune-sta / gist:3334737
Created August 12, 2012 21:48
Project Euler 22
c, score = 0, 0
open("names.txt").gets.split(/,/).map{|a| a.delete("\"")}.sort.each do |i|
c += 1
i.size.times do |j|
score += (i[j].bytes.to_a[0].to_i - "A".bytes.to_a[0].to_i + 1) * c
end
end
puts score # 871198282
@lune-sta
lune-sta / gist:3344173
Created August 13, 2012 21:18
Project Euler 23
require 'prime'
m = 28123
sum = 0
adn = Array.new()
flag = Array.new(m+1){false}
2.upto(m) do |i|
a = 1
i.prime_division.each do |j|
b = 0
(j[1]+1).times do |k|
@lune-sta
lune-sta / gist:3355214
Created August 15, 2012 02:46
Project Euler 24
puts (0..9).to_a.permutation(10).to_a[1000000-1].join
# 2783915460
@lune-sta
lune-sta / gist:3365517
Created August 16, 2012 01:47
Project Euler 25
f = [1,1]
f.push(f[-1] + f[-2]) while f[-1].to_s.size < 1000
p f.size # 4782
@lune-sta
lune-sta / gist:3373708
Created August 16, 2012 21:13
Project Euler 26
require 'Prime'
max, answer = 0, 0
Prime.each(1000).to_a.reject{|p| p == 2 || p == 5}.each do |p|
a, c = 10, 1
while a % p != 1 do
c += 1
a *= 10
end
max, answer = c, p if max < c
end
@lune-sta
lune-sta / gist:3382641
Created August 17, 2012 21:03
Project Euler 27
require 'Prime'
max, answer = 0, 0
(-999..999).each do |a|
Prime.each(999).to_a.each do |b|
n = 0
n += 1 while Prime.prime?(n ** 2 + a * n + b)
max, answer = n, a * b if max < n
end
end
puts answer # -59231