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.

extra課題(google)

Last updated at Posted at 2020-12-31

!Mac OS X-10.15.7 !ruby-2.6.3p62

課題内容

自然対数の底e(200桁まで)の値で連続10桁で最初の素数を求める.以下をexp.txtとして入力として使用する.

2.71828182845904523536028747135266249775
7247093699959574966967627724076630353547
5945713821785251664274274663919320030599
2181741359662904357290033429526059563073
81323286279434907632338298807531952510190

方針

素数を特定するために, 以下サイトを参考にした.https://qiita.com/evendemiaire/items/f7069a77b7432a3622a3

コード

# require './aeroc'
require 'prime'
require 'colorize'


def puts_vals(expected, result)
    puts "expected :: #{expected}"
    puts "result   :: #{result}"
end


def assert_equal(expected, result)
    puts_vals(expected, result)
    print  case expected == result
	when true  ; "succeeded in #{__method__}.\n".green
	when false ; "failed in #{__method__}.\n".red
	end
end


def check(num)
    2.step(to: Math.sqrt(num), by: 1) do |i|    
    return false if num%i==0
    true
    end
end


def read_exp(path)
    exp = File.read(path).delete(".")
    return exp
  end


def exp_check(exp)
    0.step(to: exp.length-10, by: 1) do |i|
	num = exp[i..i+10-1].to_i
	if check(num)
	result = num
	end
	if Prime.prime?(num)
	assert_equal(num,result)
	break
	end
    end
end


if $PROGRAM_NAME == __FILE__
    exp = read_exp("exp.txt")
    exp_check(exp)
end

参考サイト


  • source ~/my_ruby/grad_members_20f/members/drop-sd/lectures/ex_google.org
1
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
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?