[RUBY] Google Recruit

!ruby-2.7.0p0

Task

https://qiita.com/daddygongon/items/ba94b0f2a73990fc6a07

problem

Find the first prime number of 10 consecutive digits of e (base of natural logarithm) with ruby. However, e (base of natural logarithm) is up to 200 digits.

answer

code


def prime?(num)
  return true if num<=3
  (2..Math.sqrt(num)).each do |i|
    return false if num % i == 0
  end
  return true
end

instring = gets.to_s.chop
(2..instring.length-2-10).each do |i|
  num = instring[i..i+10-1].to_i
  if prime?(num)
    puts "#{num} is first 10 digit prime."
    break
  end
end

Input file

2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190

result

$ ruby google_recruit.rb  < google_recruit_in.txt
7427466391 is first 10 digit prime.

Recommended Posts

google recruit
google recruit
google recruit
Google Recruit
Google Recruit
Google Recruit
Google Recruit
Google recruit problem
EX2: Google Recruit
google java format