User Tools

Site Tools


Sidebar

projects

ntr0 (due 20200826)
pct1 (bonus; due 20200821)
wcp1 (due 20200821)
fwf0 (due 20200826)
pct2 (due 20200826)
wcp2 (due 20200826)
sof0 (due 20200902)
pct3 (bonus; due 20200902)
wcp3 (due 20200902)
lob0 (due 20200909)
pct4 (bonus; due 20200909)
wcp4 (due 20200909)
dow0 (due 20200916)
pct5 (bonus; due 20200916)
wcp5 (due 20200916)
afn0 (due 20200923)
pct6 (due 20200923)
wcp6 (due 20200923)
cbf0 (due 20200930)
pct7 (bonus; due 20200930)
wcp7 (due 20200930)
sam0 (due 20201007)
pct8 (due 20201007)
wcp8 (due 20201007)
cnv0 (due 20201014)
gfo0 (due 20201014)
pct9 (bonus; due 20201014)
wcp9 (due 20201014)
cnv1 (due 20201021)
pctA (due 20201021)
wcpA (due 20201021)
cos0 (due 20201028)
pctB (bonus; due 20201028)
wcpB (due 20201028)
pctC (due 20201104)
wcpC (due 20201104)
pctD (bonus; due 20201111)
wcpD (bonus; due 20201111)
pctE (bonus; due 20201118)
wcpE (bonus; due 20201118)
eoce (due 20201125)
haas:fall2020:cprog:projects:nikhilam

Corning Community College

CSCS1320 C/C++ Programming

~~TOC~~

Project: MENTAL MATH (nikhilam: All from 9, the last from 10)

Objective

To implement a programmatic solution (ie simulation) of a real life process- the mental math trick of multiplying two numbers together that are near a common power of 10.

Prerequisites

In addition to the new skills required on previous projects, to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • ability to obtain input from the user
  • ability to make decisions using selection statements
  • ability to use variables and mathematical expressions to aid in processing

Scope

The allure of using (and learning) a programming language is to be able to effectively use it to solve problems, which in and of themselves are simulations of some process we can do in “the real world”.

In this case, we will be writing a program which will implement the mental math techniques for multiplying any two values together that are near a common power of 10.

Background

Mental Math constitutes an intersection of mental techniques and math- instead of utilizing a purely math-only solution, textual manipulations or simplifications in the computational process may take place enabling an individual to, once having learned the process, solve such problems in their head, and typically without the use of a calculating device.

The process in this case is one of numeric manipulation and simple (reduced) multiplication. To wit:

The Nikhilam Sutra

This is one of the sixteen Vedic Math sutras, which (translated) comes out to mean “all from nine, the last from ten”.

Applying this concept can be most immediately visible in the case of subtracting from a power of 10:

    100
   - 57 9-5=4; 7, being the last, is 10-7=3
   ----
     43

Is it really that easy? Let's try a larger example:

    10000
   - 2678
   ------

Again, remember “all from 9”… so the 2, 6, and 7 will be evaluated against 9, and the 8, being the last, will be evaluated against 10.

  • 2 + WHAT = 9?
  • 6 + WHAT = 9?
  • 7 + WHAT = 9?

Then:

  • 8 + WHAT = 10?

We see that:

  • 2 + 7 = 9
  • 6 + 3 = 9
  • 7 + 2 = 9
  • 8 + 2 = 10

So our answer is: 7322

The trick is to make sure we have enough digits to match up with the zeroes in the power of 10. If the number we are subtracting is smaller, we simply add leading zeroes (0 + 9 = 9):

           1000         1000
          -  52   =>   - 052
          -----        -----
                         948

Pretty neat, eh?

Nikhilam Multiplication

Multiplication using this principle is also rather straightforward, provided both numbers share a common power of 10 (as such, this is still considered a special case for multiplication– something where you can identify a pattern and apply it to perform the operation in a more optimized fashion than applying the general method).

The procedure is basically as follows:

  1. 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 “-3
  2. To the right of the bottom number, do the same: 94 is 6 short of 100, so a “-6” is put down
  3. Pick a number (top or bottom, doesn't matter) and add it to the other number's short value (97 + -6, or 94 + -3)
  4. 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
  5. 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 the common power is 100 in this case, the right hand side needs to be 2 digits)
  6. Concatenate the two together, and you get: 9118

Excess

And what about cases where the numbers are in excess of their common base? Same thing:

    103  +3     103 is 3 above 100
  x 107  +7     107 is 7 above 100
    === ===
  103+7 3*7     half cross add on the left, multiply excess numbers on right
    110  21     make sure right hand value is 2 digits (100 = 10 to the 2nd power)
    
    11021

Mixed

And how about mixed? One in excess, one short?

    106  +6      6 in excess of 100
  x  97  -3      3 short of 100
 ======  ===== 
 106+-3  +6*-3   same deal as always- half cross add, multiply differences
    103  -18
    

Now, because the right hand value is negative, we have a “borrow” operation that needs to happen… we essentially have the following problem to consider:

    103    100
   -  1   - 18
   ====   ====
    102     82 <-- all by nine, last by 10
    
    10282          and voila! Our answer.

Program

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:

  • prompt the user for the two numbers (input)
  • determine the common power of 10
  • perform the task (nikhilam multiplication process)
  • display the final value (output)

Execution

lab46:~/src/cprog/nikhilam$ ./nikhilam
Enter first number: 91
Enter second number: 99
91 x 99 = 9009
lab46:~/src/cprog/nikhilam$ 

The execution of the program is short and simple- obtain the input, do the processing, produce the output, and then terminate.

Reflection

Be sure to provide any commentary on your opus regarding realizations had and discoveries made during your pursuit of this project.

  • Do these tricks work (or can it be adapted to work) for one- and three-digit values?
  • How about 4 digits and beyond?

Submission

To successfully complete this project, the following criteria must be met:

  • Code must compile cleanly (no warnings or errors)
  • Output must be correct, and match the form given in the sample output above.
  • Code must be nicely and consistently indented (you may use the indent tool)
  • Code must implement solution using the mental math technique described above
  • Code must be commented
    • have a properly filled-out comment banner at the top
    • have at least 20% of your program consist of //-style descriptive comments
  • Output Formatting (including spacing) of program must conform to the provided output (see above).
  • Track/version the source code in a repository
  • Submit a copy of your source code to me using the submit tool.

To submit this program to me using the submit tool, run the following command at your lab46 prompt:

$ submit cprog nikhilam nikhilam.c
Submitting cprog project "nikhilam":
    -> nikhilam.c(OK)

SUCCESSFULLY SUBMITTED

You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.

haas/fall2020/cprog/projects/nikhilam.txt · Last modified: 2014/02/24 14:15 by 127.0.0.1