## Number Trick

General FreeBASIC programming questions.
albert
Posts: 5927
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

I found a way to square 2 digits with an add...

11 x 11 = 121
0101 <-- square each digit and make them all 2 digits
1 + 1 = 2 * 10 = 20
0101 + 20 = 121 <-- correct answer

23 x 23 = 529
0409 <-- square each digit and make them all 2 digits
3 + 3 = 6 x 20 = 120
0409 + 120 = 529 <-- correct answer

Instead of cross mul , you add the last digits and mul by the first digit and add..

If you squaring 43 , then you add 3 + 3 and mul by 40 and add it to the squared digits..
If you squaring 92 , then you add 2 + 2 and mul by 90 and add it to the squared digits..

Now , on to figure 3 digit squares..
albert
Posts: 5927
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

With 123 ( 3 digits )

123 x 123 = 15129

010409 <-- square each digit and make them all 2 digits..

15129
-10409
---------
4720

23 + 23 x 100 = 4600
3 + 3 x 20 = 120

4600 + 120 = 4720

10409 + 4720 = 15129 <-- correct answer...
albert
Posts: 5927
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

With 246 ( 3 digits )

246 x 246 = 60516

041636 <-- square each digit and make them all 2 digits..

60516
-41636
----------
18880

46 + 46 x 200 = 18400
6 + 6 x 40 = 480

18400 + 480 = 18880

41636 + 18880 = 60516 <-- correct answer
albert
Posts: 5927
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

1234 ( 4 digits )

1234 x 1234 = 1522756

01040916 <-- square each number and make them all 2 digits..

234 + 234 x 1000 = 468000
34 + 34 x 200 = 13600
4 + 4 x 30 = 240

With 2 digits we have 1 add
With 3 digits we have 2 adds..
With 4 digits we have 3 adds..

468000 + 13600 + 240 = 481840

1040916
+481840
-------------

So a million digits would require 999,999 adds... I got to invent a faster formula....
albert
Posts: 5927
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

Another trick, I just invented..

I only tried on these 2 numbers ( i''l have to play around with different numbers )

12 x 14 = 168

0104 <-- square first number 12 , and make each digit into 2 digits
0116 <-- square second number 14, and make each digit into 2 digits

104
116
------
220

12 + 14 = 26 x 2 = 52

220 - 52 = 168 <--- correct answer.

it only requires a x 2 multiply , other than the squaring part..( you can do the squaring part , on a million digits , in a couple seconds.. )

I'll have to try in on different 2 digits , and different lengths of digits.....
Stonemonkey
Posts: 649
Joined: Jun 09, 2005 0:08

### Re: Number Trick

@albert

Here's one I use on the 6502 since it doesn't have any multiply instruction, it uses pre calculated tables of squares so is quick to lookup with just some addition and subtraction

a*b=((a+b)²-(a-b)²)/4
albert
Posts: 5927
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

@StoneMonkey

I've filled a 500 page notebook in the last couple days , working on number manipulations...

The above 12 x 14 = 168 formula , only works on those 2 numbers.. I tried all different numbers and it doesn't work on other numbers..

I don't know how i do it ????
I come up with a formula using just the right numbers to make it work.. and other numbers don't work... I keep doing it..

Then i get the right answer , and go off all hog wild , celebrating.. And then it doesn't work on any other numbers...
Stonemonkey
Posts: 649
Joined: Jun 09, 2005 0:08

### Re: Number Trick

@albert

Why not try making it a rule not to celebrate until you've tested further, see if you can break what you're doing using other values, write a program to test your functions and get it to loop through every possible combination to test your function and count how many times it passes/fails.

I might do something like this:

Code: Select all

`'multiply using lookup table of squares'create global table of squares    dim shared as ushort table(0 to 511)    for i as long=0 to 511        table(i)=(i*i)shr 2    next'multiply 8 bits by 8 bits=16 bits (unsigned) using table    function mul(byval a as ubyte,byval b as ubyte)as ushort        return table(a+b)-table(abs(a-b))    end function'test    sub main        randomize timer        for i as long=0 to 20            dim as ubyte a=rnd*255,b=rnd*255            print a;"*";b;"=";mul(a,b);"       ERROR=";mul(a,b)-a*b        next    end submainsleepend`
albert
Posts: 5927
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

@StoneMonkey

The formulas I'm working on , i can't write a program , till I've got several that match a rule..
So i have to do the manipulations on paper , until i get several sets of numbers working , with a particular rule..

Once i figure out the rule , then i can write a program...
albert
Posts: 5927
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

I found an interesting formula..

44 x 44 = 1936

------------------------
66 x 66 = 4356
22 x 22 = 484
-----------------------

4356 - 484 = 3872 x .5 = 1936

It doesn't work with other squares..
fxm
Posts: 10052
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

### Re: Number Trick

albert wrote:I found an interesting formula..

44 x 44 = 1936

------------------------
66 x 66 = 4356
22 x 22 = 484
-----------------------

4356 - 484 = 3872 x .5 = 1936

It doesn't work with other squares..

Pay attention to your erroneous equality.
Rather:
4356 - 484 = 3872 = 2 * 1936

Let a = 66, b = 22, c = 44
a^2 - b^2 = (a - b) * (a + b)

a - b = c
a + b = 2 * c
Therefore:
a^2 - b^2 = c * (2 * c) = 2 * c^2
albert
Posts: 5927
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

@fxm

44 x 44 = 1936

------------------------
add 22 = 66 x 66 = 4356
sub 22 = 22 x 22 = 484
-----------------------

4356
- 484
--------
3872
x .5 or ( / 2 )
-------

I was trying to find a way to get the square of a even mul of 10..
And then divide by the difference..
But with big numbers the fraction to mul by equals would equal the number of original digits...

So if you want to find the square of 22 , you square 30 ( 30 - 22 = 8 ) and the mul by the fraction difference of 30 and 22...
fxm
Posts: 10052
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

### Re: Number Trick

I now think that I should not have answered you because it is useless. So I stop right away.
albert
Posts: 5927
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

I got the formula finished...

123 x 123 = 15129

010409 <-- square each digit and make them all 2 digits..

15129
-10409
----------
4720 <-- difference

100 x 20 = 2000
100 x 3 = 300
20 x 3 = 60

2360 x 2 = 4720 <-- our difference. Since were multplying 2 sets , we need to double the output

10409
+ 4720
----------
angros47
Posts: 1790
Joined: Jun 21, 2005 19:04

### Re: Number Trick

Listen, Albert: if you want a formula to square numbers, use this:

Code: Select all

`Exp(Log(X)*2)`

It works. Now, please, stop posting non-working pieces of code.