Easy Math Tip and LUA verification


Do you want to compute such numbers in two seconds? such as 85 x 85, 97 x 93, 23 x 27 … ?

Multiplication of two digits (from 10 to 99) can be solved quickly if the tens of both numbers is the same and the sum of each ones’ is ten.

For example, if ab is the first two digit (a is the tens and b is the ones), similarly,  ad is the second number. We will have:

b + d = 10

Thus ab*ad= (a * 10 + b) * (a * 10 + d) => 100*a*a + (b + d) * 10a + b * d => 100*a*a + 100a + b * d => 100*[a * (a + 1)] + b * d

So the quickest way to solve the multiplication is straightforward: multiply the tens and (tens plus 1), multiply the ones and combine these two numbers. For example, 23 x 27 ==> 2 * (2 + 1) and 3 x 7 ==> 621

We can verify this by the following LUA program, which also counts the number of two digits that satisfy this equation.

-- http://HelloACM.com

function Compute(a, b)
  m = math.floor(a / 10)
  u = a % 10
  v = b % 10
  return (m * (m + 1) * 100 + u * v)
end

correct = 0
wrong = 0

for i = 1,9  -- tens
do
    for j = 1,9  -- ones
    do
      a = i * 10 + j
      b = i * 10 + (10 - j)
      if Compute(a, b) == a * b then
        correct = correct + 1
      else
        wrong = wrong + 1
      end 
    end
end

print ("Correct = ", correct)
print ("Wrong = ", wrong)

It prints ‘correct = 81’ and ‘wrong = 0’.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
303 words
Last Post: C/C++ Frequent Pointer Mistakes
Next Post: Check Given String has Repeated Characters

The Permanent URL is: Easy Math Tip and LUA verification

Leave a Reply