google recruit

!Mac OS X-10.15.7!ruby-2.7.1p83

google recruit

Task

Output the first prime number of 10 consecutive digits in the value of the natural logarithm e Use the following as input as exp.txt

2.71828182845904523536028747135266249775
7247093699959574966967627724076630353547
5945713821785251664274274663919320030599
2181741359662904357290033429526059563073
81323286279434907632338298807531952510190

answer

code

def prime?(num)
 if (number < 2) return false;
 elsif (number == 2  && num == 3) return true;
 elsif (number % 2 == 0) return false;
 end

sqrt_num = Math.sqrt(num);
3.step(sqrt_num,2) do |i|
 if num % i == 0
  return false
 end
end

 return true
end

def read_exp
 exp = gets.to_s.chomp
end

exp = read_exp()
for i in 0..(exp.length - 10)
 num = exp[i..(i+9)].to_i
 prime = prime?(num)
 if prime 
  puts(num)
  break
 end
end

result

7427466391

Recommended Posts

google recruit
google recruit
google recruit
google recruit
Google Recruit
Google Recruit
Google Recruit
Google Recruit
Google recruit problem
EX2: Google Recruit
Google recruit problem, exp and prime
google java format