title: Number of possible equations of K numbers whose sum is N in ruby
body: <p>I have to create a program in ruby on rails so that it will take less time to solve the particular condition. Now i am to getting the less response time for k=4 but response time is more in case of k&gt;5</p>
  <p>Problem:</p>
  <blockquote>
  <p>Problem is response time.</p>
  <p>When value of k is more than 5 (k&gt;5) response time is too late for given below equation.</p>
  </blockquote>
  <p>Input: K, N (where 0 &lt; N &lt; ∞, 0 &lt; K &lt; ∞, and K &lt;= N)
  Output: Number of possible equations of K numbers whose sum is N.</p>
  <pre><code>Example Input:
  N=10 K=3
  Example Output:
  Total unique equations = 8
    1 + 1 + 8 = 10
    1 + 2 + 7 = 10
    1 + 3 + 6 = 10
    1 + 4 + 5 = 10
    2 + 2 + 6 = 10
    2 + 3 + 5 = 10
    2 + 4 + 4 = 10
    3 + 3 + 4 = 10
  For reference, N=100, K=3 should have a result of 833 unique sets 
  </code></pre>
  <p>Here is my ruby code</p>
  <pre><code>module Combination
    module Pairs
      class Equation
        def initialize(params)
          @arr=[]
          @n = params[:n]
          @k = params[:k]
        end
  
        #To create possible equations
        def create_equations
          return &quot;Please Enter value of n and k&quot; if @k.blank? &amp;&amp; @n.blank?
          begin
            Integer(@k)
          rescue
            return &quot;Error: Please enter any +ve integer value of k&quot;
          end
          begin
            Integer(@n)
          rescue
            return &quot;Error: Please enter any +ve integer value of n&quot;
          end
          return &quot;Please enter k &lt; n&quot; if @n &lt; @k
          create_equations_sum
        end
  
        def create_equations_sum
          aar = []
          @arr = []
          @list_elements=(1..@n).to_a
          (1..@k-1).each do |i|
            aar &lt;&lt; [*0..@n-1]
          end
          traverse([], aar, 0)
          return @arr.uniq #return result
        end
  
        #To check sum
        def generate_sum(*args)
          new_elements = []
          total= 0
          args.flatten.each do |arg|
            total += @list_elements[arg]
            new_elements &lt;&lt; @list_elements[arg]
          end
          if total &lt; @n
            new_elements &lt;&lt; @n - total
            @arr &lt;&lt; new_elements.sort
          else
            return
          end
        end
  
        def innerloop(arrayOfCurrentValues)
          generate_sum(arrayOfCurrentValues)
        end
  
        #Recursive method to create dynamic nested loops.
        def traverse(accumulated,params, index) 
          if (index==params.size)
            return innerloop(accumulated) 
          end
          currentParam = params[index]
          currentParam.each do |currentElementOfCurrentParam|
            traverse(accumulated+[currentElementOfCurrentParam],params, index+1)
          end
        end
      end
    end
  end
  </code></pre>
  <p>run the code using</p>
  <pre><code>params = {:n =&gt;100, :k =&gt;4}
  c = Combination::Pairs::Equation.new(params)
  c.create_equations
  </code></pre>