LoginSignup
9
10

More than 5 years have passed since last update.

Rubyで円周率を10000桁出力するワンライナー

Posted at

Pythonで円周率を10000桁出力するワンライナー に触発されました。

ワンライナー

円周率10000桁
ruby -r bigdecimal/math -e "puts BigMath.PI(10000).to_s(?F)[0..10001]"

解説

require 'bigdecimal/math'

# 有効桁数10000桁の円周率 (BigDecimal)
num = BigMath.PI 10000

# BigDecimal#to_sに"f"を渡さない場合、指数形式で出力される
str = num.to_s "f"

# 文字列の範囲指定で1万桁までを取り出す (「3.」を含むから10002文字)
puts str[0..10001]

有効桁数10000でto_sしたら小数点以下10034桁まで出力された
(確かめたら一応10014桁まで正しかった)

累乗根とかも出来る

require 'bigdecimal/math'
require 'bigdecimal/util'

puts 2.to_d.sqrt(10000).to_s("f")

詳しくは参考リンクへ (解説してねー)

参考

9
10
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
9
10