---
title: "Factorial single-purpose processor"
subject: "Embedded System"
chapter: 2
key: "semester_6/embedded_system:spp-factorial"
source: https://www.bctengineer.com/q/semester_6/embedded_system/spp-factorial
site: BCT Engineering
---

# Factorial single-purpose processor

Embedded System · Chapter 2: Hardware Design Issues

- Asked in: 2073 Magh, 8 marks
- Video: [Designing Single Purpose Processor To Calculate GCD (HCF).](https://www.youtube.com/watch?v=Wy344fMRnOo) — AFlick
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/embedded_system/spp-factorial
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-2

## Question

Design a single-purpose processor that calculates Factorial of an integer number 'n'. Start with a function computing the desired result, translate it into a state diagram and sketch a probable datapath.

## Short answer

The algorithm initializes F=1, I=2, then while I<=n multiplies F=F*I and increments I. Datapath includes registers N, I, F, multiplier, incrementer, comparator; controller is a 4-state FSM with input start and status le.

## Answer

**Assumption:** input n is an 8-bit unsigned integer; with a 16-bit result register the output is valid for 0 <= n <= 8 (8! = 40320). Use a wider output register for larger n.

#### Step 1: Function
```text
 Factorial(n):
   F = 1; I = 2
   while (I <= n):
       F = F * I
       I = I + 1
   output F
```

#### Step 2: State diagram (FSMD)
```text
 S0 --start=1 (N=n, F=1, I=2)--> S1 (test)
 S0 --start=0--> S0
 S1 --I<=N--> S2 (F=F*I, I=I+1) --> S1
 S1 --I>N---> S3 (out=F, done=1) --> S0
```
| State | Condition | Actions | Next |
|---|---|---|---|
| S0 wait | start = 0 | done = 0 | S0 |
| S0 | start = 1 | N = n, F = 1, I = 2 | S1 |
| S1 test | I <= N | none | S2 |
| S1 | I > N | none | S3 |
| S2 multiply | always | F = F * I, I = I + 1 | S1 |
| S3 output | always | out = F, done = 1 | S0 |

#### Step 3: Datapath
```text
  n -> [N] ------------> [ I <= N ] --> le (to controller)
                              ^
  [I] <- mux(2, I+1) ---------+
  [F] <- mux(1, F*I) <- [ x ] <- F, I
  F --> out
```
Components: registers N, I, F; a multiplier; an incrementer; a comparator (I <= N); two 2:1 muxes. Control signals: N_ld, I_sel, I_ld, F_sel, F_ld, out_en. Status: le.

#### Controller
4-state FSM (2-bit state register). Block diagram:
```text
 start, status flags --> +-------------+   +-----------+
 (from datapath)         | Next-state  |-->| State     |
                         | logic       |   | register  |
                         +------^------+   +-----+-----+
                                |   current state |
                                +-----------------+
                                                  v
                                           +-------------+
                                           | Output logic|--> control
                                           +-------------+    signals
```

**Trace n = 4:** F = 1; I = 2 -> F = 2; I = 3 -> F = 6; I = 4 -> F = 24; I = 5 > 4 -> output 24. (n = 0 or 1 outputs 1.)

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. When does the test state S1 go to S3 (output)?
   A. When I <= N
   B. When start = 0
   C. When F = 1
   D. When I > N
   Answer: D. Once I exceeds N all multiplications are done and F is output. (Key point: Loop condition)

2. What are F and I initialised to?
   A. F = 1, I = 2
   B. F = 0, I = 0
   C. F = n, I = 1
   D. F = 1, I = 0
   Answer: A. F starts at 1 and the product starts with I = 2 (multiplying by 1 changes nothing). (Key point: Initial values)

3. What does S2 do?
   A. F = F + I
   B. F = F * I and I = I + 1
   C. Outputs F
   D. Loads N
   Answer: B. State S2 multiplies and increments, then returns to the test. (Key point: Multiply state)

4. What is output for n = 4?
   A. 10
   B. 16
   C. 24
   D. 120
   Answer: C. F goes 1, 2, 6, 24 as I goes 2, 3, 4. (Key point: Trace n = 4)

## Related in Embedded System

- [Fibonacci single-purpose processor](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aspp-fibonacci) — Embedded System, chapter 2, asked 2×
- [Single-purpose processor for power](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aspp-power) — Embedded System, chapter 2, asked 2×
- [Dual-purpose processor: factorial and prime](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Adual-purpose-factorial-prime) — Embedded System, chapter 2
- [Design LCM single-purpose processor](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aspp-lcm-datapath-controller) — Embedded System, chapter 2
- [Sum of digits processor](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aspp-sum-of-digits) — Embedded System, chapter 2
- [Prime number checking processor](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aspp-prime-check) — Embedded System, chapter 2
