Logarithms and exponents
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
An exponent counts how many times you multiply; a logarithm counts it backwards. Follow one number, 64 = 2⁶: double from 1 six times to build it, then run a binary search that halves 64 items six times down to one. That count, 6, is log₂(64), and it is why searching a million sorted items takes only about 20 steps.
Exponents: repeated doubling
An exponent counts repeated multiplication. 2⁶ means six 2s multiplied: 2×2×2×2×2×2 = 64. Start from 1 and double six times. The exponent counts the doublings, and the board lights 1, 2, 4, 8, 16, 32, then 64 squares.
>>> 2 ** 6 64 >>> 2 * 2 * 2 * 2 * 2 * 2 64 >>> [2 ** i for i in range(7)] [1, 2, 4, 8, 16, 32, 64]
اقرأ الدرس خطوة بخطوة
Exponents: repeated doubling
An exponent counts repeated multiplication. 2⁶ means six 2s multiplied: 2×2×2×2×2×2 = 64. Start from 1 and double six times. The exponent counts the doublings, and the board lights 1, 2, 4, 8, 16, 32, then 64 squares.
>>> 2 ** 6 64 >>> 2 * 2 * 2 * 2 * 2 * 2 64 >>> [2 ** i for i in range(7)] [1, 2, 4, 8, 16, 32, 64]Why 2⁰ is 1
Walk the powers backwards and each step divides by 2: 2² = 4, 2¹ = 2, 2⁰ = 1. That is why 2⁰ is 1, not 0. One more step gives a fraction: 2⁻¹ = 1/2.
>>> 2 ** 0 1 >>> 2 ** -1 0.5 >>> 2 ** -3 # 1/8 0.125Multiply: exponents add
Multiplying powers of the same base adds their exponents: 2² × 2⁴ = 4 × 16 = 64 = 2⁶, and 2 + 4 = 6. Two doublings and then four more are six doublings in all.
>>> 2 ** 2 * 2 ** 4 64 >>> 2 ** (2 + 4) 64 >>> (2 ** 3) ** 2 # a power of a power multiplies 64A log undoes a power
A logarithm asks the reverse question. log₂(64) means: 2 to what power gives 64? Find 64 among the powers of 2. It sits in the 2⁶ cell, so log₂(64) = 6. A log always answers with an exponent.
>>> import math >>> math.log2(64) 6.0 >>> 2 ** 6 # the check 64Count the halvings
A log also counts halvings. To find item 22 among 64 sorted items, check the middle and keep the half that holds it. After six halvings one item is left, so log₂(64) = 6. This is binary search.
>>> lo, hi, n = 1, 64, 0 >>> while lo < hi: ... mid = (lo + hi) // 2 ... if 22 <= mid: hi = mid ... else: lo = mid + 1 ... n += 1 ... >>> lo, n (22, 6)Logs turn × into +
Exponents add when powers multiply, so logs turn multiplication into addition: log₂(4 × 16) = log₂ 4 + log₂ 16 = 2 + 4 = 6. This product rule works for any positive numbers, not just powers of 2.
>>> import math >>> math.log2(4) + math.log2(16) 6.0 >>> math.log2(4 * 16) 6.0Between two powers
Most numbers are not exact powers of 2. 1000 lies between 2⁹ = 512 and 2¹⁰ = 1024, so log₂(1000) ≈ 9.97. Its whole part, 9, plus one gives 10: the number of bits 1000 needs in binary.
>>> import math >>> math.log2(1000) 9.965784284662087 >>> bin(1000) '0b1111101000' >>> (1000).bit_length() 10Logs grow slowly
Logs grow very slowly. 2²⁰ = 1,048,576, just over a million, so log₂ of a million is about 19.9. Binary search through a million sorted items needs at most 20 checks. Reading them one by one could take a million.
>>> import math >>> 2 ** 20 1048576 >>> math.log2(1_000_000) 19.931568569324174 >>> math.floor(math.log2(1_000_000)) + 1 20Other bases
Logs work in any base. log₁₀(1000) = 3, because 10³ = 1000: count the zeros. Changing base only multiplies a log by a fixed number, about 3.32 from base 10 to base 2. So growth estimates often just write log n.
>>> import math >>> math.log10(1000) 3.0 >>> math.log2(1000) / math.log10(1000) 3.321928094887362 >>> math.log2(10) 3.321928094887362
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01What is 2⁵?
2⁵ means five 2s multiplied: 2 × 2 × 2 × 2 × 2 = 32. It is not 2 × 5 = 10.
02What is log₂(128)?
2⁷ = 128, so log₂(128) = 7. You can also count halvings: 128 → 64 → 32 → 16 → 8 → 4 → 2 → 1 is seven halvings.
03Which is equal to 2³ × 2⁴?
Multiplying powers of the same base adds the exponents: 3 + 4 = 7. Check: 8 × 16 = 128 = 2⁷.
04A sorted list has 1,000,000 items. About how many checks does binary search need, at most?
Each check halves what is left. 2²⁰ = 1,048,576 is just over a million, so at most ⌊log₂ 1,000,000⌋ + 1 = 20 checks.
05What does 2 ** -1 give in Python?
A negative exponent means divide: 2⁻¹ = 1/2 = 0.5. Each step down the powers of 2 divides by 2.
المسرد
- Exponent (power)
- How many times a number is multiplied by itself: in 2⁶ the base is 2 and the exponent is 6, so 2⁶ = 64.
- Base
- The number being multiplied in a power, or the number a logarithm counts in: 2 in 2⁶ and in log₂.
- Logarithm
- The inverse of a power. log₂(64) asks: 2 to what power gives 64? The answer is the exponent, 6.
- log₂ (lg)
- The base-2 logarithm, the one computer science uses most. It counts doublings or halvings; many books write it lg n.
- log₁₀
- The base-10 logarithm. For powers of ten it counts the zeros: log₁₀(1000) = 3.
- Binary search
- A way to find an item in a sorted list by checking the middle and keeping only the half that can hold it.
- Exponential growth
- Growth by repeated multiplying, such as doubling: 1, 2, 4, 8, … It gets huge fast, and its inverse, the log, grows very slowly.
الملاحظات والمصادر
- The exponent rules, for any base a > 0: aᵐ × aⁿ = aᵐ⁺ⁿ, (aᵐ)ⁿ = aᵐⁿ, a⁰ = 1 and a⁻ⁿ = 1/aⁿ. Each log rule is one of these read backwards: log(xy) = log x + log y.
- log₂ and 2ˣ undo each other: log₂(2ˣ) = x and 2 to the power log₂(x) is x again. A log is only defined for numbers greater than 0.
- Computer science books often write lg n for log₂ n. Changing base: log₂ x = log₁₀ x / log₁₀ 2, and 1 / log₁₀ 2 ≈ 3.32.
- For a whole number n ≥ 1, the bits needed to write it are ⌊log₂ n⌋ + 1 (round the log down, add one): 64 needs 7 bits (1000000) and 1000 needs 10.
- Use math.log2 and math.log10 for exact powers: math.log(1000, 10) divides two rounded logs and gives 2.9999999999999996, a tiny floating-point error.
- Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 3rd ed., section 3.2 Standard notations and common functions
- Lehman, Leighton & Meyer, Mathematics for Computer Science (MIT OCW 6.042J)
- Sedgewick & Wayne, Algorithms 4th ed., 3.1 Symbol tables (binary search: at most lg N + 1 compares)
- Python library: math (log2, log10, log)
- Python reference: The power operator (**)
- Python library: int.bit_length()
اختبار يتاح بعد الخطوة الأخيرة.