to site top page

Problem 27 - Project Euler

prime = [2]
limit = Math::sqrt(1000 ** 2 + 1000 * 1000 + 1000).ceil
#limit = Math::sqrt(1601 ** 2 + 100 * 1601 + 1601).ceil

#start_time = Time.now
factor_f = 0
3.step(limit,2) {|i|
    prime.each {|x|
        factor_f += 1 if i % x == 0
        break if (factor_f > 0)||(x ** 2 > i)
    }
    if factor_f == 0
        prime << i
    else
        factor_f = 0
    end
}

count = factor_f = ans = 0
count_tmp = 1
b_max_num = prime.length - 1

for sign_a, sign_b in [[-1,1], [1,-1], [1,1], [-1,-1]]
for a in 1..100
for b in prime
    break if b > 1000
    for n in 1..b_max_num
        eu = n ** 2 + sign_a * a * n + sign_b * b
        break if eu < 0
        for m in prime
            if (eu % m == 0)||(m ** 2 > eu)
                factor_f = 1 if eu % m == 0
                break
            end
        end
        if factor_f == 0
            count_tmp += 1
        else
            factor_f = 0
        break
    end
    end
    if count_tmp > count
        count = count_tmp
        ans_a = sign_a * a
        ans_b = sign_b * b
    end
    count_tmp = 1
end
end
end
print('a=', ans_a, ', b=', ans_b, ', answer=', ans_a * ans_b, ' (count=', count, ")\n")
#end_time = Time.now
#puts end_time - start_time

can't load my result

最近の記事(5件分)

する事

そのうち記事にするかもリスト

欲しい本