|
CS 3343/3341 Analysis of Algorithms Fall 2012 Recitation 10 Subset Sum, Etc. Answers |
Remaining Problems:
To calculate: a*b = 54524139 * 98646231 = 5378600810870109 Break a and b apart: a = 10^4*a1 + a0, b = 10^4*b1 + b0 a1=5452, a0=4139, b1=9864, b0=6231 Then a*b = (10^8 + 10^4)*a1*b1 + 10^4*(a1-a0)*(b0-b1) + (10^4+1)*a0*b0 Here are the 3 4-digit multiplications: a1*b1 = 5452*9864 = 53778528 a0*b0 = 4139*6231 = 25790109 (a1-a0)*(b0-b1) = (5452-4139)*(6231-9864) = (1313)*(-3633) = -4770129 a*b = (10^8 + 10^4)*a1*b1 + 10^4*(a1-a0)*(b0-b1) + (10^4+1)*a0*b0= 10^8*53778528 + 10^4*53778528 + 10^4*(-4770129) + 10^4*25790109 + 25790109= 5377852800000000 + 537785280000 + -47701290000 + 257901090000 + 25790109 = 5378600810870109 |
8-digit numbers: c = a * b = c 54524139 * 98646231 = 5378600810870109 Break up a and b into 4-digit pieces: a = a1∙10e4 + a0 = a 54520000 + 4139 = 54524139 b = b1∙10e4 + b0 = b 98640000 + 6231 = 98646231 c = a * b = (a1∙10e4 + a0) * (b1∙10e4 + b0) = c2∙10e8 + c1∙10e4 + c0 c2 = a1 * b1 = c2 5452 * 9864 = 53778528 c0 = a0 * b0 = c0 4139 * 6231 = 25790109 Use the fancy method for c1: c1 = (a1 + a0) * (b1 + b0) - (c2 + c0) = c1 9591 * 16095 - 79568637 = 74798508 c = c2∙10e8 + c1∙10e4 + c0 = c 5377852800000000 + 747985080000 + 25790109 = 5378600810870109 |