This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
haas:spring2014:cprog:projects:nikhilam [2014/02/24 12:30] – [The Nikhilam Sutra] wedge | haas:spring2014:cprog:projects:nikhilam [2014/02/24 14:15] (current) – [Nikhilam Multiplication] wedge | ||
---|---|---|---|
Line 80: | Line 80: | ||
Pretty neat, eh? | Pretty neat, eh? | ||
====Nikhilam Multiplication==== | ====Nikhilam Multiplication==== | ||
- | Squaring | + | Multiplication using this principle |
- | * 5 squared (5< | + | {{ : |
- | * 8 squared (8< | + | |
- | While not outwardly a difficult | + | The procedure |
- | Finding a shortcut through | + | - To the right of the top number, put down its difference from the common power of 10 (100 in this case, which 97 falls 3 short of, hence the " |
+ | - To the right of the bottom number, do the same: 94 is 6 short of 100, so a " | ||
+ | - Pick a number (top or bottom, doesn' | ||
+ | - That result goes down as the left hand side of our answer, and barring any carries or deficiencies in the right hand side, may well be set | ||
+ | - For the right hand side, multiply those two short values together (-6 * -3 = +18). THAT is the right hand side (a point to consider: because | ||
+ | - Concatenate the two together, and you get: **9118** | ||
- | This particular trick relies on a subset of the squares | + | ===Excess=== |
+ | And what about cases where the numbers are in excess | ||
- | The operational scope of this trick will be just those two-digit values ending with 5: | + | < |
- | * 15 | + | 103 +3 103 is 3 above 100 |
- | * 25 | + | |
- | | + | === === |
- | * 45 | + | |
- | * 55 | + | |
- | * 65 | + | |
- | | + | |
- | * 85 | + | </ |
- | * 95 | + | |
- | ====Squaring double digit values ending with 5==== | + | ===Mixed=== |
- | The trick here is two-fold. | + | And how about mixed? One in excess, one short? |
- | One, we square the 5 to get 25. That is how our result will end (so bam! we now have our tens and ones place already solved) | + | < |
- | Next, we take the value in the tens place, and multiply | + | 106 +6 6 in excess of 100 |
+ | x 97 -3 3 short of 100 | ||
+ | | ||
+ | | ||
+ | 103 -18 | ||
+ | |||
+ | </ | ||
- | * 1's increment (1+1) is 2, so 1*2 | + | Now, because the right hand value is negative, we have a " |
- | * 2's increment (2+1) is 3, so 2*3 | + | |
- | * 3's increment (3+1) is 4, so 3*4 | + | |
- | * 4's increment (4+1) is 5, so 4*5 | + | |
- | * ... | + | |
- | * 9's increment (9+1) is 10, so 9*10 | + | |
- | + | ||
- | We take this result and append | + | |
- | + | ||
- | For example: | + | |
< | < | ||
- | 15 * 15 = 1*(1+1) 5*5 | ||
- | = 1*2 5*5 | ||
- | = 2 25 | ||
- | = 225 | ||
- | </ | ||
- | and: | + | 103 100 |
- | + | - | |
- | < | + | |
- | 75 * 75 = 7*(7+1) 5*5 | + | 102 |
- | = 7*8 | + | |
- | = 56 25 | + | |
- | = 5625 | + | |
</ | </ | ||
- | |||
=====Program===== | =====Program===== | ||
- | It is your task to write the program that will use the above method to compute | + | It is your task to write the program that will use the above method to multiply two numbers together (you can make the assumption that the user will only ever be putting in values close to a common base). |
Your program should: | Your program should: | ||
- | * prompt the user for the number | + | * prompt the user for the two numbers |
- | * perform the task (process) | + | * determine the common power of 10 |
+ | * perform the task (nikhilam multiplication | ||
* display the final value (output) | * display the final value (output) | ||
Line 149: | Line 144: | ||
<cli> | <cli> | ||
lab46: | lab46: | ||
- | Enter value: 34 | + | Enter first number: 91 |
- | 34 x 34 = 5625 | + | Enter second number: 99 |
+ | 91 x 99 = 9009 | ||
lab46: | lab46: | ||
</ | </ |