Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8895079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:40:42+00:00 2026-06-14T23:40:42+00:00

Is there any algorithm to find out that how many ways are there for

  • 0

Is there any algorithm to find out that how many ways are there for write a number for example n , with sum of power of 2 ?

example : for 4 there are four ways :

4 = 4 
4 = 2 + 2 
4 = 1 + 1 + 1 + 1
4 = 2 + 1 + 1

thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:40:44+00:00Added an answer on June 14, 2026 at 11:40 pm

    Suppose g(m) is the number of ways to write m as a sum of powers of 2. We use f(m,k) to represent the number of ways to write m as a sum of powers of 2 with all the numbers’ power is less than or equal to k. Then we can reduce to the equation:

    if m==0 f(m,k)=1;    
    if k<0 f(m,k)=0;    
    if k==0 f(m,k)=1;    
    if m>=power(2,k) f(m,k)=f(m-power(2,k),k)+f(m,k-1);//we can use power(2,k) as one of the numbers or not.    
    else f(m,k)=f(m,k-1);
    

    Take 6 as an example:

    g(6)=f(6,2)
    =f(2,2)+f(6,1)
    =f(2,1)+f(4,1)+f(6,0)
    =f(0,1)+f(2,0)+f(2,1)+f(4,0)+1
    =1+1+f(0,1)+f(2,0)+1+1
    =1+1+1+1+1+1
    =6
    

    Here is the code below:

    #include<iostream>
    using namespace std;
    
    int log2(int n)
    {
        int ret = 0;
        while (n>>=1) 
        {
            ++ret;      
        }
        return ret;
    }
    
    int power(int x,int y)
    {
        int ret=1,i=0;
        while(i<y)
        {
            ret*=x;
            i++;
        }
        return ret;
    }
    
    int getcount(int m,int k)
    {
        if(m==0)return 1;
        if(k<0)return 0;
        if(k==0)return 1;
        if(m>=power(2,k))return getcount(m-power(2,k),k)+getcount(m,k-1);
        else return getcount(m,k-1);
    
    }
    
    int main()
    {
        int m=0;
        while(cin>>m)
        {
            int k=log2(m);
            cout<<getcount(m,k)<<endl;
        }
        return 0;
    }
    

    Hope it helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any algorithm that gives the compass direction of a shadow given latitude,
Is there any fast algorithm that allows to compare two files (for verification purpose)
Is there any ways to try to guess encryption algorithm used to encrypt the
I want to find out if there's a way to implement the certain algorithm
Are there any specific algorithms that will allow me to find the min and
Is there any way of finding out what assemblies that's not used in a
Is there any algorithm in c# to singularize - pluralize a word (in english)
Is there any specific algorithm for the encoding and decoding MD5. Kindly give me
Is there any well-known algorithm (or obvious solution) for transforming a list from order
Is there any known hashing algorithm, which for similar input returns similar output? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.