# 2020q3 Homework5 (quiz5) contributed by < `erickuo5124` > ###### tags: `2020進階電腦系統理論與實作` --- ## Q1 ```c= #include <stdio.h> #include <stdlib.h> double divop(double orig, int slots) { if (slots == 1 || orig == 0) return orig; int od = slots & 1; double result = divop(orig / 2, od ? (slots + 1) >> 1 : slots >> 1); if (od) result += divop(result, slots); return result; } ``` 原理: $$ \begin{split} \frac{orig}{slots} &= \frac{orig/2}{slots/2} \\ &=\frac{orig}{slots} \times \frac{slots+1}{slots+1} \\ &=\frac{orig/2}{(slots+1)/2} \times \frac{slots+1}{slots} = \frac{orig/2}{(slots+1)/2} \times (1+\frac{1}{slots}) \end{split} $$ 將除術與被除數同除以 2 來作除法運算。若被除數為奇數,上下同乘被除數+1 --- ## Q2