Skip to content

A more rigorous solution for Chapter 4, Exercise 8 #129

@alvinng4

Description

@alvinng4

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
*/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions