Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ALGORITHM 1
- Input: a positive integer n
- Output: a sequence of bits representing the number in binary.
- For an input of n = 11, the output should be (1, 0, 1, 1).
- Step 0: start with an empty output sequence S = ()
- Step 1: if n is 0, return the sequence S.
- Step 2: divide n by 2 to get k with remainder r; r must be 0 or 1.
- Step 3: prepend r onto S. replace n with k.
- Step 4: go to step 1.
- ALGORITHM 2
- Input: a positive integer n
- Output: a sequence of bits representing the number in binary.
- For an input of n = 11, the output should be (1, 0, 1, 1).
- Step 0: start with an empty output sequence S = ()
- Step 1: divide n by 2 to get k with remainder r; r must be 0 or 1.
- Step 2: prepend r onto S. replace n with k.
- Step 3: if n is 1, return the sequence S.
- Step 4: go to step 1.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement