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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:52:41+00:00 2026-06-17T02:52:41+00:00

While learning for an exam, I’ve just found the following task in an exercise:

  • 0

While learning for an exam, I’ve just found the following task in an exercise:

Write a function that gives the integer logarithm to base 2 (rounded up) while only using multiplication and addition.

I tried, immediately, but couldn’t come to any solution. I thought that would be an easy task but I could only find a solution when using integer division (e.g. in Haskell):

log2 :: Int -> Int
log2 1 = 0
log2 2 = 1
log2 x = 1 + log2 (x `div` 2)

Is this task possible with multiplication only at all? Using multiplication on the left side (pattern) always results in compiler errors. And using it on the right side, how can I trace the solution back to lower numbers?

  • 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-17T02:52:43+00:00Added an answer on June 17, 2026 at 2:52 am

    And using it on the right side, how can I trace the solution back to lower numbers?

    Recursion. Since it’s easier to compute the floor, we use the fact that

    ceiling (log_2 n) == floor (log_2 (2*n-1))
    

    as can easily be seen. Then to find the logarithm to the base b, we compute the logarithm to base b² and adjust:

    log2 :: Int -> Int
    log2 1 = 0
    log2 2 = 1
    log2 n
        | n < 1     = error "Argument of logarithm must be positive"
        | otherwise = fst $ doLog 2 1
          where
            m = 2*n-1
            doLog base acc
                | base*acc > m = (0, acc)
                | otherwise = case doLog (base*base) acc of
                                (e, a) | base*a > m -> (2*e, a)
                                       | otherwise  -> (2*e+1,a*base)
    

    A simpler algorithm that needs more steps would be to simply iterate, multiplying with 2 in each step, and count, until the argument value is reached or surpassed:

    log2 :: Int -> Int
    log2 n
        | n < 1     = error "agument of logarithm must be positive"
        | otherwise = go 0 1
          where
            go exponent prod
                | prod < n  = go (exponent + 1) (2*prod)
                | otherwise = exponent
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While learning on anonymous methods, i've found the following example on the internet: namespace
While learning Prolog, I'm trying to solve the following problem, using accumulators: Write a
While learning Sitecore I have found that the majority of Sitecore sample code on
While learning the concept of copying members, the book gives the following statement. In
While learning Rails, I found this example in the Sam Ruby's book: app/controllers/line_items_controller.rb def
While learning an example from learn-you-a-haskell which right triangle that has integers for all
i just got some more questions while learning PHP, does php implement any built
MOV is probably the first instruction everyone learns while learning ASM. Just now I
So I found some similarities between arrays and set notation while learning about sets
While learning .net (by c#) i found 5 ways for checking equality between objects.

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.