title: How to 'reverse sum' in Ruby?
body: <p>I have no clue how to call this in correct math-terms. Consider a method which takes two digits:</p>
  
  <pre><code> def num_of_sum(total, group_count)
  
   end
  </code></pre>
  
  <p>where <code>total</code> is an integer and <code>group_count</code> is an integer.</p>
  
  <p>How would I get a 'nicely' grouped Array of integers of <code>group_count</code>-length which sum up till <code>total</code>.</p>
  
  <p>My spec would look like:</p>
  
  <pre><code>describe "number to sum of" do
    it "grabs all numbers" do
      expect(num_of_sum(10, 2)).to eq([5,5])
      expect(num_of_sum(10, 3)).to eq([3,3,4])
      expect(num_of_sum(20, 3)).to eq([6,7,7])
      expect(num_of_sum(100, 3)).to eq([33,33,34])
      expect(num_of_sum(100, 2)).to eq([50,50])
    end
  end
  </code></pre>
  
  <p>I tried this, which works:</p>
  
  <pre><code>def num_of_sum(total, in_groups_of)
    result = []
    section_count ||= (total.to_f / in_groups_of.to_f).round
  
    while(total &gt; 0)
      total -= section_count
  
      if (total - section_count) &lt; 0 &amp;&amp; (total + section_count).even?
        section_count += total
        total -= total
      end
  
      result &lt;&lt; section_count
    end
  
    result
  end
  </code></pre>
  
  <p>But, for instance, this spec doesn't work:</p>
  
  <pre><code> expect(num_of_sum(67,5)).to eq([13,13,13,14,14]) 
  </code></pre>
  
  <p>I need the array to contain numbers that are as close to each other as possible. But the array is limited to the length of the <code>group_count</code>.</p>
  
  <hr>
  
  <p>Does someone know what the mathemetical name for this is, so I can search a bit more accurately?</p>