-
Notifications
You must be signed in to change notification settings - Fork 232
A more rigorous solution for Chapter 4, Exercise 8 #129
Copy link
Copy link
Open
Description
Here is my solution:
/*
* Sol: Yes, they are equivalent in C99, except for 0 00000 00000.
*
* Mathematically,
* 9 - ((x - 1) % 10) = (10 - (x % 10)) % 10, x = 1, 2, 3, ...
*
* Proof:
*
* Let the last digit of x be y.
* For y != 0,
* L. H. S. = 9 - ((x - 1) % 10)
* = 9 - (y - 1)
* = 10 - y
*
* R. H. S. = (10 - (x % 10)) % 10
* = (10 - y) % 10
* = 10 - y
*
* For y = 0 and x != 0,
* L. H. S. = 9 - ((x - 1) % 10)
* = 9 - 9
* = 0
*
* R. H. S. = (10 - (x % 10)) % 10
* = (10 - 0) % 10
* = 0
*
* However, note that for x = 0,
* L. H. S. = 9 - ((x - 1) % 10)
* = 9 - (-1)
* = 10
*
* R. H. S. = (10 - (x % 10)) % 10
* = (10 - 0) % 10
* = 0
*/
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels