Cody

# Problem 42307. Long-digit Subtraction

Solution 1483474

Submitted on 8 Apr 2018 by J. S. Kowontan
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
a = '99999'; b = '87654'; %---------- c = '12345'; assert(strcmp(long_digit_subtraction(a,b),c))

ans = 1 2 3 4 5

2   Pass
a = '99999'; b = '1234'; %---------- c = '98765'; assert(strcmp(long_digit_subtraction(a,b),c))

ans = 9 8 7 6 5

3   Pass
a = '99999'; b = '98765'; %---------- c = '1234'; assert(strcmp(long_digit_subtraction(a,b),c))

ans = 0 1 2 3 4

4   Pass
a = '99999'; b = '90909'; %---------- c = '9090'; assert(strcmp(long_digit_subtraction(a,b),c))

ans = 0 9 0 9 0

5   Pass
a = '11111'; b = '1234'; %---------- c = '9877'; assert(strcmp(long_digit_subtraction(a,b),c))

ans = 1 0 -1 -2 -3

6   Pass
a = '10000'; b = '1'; %---------- c = '9999'; assert(strcmp(long_digit_subtraction(a,b),c))

ans = 1 0 0 0 -1

7   Pass
a = '10000000'; b = '9999999'; %------------- c = '1'; assert(strcmp(long_digit_subtraction(a,b),c))

ans = 1 -9 -9 -9 -9 -9 -9 -9

8   Pass
a = '100000000000000000000000000000000000000000000000000000000000000000000'; b = '1'; %-------------------------------------------------------------------------- c = '99999999999999999999999999999999999999999999999999999999999999999999'; assert(strcmp(long_digit_subtraction(a,b),c))

ans = Columns 1 through 29 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 58 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 59 through 69 0 0 0 0 0 0 0 0 0 0 -1

9   Pass
a = '2135476542136876516521651162516'; b = '165267982494266414654661818517'; %------------------------------------ c = '1970208559642610101866989343999'; assert(strcmp(long_digit_subtraction(a,b),c))

ans = Columns 1 through 29 2 0 -3 0 2 1 -1 -4 -4 0 -3 -6 2 6 1 0 1 0 2 -1 -3 -3 0 -1 0 -7 5 -6 0 Columns 30 through 31 0 -1

10   Pass
a = '1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'; b = '1'; %------------------------------------------------------------------------------------------------------------------------------------------------------------------ c = '999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999'; assert(strcmp(long_digit_subtraction(a,b),c))

ans = Columns 1 through 29 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 58 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 59 through 87 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 88 through 116 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 117 through 145 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 146 through 157 0 0 0 0 0 0 0 0 0 0 0 -1

11   Pass
a = '1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'; b = '1'; %------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ c = '999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999'; assert(strcmp(long_digit_subtraction(a,b),c))

ans = Columns 1 through 29 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 58 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 59 through 87 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 88 through 116 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 117 through 145 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 146 through 174 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 175 through 203 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 204 through 232 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 233 through 261 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 262 through 290 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 291 through 319 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 320 through 348 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 349 through 377 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 378 through 406 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 407 through 435 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 436 through 464 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 465 through 493 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 494 through 522 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 523 through 551 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 552 through 580 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 581 through 609 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 610 through 638 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 639 through 667 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 668 through 696 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 697 through 725 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 726 through 754 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 755 through 781 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1

12   Pass
switch randi(4) case 1 a = '99999'; b = '87654'; c = '12345'; case 2 a = '99999'; b = '1234'; c = '98765'; case 3 a = '10000000'; b = '9999999'; c = '1'; case 4 a = '99999'; b = '90909'; c = '9090'; end assert(strcmp(long_digit_subtraction(a,b),c))

ans = 9 8 7 6 5

13   Pass
switch randi(4) case 1 a = '99999'; b = '98765'; c = '1234'; case 2 a = '10000'; b = '1'; c = '9999'; case 3 a = '99999'; b = '87654'; c = '12345'; case 4 a = '2135476542136876516521651162516'; b = '165267982494266414654661818517'; c = '1970208559642610101866989343999'; end assert(strcmp(long_digit_subtraction(a,b),c))

ans = Columns 1 through 29 2 0 -3 0 2 1 -1 -4 -4 0 -3 -6 2 6 1 0 1 0 2 -1 -3 -3 0 -1 0 -7 5 -6 0 Columns 30 through 31 0 -1

14   Pass
switch randi(4) case 1 a = '11111'; b = '1234'; c = '9877'; case 2 a = '10000000'; b = '9999999'; c = '1'; case 3 a = '99999'; b = '98765'; c = '1234'; case 4 a = '99999'; b = '87654'; c = '12345'; end assert(strcmp(long_digit_subtraction(a,b),c))

ans = 1 0 -1 -2 -3