Skip to content

Instantly share code, notes, and snippets.

View yuuki's full-sized avatar

Yuuki TSUBOUCHI yuuki

View GitHub Profile
(1..1000).each do |a|
(a..(1000 - a)).each do |b|
c = 1000 - a - b
puts a*b*c or exit if a**2 + b**2 == c**2
end
end
# 31875000
r = (2..2000000).select do |i|
!(2..Math.sqrt(i).floor).find {|j| i % j == 0 } # 素数判定
end.inject(:+)
puts r
# 142913828922
# 30.40s with Core i5 mem 4G
puts [
37107287533902102798797998220837590246510135740250,
46376937677490009712648124896970078050417018260538,
74324986199524741059474233309513058123726617309629,
91942213363574161572522430563301811072406154908250,
23067588207539346171171980310421047513778063246676,
89261670696623633820136378418383684178734361726757,
28112879812849979408065481931592621691275889832738,
44274228917432520321923589422876796487670272189318,
47451445736001306439091167216856844588711603153276,
def collatz(i)
Enumerator.new do |y|
n = i
while n != 1 do
n = n.even? ? n/2 : 3*n + 1
y << n
end
end
end
MAXSIZE = 1000000
MEMO = Array.new(MAXSIZE)
def collatz_size(i)
size = 0
n = i
while n != 1 do
if n <= MAXSIZE && MEMO[n]
size += MEMO[n]
break
def fact(n)
n == 1 and return 1
n*fact(n-1)
end
puts fact(40) / fact(20) / fact(20)
digits = Enumerator.new do |y|
n = 2**1000
while n > 0 do
y << n % 10
n /= 10
end
end
puts digits.inject(:+)
# 1366
print(sum((int(s) for s in list(str(2**1000)))))
# 1366
data = %w/
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
use 5.010;
# チート
use Date::Simple ('ymd');
use Date::Range;
my $range = Date::Range->new(ymd(1901, 1, 1), ymd(2000, 12, 31));
my @dates = grep { $_->day == 1 and $_->day_of_week == 0 } $range->dates;
say $#dates + 1;