お題
https://qiita.com/daddygongon/items/ba94b0f2a73990fc6a07
解答
# frozen_string_literal: true
require './assert_equal'
def prime?(n)
return true if n == 2
# ある数 n に対し、初期値 m=1, l=nとして、mを増やしながら、lを減らしながら n =
# m*l となる(m,l)を探すとき、m=sqrt(n)に到達した以降の(m,l)は、m=sqrt(n) に到
# 達する以前の(l,m)と同じであるから、探索はm=sqrt(n)で打ちきることができる
max_i = Math.sqrt(n).to_i + 1
[*2..max_i].each do |i|
mod = n % i
break if mod.zero?
return true if i == max_i
end
false
end
def read_exp
gets.to_s.chomp
end
def prime_test
expected = [2, 3, 5, 7]
results = []
[*2..10].each do |num|
results << num if prime?(num)
end
assert_equal(expected, results)
end
def read_exp_test
exp1 = read_exp
assert_equal(7_182_818_284, exp1[2..2 + 10 - 1].to_i)
end
def main
exp1 = read_exp
[*2..exp1.length - 10].each do |i|
num = exp1[i..i + 10 - 1].to_i
if prime?(num)
puts num
break
end
end
end
main if __FILE__ == $PROGRAM_NAME
output
7427466391
類題
Congratulations. You've made it to level 2. Go to www.Linux.org and enter Bobsyouruncle as the login and the answer to this equation as the password.
f(1)=7182818284
f(2)=8182845904
f(3)=8747135266
f(4)=7427466391
f(5)=__________
ctf っぽい。数列を眺め、試行錯誤してたら法則性を偶然発見した。
# frozen_string_literal: true
require './assert_equal'
def read_exp
gets.to_s.chomp
end
def g_eq?(n)
sum = 0
until n.zero?
sum += n % 10
n = (n / 10).floor
end
sum == 49
end
def main
exp1 = read_exp
[*2..exp1.length - 10].each do |i|
num = exp1[i..i + 10 - 1].to_i
puts num if g_eq?(num)
end
end
main if __FILE__ == $PROGRAM_NAME
output
7182818284
8182845904
8747135266
7427466391
5966290435
2952605956
余談
Go to www.Linux.org and enter Bobsyouruncle as the login and the answer to thisequation as the password. っていうとおりにlogin を試みたが、失敗した。実は上の回答が間違っているか、もうgoogle_recruit は時間が経ちすぎていて、無効になっているか…多分後者だと思うけど。
- source ~/ghq/github.com/TeamNishitani/grad_members_20f/members/syasin-5d/google_recruit.org