1
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-24

自然対数eの値で連続する10桁の数のうち、最初の素数を出力するプログラムです。

本体
def prime?(number)
 if (number < 2) return false;
 elsif (number == 2) return true;
 elsif (number % 2 == 0) return false;
 end

 sqrt_num = Math.sqrt(number);
 3.step(sqrt_num,3) do |i|
  if number % 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)
 number = exp[i..(i+9)].to_i
 prime = prime?(number)
 if prime 
  puts(number)
  break
 end
end
結果
1828182845

  • source ~/grad_members_20f/members/acoe5781/Google_Recruit.org
1
0
1

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
1
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?