A memory with n address lines has 2ⁿ locations, and each location holds one word of b bits. So the size is 2ⁿ × b bits.
What to build
From the number of locations and the word size, find the address lines and the total size.
Requirements
- Print three numbers on one line: the number of address lines, the total number of bits, and the total number of bytes.
- A byte is 8 bits.
Input
One line with the number of locations L and the word size b in bits.
Output
One line with the three numbers.
Constraints
L is a power of 2 from 2 to 2²⁰, and b is a multiple of 8 from 8 to 64.
Example
Input:
1024 8
Output:
10 8192 1024
Why it works
To choose one of L locations you need enough address bits to count them, and n bits count 2ⁿ locations, so n = log₂ L. The memory has L words of b bits, so the total number of bits is their product. For example, 1024 locations of 8 bits need 10 address lines and hold 8192 bits, which is 1024 bytes, that is 1 KB.
Examples
1024 8
10 8192 1024
65536 16
16 1048576 131072
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The number of address lines is the number of bits needed for L locations: log2(L), since L is a power of 2.
Hint 2
The total number of bits is L × b. In bytes, divide by 8.
Hint 3
In Python, L.bit_length() - 1 gives log2(L) when L is a power of 2.
Solution
Show one correct solution (try it yourself first)
locations, bits = map(int, input().split()) lines = locations.bit_length() - 1 total = locations * bits print(lines, total, total // 8)
▶ Open this solution in the compiler, then press Visualize to watch it run step by step