brewersfan1976

comfortable_numbers.rb

Sep 25th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.88 KB | None | 0 0
  1. def comfortableNumbers(l, r)
  2.     a = l
  3.     b = a + 1
  4.     a_sum = 0
  5.     b_sum = 0
  6.     pairs = 0
  7.    
  8.     while (a < r)
  9.         string = a.to_s
  10.         x = 0
  11.            
  12.         while (x < string.length)
  13.               a_sum = a_sum + string[x].to_i
  14.               x = x + 1
  15.         end
  16.        
  17.         while (b <= r)
  18.               string = b.to_s
  19.               x = 0
  20.            
  21.               while (x < string.length)
  22.                     b_sum = b_sum + string[x].to_i
  23.                     x = x + 1
  24.               end
  25.            
  26.               if b >= a - a_sum and b <= a + a_sum and a >= b - b_sum and a <= b + b_sum then
  27.                  pairs = pairs + 1
  28.               end
  29.                  
  30.               b = b + 1
  31.               b_sum = 0
  32.         end
  33.            
  34.         a = a + 1
  35.         b = a + 1
  36.         a_sum = 0
  37.     end
  38.        
  39.     return pairs
  40. end
Advertisement
Add Comment
Please, Sign In to add comment