2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

google recruit

Last updated at Posted at 2020-12-27

!Mac OS X-10.15.7 !ruby-2.7.1p83

google recruit

課題

自然対数eの値で連続10桁の数のうち、最初の素数を出力する以下をexp.txtとして入力として使用する

2.71828182845904523536028747135266249775
7247093699959574966967627724076630353547
5945713821785251664274274663919320030599
2181741359662904357290033429526059563073
81323286279434907632338298807531952510190

解答

コード

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

結果

7427466391

  • source ~/grad_members_20f/members/wjswnsrud12/gool.org
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?