brewersfan1976

kth_digit_after_decimal_point.rb

Sep 4th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.14 KB | None | 0 0
  1. def quotientKthDigitAfterDecimalPoint(a, b, k)
  2.     value =  a / b.to_f
  3.     string = value.to_s
  4.     x = 0
  5.     y = 1
  6.    
  7.     # the next nine tests will test for infinite numbers
  8.    
  9.     if string == "0.1111111111111111" then
  10.        return 1
  11.     end
  12.        
  13.     if string == "0.2222222222222222" then
  14.        return 2
  15.     end
  16.        
  17.     if string == "0.3333333333333333" then
  18.        return 3
  19.     end
  20.        
  21.     if string == "0.4444444444444444" then
  22.        return 4
  23.     end
  24.  
  25.     if string == "0.5555555555555555" then
  26.        return 5
  27.     end
  28.        
  29.     if string == "0.6666666666666666" then
  30.        return 6
  31.     end
  32.        
  33.     if string == "0.7777777777777777" then
  34.        return 7
  35.     end
  36.        
  37.     if string == "0.8888888888888888" then
  38.        return 8
  39.     end
  40.        
  41.     if string == "0.9999999999999999" then
  42.        return 9
  43.     end
  44.    
  45.     while(true)
  46.          if string[x] == '.' then
  47.             while (y <= k)
  48.                   x = x + 1
  49.                   y = y + 1
  50.             end
  51.              
  52.             return string[x].to_i
  53.          end
  54.              
  55.          x = x + 1
  56.     end
  57. end
Add Comment
Please, Sign In to add comment