Created
March 14, 2014 18:59
-
-
Save Jared-Prime/9554433 to your computer and use it in GitHub Desktop.
This file contains 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
#! /usr/bin/env ruby | |
# see: https://en.wikipedia.org/wiki/BaileyBorwein-Plouffe_formula | |
require 'bigdecimal' | |
NUMERATOR = ->(k) do | |
BigDecimal.new('120')*(k**2) + BigDecimal.new('151')*k + BigDecimal.new('47') | |
end | |
DENOMINATOR_1 = ->(k) do | |
BigDecimal.new('512')*(k**4) + BigDecimal.new('1024')*(k**3) + BigDecimal.new('712')*(k**2) + BigDecimal.new('194')*k + BigDecimal.new('15') | |
end | |
DENOMINATOR_2 = ->(k) do | |
BigDecimal.new('16')**k | |
end | |
sum = BigDecimal.new('0.0') | |
k = BigDecimal.new('0.0') | |
until k > 100 | |
sum += (NUMERATOR.(k)/(DENOMINATOR_1.(k) * DENOMINATOR_2.(k))) | |
k += 1.0 | |
puts "\n\tk: #{k}\n\tsum: #{sum}" | |
end | |
puts "\nfinal value: #{sum}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
^ looses accuracy after 19 digits