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 7666273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:47:31+00:00 2026-05-31T14:47:31+00:00

Write an algorithm to find F(n) the number of bits set to 1, in

  • 0

Write an algorithm to find F(n) the number of bits set to 1, in all numbers from 1 to n for any given value of n.

Complexity should be O(log n)

For example:

1: 001
2: 010
3: 011
4: 100
5: 101
6: 110

So

F(1) = 1,  
F(2) = F(1) + 1 = 2,
F(3) = F(2) + 2 = 4,
F(4) = F(3) + 1 = 5,
etc.

I can only design an O(n) algorithm.

  • 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-05-31T14:47:32+00:00Added an answer on May 31, 2026 at 2:47 pm

    The way to solve these sorts of problems is to write out the first few values, and look for a pattern

    Number  binary   # bits set   F(n)
    1       0001     1            1
    2       0010     1            2
    3       0011     2            4
    4       0100     1            5
    5       0101     2            7
    6       0110     2            9
    7       0111     3            12
    8       1000     1            13
    9       1001     2            15
    10      1010     2            17
    11      1011     3            20
    12      1100     2            22
    13      1101     3            25
    14      1110     3            28
    15      1111     4            32
    

    It takes a bit of staring at, but with some thought you notice that the binary-representations of the first 8 and the last 8 numbers are exactly the same, except the first 8 have a 0 in the MSB (most significant bit), while the last 8 have a 1. Thus, for example to calculate F(12), we can just take F(7) and add to it the number of set bits in 8, 9, 10, 11 and 12. But that’s the same as the number of set-bits in 0, 1, 2, 3, and 4 (ie. F(4)), plus one more for each number!

    #    binary
    0    0 000
    1    0 001
    2    0 010
    3    0 011
    4    0 100
    5    0 101
    6    0 110
    7    0 111
    
    8    1 000  <--Notice that rightmost-bits repeat themselves
    9    1 001     except now we have an extra '1' in every number!
    10   1 010
    11   1 011
    12   1 100
    

    Thus, for 8 <= n <= 15, F(n) = F(7) + F(n-8) + (n-7). Similarly, we could note that for 4 <= n <= 7, F(n) = F(3) + F(n-4) + (n-3); and for 2 <= n <= 3, F(n) = F(1) + F(n-2) + (n-1). In general, if we set a = 2^(floor(log(n))), then F(n) = F(a-1) + F(n-a) + (n-a+1)


    This doesn’t quite give us an O(log n) algorithm; however, doing so is easy. If a = 2^x, then note in the table above that for a-1, the first bit is set exactly a/2 times, the second bit is set exactly a/2 times, the third bit… all the way to the x’th bit. Thus, F(a-1) = x*a/2 = x*2^(x-1). In the above equation, this gives us

    F(n) = x*2x-1 + F(n-2x) + (n-2x+1)
    

    Where x = floor(log(n)). Each iteration of calculating F will essentially remove the MSB; thus, this is an O(log(n)) algorithm.

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

Sidebar

Related Questions

I'm trying to write an algorithm that will find the set of all vertices
I'm trying to write an algorithm to generate the ceiling panel from a horiontally
I am trying to write a small application using bouncycastle algorithm, from the BouncyCastleProvider.java
I'm given a task to write an algorithm to compute the maximum two dimensional
I have an assignment to write an algorithm to find duplicate in a Dynamic
Problem Given a number n, 2<=n<=2^63. n could be prime itself. Find the prime
Given set of elements n[1], n[2], n[3], .... n[x] and a number V. (Elements
For a given set of text files, I need to find every \ character
I have to write an algorithm that find the path in DAG with single
I am trying to write an efficient algorithm that will effectively let me merge

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.