title: How to find and update levels accordingly based on points?
body: <p>I am creating a rails application which is like a game. So it has points and levels. For example: to become level one the user has to get atleast 100 points and again for level two the user has to reach level 2 the user has to collect 200 points. The level difference changes after every 10 levels i.e., The difference between each level changes after 10 levels always. By that I mean the difference in points between level one and two is 100 and the difference in points in level 11 and 12 is 150 and so on. There is no upper bound for levels.</p>
  
  <p>Now my question is let's say a user's total points is 3150 and just got updated to 3155. What's the optimal solution to find the current level and update it if needed?</p>
  
  <p>I can get a solution using while loops and again looping inside it which  will give a result in O(n^2). I need something better.</p>
  
  <p>I think this code works but I'm not sure if this is the best way to go about it</p>
  
  <pre class="lang-rb prettyprint-override"><code>def get_level(points)
    diff = 100
    sum = 0
    level = -1
    current_level = 0
  
    while level.negative?
      10.times do |i|
        current_level += 1
        sum += diff
  
        if points &gt; sum
          next
        elsif points &lt;= sum
          level = current_level
          break
        end
      end  
      diff += 50
    end
    puts level
  end
  </code></pre>