Go Down

Topic: Neat math tricks on youtube. (Read 294 times) previous topic - next topic

GoForSmoke

The terms used to explain some of it are poor metaphors but what they do is solid.

First is shown using binary (mixed with decimal but not) to multiply and divide and then the same only a different way.  :)

To multiply 17 by 25

17     x     25         <-- left value then as binary, right value doubles every line
-------------------
1x1          25
2x0          50
4x0        100
8x0        200
16x1      400
------------------
= 25 + 400 = 425   <--- then add the right column values that the left has 1's not 0's

17 x 25 = 1 x 25 + 16 x 25 = 425

Division goes the other way as they show.

I haven't taken the time to proof the second method they show, need to work out the part of crossing out evens.

There are other fast math videos worth watching, algorithms to be gained!
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

TomGeorge

#2
Sep 01, 2017, 04:06 pm Last Edit: Sep 01, 2017, 04:10 pm by TomGeorge
Hi,
Where on youtube?
Please read the sticky at the top of  each forum list.... :) :)

17 x  25

17 x 100 = 1700  easy
17 x 50 = 850  easy 1/2 of 1700
17 x 25 = 425  easy 1/2 of 850

Thats how I'd do it, realizing 4 x25 =100 you only have to halve and halve, easy to do in your mind.
(Two binary bit shifts) :) :)

Anything multiplied by 50 or 25.. easy.

But I appreciate the gymnastics of @GoForSmokes example.

Tom.... :)
Everything runs on smoke, let the smoke out, it stops running....

GoForSmoke

Oops!  Youtube video title is An Easy Math Trick Nobody Will Show You

The guy is a bit of a nut but the first part math technique itself is solid.

Any value times 25, you have 25 as your proportion and you can relate binary to doubling proportions.

There are other fast math videos, all kinds. Vedic maths look particularly interesting.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

GoForSmoke

But did the Egyptians and Chinese really use binary? I don't know of what he says but neither used zeros and this doubling proportions math adds what's there with no need for what's not, and doubling is easy.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

GoForSmoke

#5
Sep 01, 2017, 11:26 pm Last Edit: Sep 01, 2017, 11:32 pm by GoForSmoke
Looked over the second trick in that video and got it.

You multiply two numbers by halving (right-shift) one and doubling (left-shift) the other and add the doubled column value to an accumulator when the halved column value is odd (& 1 == 1).

13 x 24 -- 13 is odd, total = 24    bit3  1
  6 x 48 --   6 is even, don't add   bit2  0
  3 x 96 --   3 is odd, total += 96   bit1  1
  1 x  192   1 is odd, total += 192 bit0  1
===========
total = 312

1011 is binary 13 decimal, it's a high to low bit version of the first method shown, still only adds proportions for bits that add to 13. There is no 48 to add in 312 as there is no 4 in 0b1011, the low bit is 0.

I wonder how it compares with the multiply we have now? Must take a crack at that one and find a similar divide!

PS - I like how this one starts right at one value, is there a quick way to pick the operand with the least bits set to cut the number of adds? Maybe always halve the smaller value to cut steps!

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

TomGeorge

#6
Sep 02, 2017, 01:45 am Last Edit: Sep 02, 2017, 01:46 am by TomGeorge
Hi

13 x 24

13 x 25
13 x 100   = 1300
13 X 50 = 650
13 x 25 = 325

so
13 x 24 = 325 -13 = 312

Sorry, its 9:30am Sat morning and first coffee of the day has soaked in?

Don't ask me why 25 type solutions, keep popping into my head like that.

Could it be having to do it by mentally and by hand in High School, we had slide rules and the only electronic hand held calculator was owned by our Applied Maths teacher, a simple scientific Canon model, not sure what it cost, but must have been a pretty penny.

Tom... :)


As we was tooorrtt in sckool.


24
x 13
=====
   72
+240
=====
 312
Everything runs on smoke, let the smoke out, it stops running....

allanhurst

13 x 24??

10 x 24 = 240
3x24 = 72
240+72  = 312.

What's the problem?

I had a colleague who could multiply two 4 digit numbers in his head as a party trick.

Neat

Allan

GoForSmoke

13 x 24 -- 13 is odd, total = 24    bit3  1
  6 x 48 --   6 is even, don't add   bit2  0
  3 x 96 --   3 is odd, total += 96   bit1  1
  1 x  192   1 is odd, total += 192 bit0  1
===========
total = 312
Slower than Arduino multiply for 32 bit values. MUCH slower.

Code: [Select]

#include "Arduino.h"

long bmult32( long smaller, long bigger ) // product must fit in long
{
  long total = 0;
  while( smaller > 0 )
  {
    if ( smaller & 1 )
    {
      total += bigger;
    }
    smaller >>= 1;
    bigger <<= 1;
  }
  return total;
}

long product;
long bproduct;
long paritycount;
long over16count;

unsigned long startUsec;
unsigned long endUsec;
unsigned long elapsedUsec;


void setup()
{
  Serial.begin( 115200 ); // if you can run faster with your IDE then do!
  Serial.println( F( "\n\nStartup!\n" ));
  Serial.print( F( "23 x 57 = " ));
  product = bmult32( 23, 57 );
  Serial.println( product );
  Serial.print( F( "633 x 15767 = " ));
  product = bmult32( 633, 15767 );
  Serial.println( product );
  Serial.print( F( "2100 x 17500 = " ));
  product = bmult32( 2100, 17500 );
  Serial.println( product );

  startUsec = micros();
  for ( long x = 2; x < 2000L; x++ )
  {
    for ( long y = x + 1; y < 3000L; y++ )
    {
      product = x * y;
      if ( product & 1 )
      {
        paritycount++;
      }
      if ( product > 0xFF )
      {
        over16count;
      }
    }
  }
  endUsec = micros();
  elapsedUsec = endUsec - startUsec;

  Serial.print( F( "\ntime in micros = " ));
  Serial.println( elapsedUsec );
  Serial.print( F( "odd parity count = " ));
  Serial.println( paritycount );
  Serial.print( F( "over 16 bit count = " ));
  Serial.println( over16count );

  paritycount = over16count = 0;
  startUsec = micros();
  for ( long x = 2; x < 2000L; x++ )
  {
    for ( long y = x + 1; y < 3000L; y++ )
    {
      bproduct = bmult32( x, y );
      if ( bproduct & 1 )
      {
        paritycount++;
      }
      if ( product > 0xFF )
      {
        over16count;
      }
    }
  }
  endUsec = micros();
  elapsedUsec = endUsec - startUsec;

  Serial.print( F( "\ntime in micros = " ));
  Serial.println( elapsedUsec );
  Serial.print( F( "odd parity count = " ));
  Serial.println( paritycount );
  Serial.print( F( "over 16 bit count = " ));
  Serial.println( over16count );
  Serial.println( );


  for ( long x = 2; x < 2000; x++ )
  {
    for ( long y = x + 1; y < 3000; y++ )
    {
      product = x * y;
      bproduct = bmult32( x, y );
      if ( product != bproduct )
      {
        Serial.print( x);
        Serial.print( F( " x " ));
        Serial.print( y );
        Serial.print( F( " ? " ));
        Serial.println( product );
        Serial.print( F( " != " ));
        Serial.println( bproduct );
      }
    }
  }

}

void loop()
{
}


Quote
Startup!


23 x 57 = 1311

633 x 15767 = 9980511

2100 x 17500 = 36750000


time in micros = 10057268

odd parity count = 998001

over 16 bit count = 0


time in micros = 61617356

odd parity count = 998001

over 16 bit count = 0
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

addem

#9
Sep 08, 2017, 07:47 am Last Edit: Sep 08, 2017, 07:53 am by addem
AFAIK Babylonians grouped numbers by 5s and 10s like the Romans, and used a base-60 system.  From whence there are 60 seconds in a minute, and 360 degrees in a circle's arc. 

GoForSmoke

Yeah, the Babylonians used base 60. 60 is evenly divisible by 2,3,4,5,6,10,12,15,20 and 30. They had no decimals or 0.

The practice of halving one side of a multiply while doubling the other is shown old. Speculation about is is speculation.

Move coins on a table and these old ways show themselves.



Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up