module Algorithmable::Cups::RootCubeIssue

Constants

ACCURACY

What is the fastest way to compute cube root?

Public Class Methods

newton_approximation(num, accuracy = ACCURACY) click to toggle source
# File lib/algorithmable/cups/root_cube_issue.rb, line 26
def self.newton_approximation(num, accuracy = ACCURACY)
  x = 1
  n = 10
  loop do
    0.upto(n).each do
      x = (2 * x + num / (x * x)) / 3.0
    end
    break if ((x * x * x) - num).abs < accuracy
  end
  x
end