<p>If you want to find all possible ways of representing a number N from a given set of numbers then you should follow a dynamic programming solution as already proposed. </p>

<p>But if you just want to know the number of ways, then you are dealing with the <a href="http://arxiv.org/pdf/math/0105218v2" rel="nofollow">restricted partition function problem</a>. </p>

<blockquote>
  <p>The restricted partition function p(n, dm) ≡ p(n, {d1, d2, . . . ,
  dm}) is a number of partitions of n into positive integers {d1, d2, .
  . . , dm}, each not greater than n.</p>
</blockquote>

<p>You should also check the wikipedia article on <a href="http://en.wikipedia.org/wiki/Partition_function_%28number_theory%29#Partition_function" rel="nofollow">partition function without restrictions</a> where no restrictions apply.</p>

<p>PS. If negative numbers are also allowed then there probably are (countably )infinite ways to represent your sum. </p>

<pre><code>1+1+1+1-1+1
1+1+1+1-1+1-1+1
etc...
</code></pre>

<p>PS2. This is more a math question than a programming one</p>
How many ways to represent a number from a given set of numbers
<p>I want to know in how many ways can we represent a number x as a sum of numbers from a given set of numbers {a1.a2,a3,...}. Each number can be taken more than once.</p>

<p>For example, if x=4 and a1=1,a2=2, then the ways of representing x=4 are:</p>

<pre><code>1+1+1+1
1+1+2
1+2+1
2+1+1
2+2
</code></pre>

<p>Thus the number of ways =5.</p>

<p>I want to know if there exists a formula or some other fast method to do so. I can't brute force through it. I want to write code for it.</p>

<p>Note: x can be as large as 10^18. The number of terms a1,a2,a3,… can be up to 15, and each of a1,a2,a3,… can also be only up to 15.</p>