Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created June 8, 2021 05:20
Show Gist options
  • Save havenwood/c0449fb7e37c244e09ff54addb47d4b5 to your computer and use it in GitHub Desktop.
Save havenwood/c0449fb7e37c244e09ff54addb47d4b5 to your computer and use it in GitHub Desktop.
# When Math.sqrt(2).fdiv(2) is just too zippy.
require 'bigdecimal'
require 'bigdecimal/util'
def i_ish(precision:)
sqrt_2 = 2.to_d.sqrt precision
sqrt_i = Complex sqrt_2 / 2, sqrt_2 / 2
sqrt_i * sqrt_i
end
0.step do |n|
imaginary = i_ish(precision: n).imaginary
nines = imaginary.to_digits.delete_prefix('0.')[/\A9*/]
puts "2.to_d.sqrt(#{n}) nines: #{nines.size}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment