Last active
October 1, 2015 04:53
-
-
Save junpeitsuji/d2a72bfd4ba8781e3f56 to your computer and use it in GitHub Desktop.
tt21akRW1 さんによる [7, 120] における素数の個数を返す計算式の検証のためのプログラム。参考URL: http://blogs.yahoo.co.jp/donald_stinger/14413096.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # tt21akRW1 さんによる [7, 120] における素数の個数を返す計算式の検証のためのプログラム | |
| # 参考URL: http://blogs.yahoo.co.jp/donald_stinger/14413096.html | |
| require 'prime' | |
| def mod(n, modulo) | |
| n.modulo(modulo).to_f | |
| end | |
| # tt21akRW1 さんによる [7, 120] における素数の個数を返す関数 | |
| def tt21akRW1_method n | |
| # (n*1*2*4*6/(2*3*5*7))+mod(n,2)/2+mod(n,3)/3+mod(n,5)/5+mod(n,7)/7-mod(n,6)/6-mod(n,10)/10-mod(n,14)/14-mod(n,15)/15-mod(n,21)/21-mod(n,35)/35+mod(n,30)/30+mod(n,42)/42+mod(n,70)/70+mod(n,105)/105-mod(n,210)/210+4-1 | |
| # 浮動小数で計算されるように式を修正 | |
| (n.to_f*1*2*4*6/(2*3*5*7))+mod(n,2)/2+mod(n,3)/3+mod(n,5)/5+mod(n,7)/7-mod(n,6)/6-mod(n,10)/10-mod(n,14)/14-mod(n,15)/15-mod(n,21)/21-mod(n,35)/35+mod(n,30)/30+mod(n,42)/42+mod(n,70)/70+mod(n,105)/105-mod(n,210)/210+4-1 | |
| end | |
| File.open("primes.dat", "w") do |io| | |
| (7..200).each do |n| | |
| result = tt21akRW1_method(n) | |
| correct = Prime.each(n).count | |
| io.puts "#{n} #{result} #{correct}" | |
| end | |
| end | |
| =begin | |
| # 以下gnuplot 用のスクリプト | |
| set key left top | |
| plot "primes.dat" u 1:2 t "tt21akRW1 method" w l, "primes.dat" u 1:3 t "Correct primes" w l | |
| pause -1 | |
| =end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment