Gabz

PI Search

Jul 17th, 2014
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.77 KB | None | 0 0
  1. #!/usr/local/bin/ruby
  2.  
  3. $stdout.sync = true
  4. k, a, b, a1, b1 = 2, 4, 1, 12, 4
  5.  
  6. search = "GAB".unpack('c*')
  7. pos    = 0
  8. last   = nil
  9. piPos  = 0
  10. fullBreak = false
  11.  
  12. until fullBreak
  13.   # Next approximation
  14.   p, q, k = k*k, 2*k+1, k+1
  15.   a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
  16.   # Print common digits
  17.   d = a / b
  18.   d1 = a1 / b1
  19.   while (d == d1) and not (fullBreak)
  20.         if last
  21.             num = "#{last}#{d}".to_i
  22.            
  23.             if num == search[pos]
  24.                 chr = num.chr
  25.                 puts "#{chr} : #{piPos}"
  26.                 pos += 1
  27.                 if pos == search.size
  28.                     puts "Encontrado na casa decimal ##{piPos - 1} de PI"
  29.                     fullBreak = true
  30.                 end
  31.             elsif pos > 0
  32.                 pos = 0
  33.             end
  34.         end
  35.      # print d
  36.         last = d
  37.         piPos += 1
  38.     a, a1 = 10*(a%b), 10*(a1%b1)
  39.     d, d1 = a/b, a1/b1
  40.   end
  41. end
Advertisement
Add Comment
Please, Sign In to add comment