A modulo-N counter

A modulo-N counter counts up 0, 1, …, N − 1 and then goes back to 0. A decade counter is the case N = 10.

What to build

Simulate a counter that starts at 0 and print its value after each clock.

Requirements

  • The counter starts at 0 and adds 1 at every clock. After N − 1 it wraps to 0.
  • Print the values in binary with the same number of digits, enough for N − 1.
  • Print all the values on one line, separated by spaces.

Input

One line with N and the number of clocks t.

Output

One line with the t values.

Constraints

2 ≤ N ≤ 16, 1 ≤ t ≤ 20.

Example

Input:

10 12

Output:

0001 0010 0011 0100 0101 0110 0111 1000 1001 0000 0001 0010

Why it works

A counter with w flip-flops has 2ʷ states, but a modulo-N counter uses only N of them. To skip the others, the circuit resets itself when it reaches N, or loads 0 when it is at N − 1. The output pattern repeats every N clocks, so a mod-10 counter divides the clock frequency by 10.

Examples

Example 1 · Input
10 12
Expected output
0001 0010 0011 0100 0101 0110 0111 1000 1001 0000 0001 0010
Example 2 · Input
4 6
Expected output
01 10 11 00 01 10

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

The count after t clocks is t % N when the counter starts at 0.

Hint 2

Print each count as a binary number with enough bits for N − 1: width = max(1, (N - 1).bit_length()).

Hint 3

Print the sequence on one line, starting with the value after the first clock.

Solution

Show one correct solution (try it yourself first)
n, t = map(int, input().split())
width = max(1, (n - 1).bit_length())
count = 0
out = []
for _ in range(t):
    count = (count + 1) % n
    out.append(format(count, "0" + str(width) + "b"))
print(*out)

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Registers and counters →