Skip to content

Instantly share code, notes, and snippets.

View yuuki's full-sized avatar

Yuuki TSUBOUCHI yuuki

View GitHub Profile
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'prime'
Prime.each.with_index do |n, i|
puts n or break if (i + 1) == 10001
end
# スクリプトキディ
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
N = 731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188399879790879227492190169972088809377665727333001053367881220235421809751254540594752243525849077116705560136048395864467063244157221553975369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042242190226710556263211111093705442175069416589604080719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606058861164671094050775410022569831552000559357297257163626956188267042825248
(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