A processor with k address lines can reach 2ᵏ memory cells. Turn the number of lines into a count of addresses and a size.
What to build
A program that reads k and prints how many addresses k lines reach and the memory size when each address holds one byte.
Requirements
- Print
addresses Nwith N the exact number of addresses. - Print
size V Uwhere U is one ofB,KB,MB,GB,TBorPB(each 1024 times the one before), chosen so that V is a whole number from 1 to 1023.
Input
One line: k.
Output
Two lines.
Constraints
1 ≤ k ≤ 59.
Example
Input:
10
Output:
addresses 1024
size 1 KB
Why it works
Every address line is one bit of the address, so each line doubles the count. Because 2¹⁰ = 1024, every ten lines move one unit up (B, KB, MB, and so on) and the leftover lines give the number 1, 2, 4, up to 512.
Examples
10
addresses 1024 size 1 KB
32
addresses 4294967296 size 4 GB
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The number of addresses is 2 to the power k, written 2 ** k in Python.
Hint 2
Each 10 address lines multiply the size by 1024: use k // 10 to pick the unit and 2 ** (k % 10) for the number.
Solution
Show one correct solution (try it yourself first)
k = int(input())
units = ["B", "KB", "MB", "GB", "TB", "PB"]
print("addresses", 2 ** k)
print("size", 2 ** (k % 10), units[k // 10])
▶ Open this solution in the compiler, then press Visualize to watch it run step by step