Bifalgorithm post · Computer science one · Computing a Logarithm

How can the logarithm of a number be computed in an arbitrary base?

How can the logarithm of a number be computed in an arbitrary base? (Computer science one · Computing a Logarithm) — "16.3. Examples 16.3.1. Computing a Logari…

Ideas in this flowchart

  • Computing a logarithm in an arbitrary base
  • The logarithm of x is the exponent to which a base must be raised to obtain x.
  • The natural logarithm ln(x) uses the base e = 2.71828....
  • Logarithms can use any base b > 1.
  • Examples of arbitrary bases include log₂(x) and logπ(x).
  • The program prompts the user for a number x and a base b.
  • The change-of-base formula is log_b(x) = log_a(x) / log_a(b).
  • Computing one base makes it possible to compute any base b.
  • The standard library provides log() to compute the natural logarithm.
  • If a solution already exists for a similar problem, use it.
  • The natural logarithm solution is adapted to arbitrary bases using the change-of-base formula.
Feed/

How can the logarithm of a number be computed in an arbitrary base?

No cover

Channel

Computer science one

1 page

"16.3. Examples 16.3.1. Computing a Logarithm The logarithm of x is the exponent that some base must be raised to get x. The most common logarithm is the natural logarithm, ln(x) which is base e = 2.71828.... But logarithms can be in any base b > 1. What if we wanted to compute log₂(x)? Or logπ(x)? Let’s write a program that will prompt the user for a number x and a base b and computes log_b(x). Arbitrary bases can be computed using the change of base formula: log_b(x) = log_a(x) / log_a(b). If we can compute some base a, then we can compute any base b. Fortunately we have such a solution. Recall that the standard library provides a function to compute the natural logarithm, log(). This is one of the fundamentals of problems solving: if a solution already exists, use it. In this case, a solution exists for a different, but similar problem (computing the natural logarithm), but we can adapt the solution using the change of base formula. In particular, if we have variables b (base) and x, we can compute log_b(x) using"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.