adi_isHere's blog

By adi_isHere, history, 5 months ago, In English

Hello Everyone. I am stuck on the following problem -

Given a natural number, n. Determine how many ways there are to represent the number n as a sum of powers of two. Partitions that differ in the order of terms are considered the same. For example, for the number 3, there are two partitions — 2 + 1 and 1 + 1 + 1, and for the number 5, there are four partitions — 4 + 1, 2 + 2 + 1, 2 + 1 + 1 + 1, 1 + 1 + 1 + 1 + 1.

This is the solution. Can anybody help me understand it?

There was also an explanation provided- Let's denote fi — the number of ways the number i is a sum of powers of 2, f0=1. Let's consider two cases — when i is odd and when i is even.

For the case of odd i, one can notice that in all partitions of this number one of the terms will be equal to 1, so the number of partitions of the number i in this case is equal to the number of partitions of the number i−1. Thus, fi=fi−1 for odd i.

For the even case i, one can notice that all partitions can be divided into two parts — partitions in which all terms are even and partitions in which at least one term equals 1. Thus fi=fi−1+fi/2.

The asymptotic behavior of the described solution is O(n).

  • Vote: I like it
  • 0
  • Vote: I do not like it