Number Trick

General FreeBASIC programming questions.
deltarho[1859]
Posts: 2846
Joined: Jan 02, 2017 0:34
Location: UK

Re: Number Trick

Postby deltarho[1859] » Aug 30, 2020 20:36

Code: Select all

(n+1)^2 = n^2 + 2*n + 1
        = n^2 + (n + n + 1)
 
(n-1)^2 = n^2 - 2*n + 1
        = n^2 - (n + n - 1)


General form: (a+b)^2 =a^2 + 2*a*b + b^2
Last edited by deltarho[1859] on Aug 30, 2020 20:41, edited 1 time in total.
albert
Posts: 5951
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Number Trick

Postby albert » Aug 30, 2020 20:38

Now to work on skipping values....

Find the square of 6 , by knowing the square of 5 and 7 or ???.
Fine the square of 150 by Knowing the square of 100 and 200... Finding a "median" square value..
Last edited by albert on Aug 30, 2020 20:54, edited 2 times in total.
deltarho[1859]
Posts: 2846
Joined: Jan 02, 2017 0:34
Location: UK

Re: Number Trick

Postby deltarho[1859] » Aug 30, 2020 20:51

Code: Select all

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

Re: Number Trick

Postby albert » Aug 30, 2020 20:54

Developing the concept to work with huge numbers...
So , you can only use addition and subtraction.. and maybe single or double digit multiplys..
albert
Posts: 5951
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Number Trick

Postby albert » Aug 30, 2020 21:13

Me and Dodicat , were working on "Huge Number Squares" , back in "Crircles" and "Squares"

I've been , off and on again , working on it...Over the years..
deltarho[1859]
Posts: 2846
Joined: Jan 02, 2017 0:34
Location: UK

Re: Number Trick

Postby deltarho[1859] » Aug 30, 2020 21:18

So, prove the idea with maths first, then expand as necessary to eliminate multiplication and division.

What I have done above is well below degree level maths. Did you get that far in your maths education?
albert
Posts: 5951
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Number Trick

Postby albert » Aug 30, 2020 21:46

In high school i only took general math... I didn't get into Algebra or Geometry or trig or Calculus...

Back in "Circles" I invented a way to square numbers.. But it was to slow to be useful for "Huge Numbers".

The idea i came up with is:

12 x 12 = 144

Square each number ( 1 x 1 = 1 * 10 = 10 ) + ( 2 x 2 = 4 ) makes 14
cascade the value to it's self
--14
14
----
154

Then you step through the original number 12 and subtract the square of the absolute difference..
12 =
(1 - 2 ) = 1 ( squared = 1 ) x 10 = 10 <-- our subtractor

154 - 10 = 144

if your working with huge numbers... 123 you have to do 1-2 , 1-3 , 2-3
1234 you have to do 1-2 , 1-3 , 1-4 and 2-3 , 2-4 and 3-4

I called the method "Squares Over" , because the cascaded value is over by whole squares of the difference in the digits..

another square:

18 x 18 = 324

8 * 8 = 64 + 1 x 1 = 1 out = 74
--74
74
----
814

abs( 1-8 ) = 7 ^ 2 = 49 * 10 = 490
814
-490
------
324 <-- correct answer..

The cascaded adding and the looping through the big number picking out the squares over , it was too slow to be used for big numbers..

But the concept falls into my , needs of addition , subtraction and single digit multiplys...
Last edited by albert on Aug 30, 2020 22:46, edited 1 time in total.
deltarho[1859]
Posts: 2846
Joined: Jan 02, 2017 0:34
Location: UK

Re: Number Trick

Postby deltarho[1859] » Aug 30, 2020 22:43

albert wrote:didn't get into Algebra or Geometry or trig or Calculus...

You need to 'get into' Algebra. I won't give you any links - the internet is awash with sites from Basic Algebra and above. It would be greatly beneficial to devote some time to learning just the basics - if you cannot prove the concept of an idea you could end up wasting a lot of time.

Code: Select all

(n+1)^2 = n^2 + 2*n + 1
        = n^2 + (n + n + 1)

That proved a concept.
albert
Posts: 5951
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Number Trick

Postby albert » Aug 30, 2020 22:55

My "Squares Over" concept above works with all digit lengths...

10 x 10 = 100

0 x 0 = 0
10 x 10 = 10
10 + 0 = 10

cascade
--10
10
-----
110
-10 <-- squared difference of 1 and 0 = 1 * 10 = 10
-----
100 <--- correct answer

11 x 11 = 121

1 x 1 = 1
1 x 1 = 1
tot = 11
cascaded
--11
11
-----
121
- 0 <-- squared difference of 1 and 1
-----
121 <-- correct answer


Me and Dodicat already tested it on millions of digits , and it's always correct....
albert
Posts: 5951
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Number Trick

Postby albert » Aug 30, 2020 23:06

You can find the "Squares Over" program on the "General" board under "Circles" topic and search for "Squares Over"...


@deltarho[1859]

With your math skills , can you find a faster way to cascade add , a million digits??? If you know all the million digits are exactly the same??

----100
--100
-100
-------
11100 except with a million of cascades of a million digits...

two digit numbers only require two cascades.. a million digits would require a million cascades...
albert
Posts: 5951
Joined: Sep 28, 2006 2:41
Location: California, USA

Squares Over

Postby albert » Aug 30, 2020 23:25

Here's a 3 digit one to help explain it..

121 x 121 = 14,641

1x1 = 1
20 x 2 = 40
100 x 1 = 100 ( tot = 141 )

cascade add
-----141
---141
-141
-------
15,651

original number 1-2-1
1 - 2 = 1 ( third place so we add 3 zeros ) 1000
1 - 1 = 0
2 - 1 = 1 ( second place so we add 1 zero ) 10
tot = 1010
15651
-1010
---------
14,641 <-- correct answer

It's always correct every time with every number...

I just couldn't figure out how to do the "cascade add" with big-nnumbers , it was too slow to use...
albert
Posts: 5951
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Number Trick

Postby albert » Aug 31, 2020 0:16

I found the program on my hard drive....

"Squares Over"

( Only works with the 32 bit version of FB ) ( The multiplier uses uinteger and on 64 bit FB , uniteger is 64 bits instead of 32 )
( i guess i should make the uintegers to ulongs?? )

Code: Select all

screen 19

'written in FreeBasic for Windows
'http://www.freebasic.net
'written by Albert Redditt and Dodicat from Scottland on the FreeBasic forum under "General" / "circles"

Declare Function plus(NUMber1 As String,NUMber2 As String) As String
Declare Function minus(number1 As String,number2 As String) As String
declare function multiplier(byref num1 as string, byref num2 as string) as string

'Your 2 digit squares set into a function
dim shared as string c1,total1,total2,Remainder1,remainder2 'share these with test code
Function _square(s1 As String,s2 as string) As String
    Dim As String num1=s1,num2=s2
    dim as string a1,b1',c1
    dim as string a2,b2,c2
    dim as ulongint n,n1
   
    b1="0"
    For n = 0 to len(num1)-1 step 1
        a1 = Str( (num1[n]-48)^2 )
        b1 = plus(b1+"0",a1)
    Next n
    c1 = b1
    if len(num1)>=2 then
        for n = 1 to len(num1)-1
            b1 = plus(b1+"0",c1)
        next
    end if
   
    total1 = b1
    Remainder1 = "0"
    cls
    for n1 = 2 to len(num1) step 1
        for n = 1 to n1 step 1
            color  7,0 : locate 1,1       : print "total of shift and adds = " ; total1
            color  1,7 : locate 2,1       : print " original number        = " ; num1
            color 14,1 : locate 2,26 + n1 : print mid(num1,n1,1)
            color 15,3 : locate 2,26 + n  : print mid(num1,n ,1)
           
            a1 = str(abs( val(mid(num1,n1,1)) - val(mid(num1,n ,1)) ) ^2 )
            'a1 = str( abs( cint(num1[n1-1]-48) - cint(num1[n-1]-48) )^2 )
            a1 = a1 + string(len(num1)+len(num1)-n-n1,"0")
           
            color 7,0 : locate 3,27       : print string(80-27," ");
            color 7,1 : locate 3,27       : print string(len(Remainder1)-len(a1)," ");  a1
            color 7,0 : locate 4,27       : print string(80-27," ");
            color 7,0 : locate 4,1        : print "Value over total by     = " ; Remainder1
           
            Remainder1 = plus(Remainder1,a1)
           
            sleep 2000
        next
    next
    b1 = minus(b1,Remainder1)
   
    Return b1

End Function

'*********** EXAMPLE (TWO DIGITS ONLY)  **********************
dim as double t1,t2,t_total
dim as ulongint digits_processed
Dim As String c,s1,multiplied
dim as ulongint inc
dim as string increment="1"
do
    s1=""
    for inc = 1 to rnd*30+1 step 1
        s1=s1+str(int(rnd*9))
    next
    if left(s1,1) = "0" then mid(s1,1,1) = str(rnd*9+1)

    t1 = timer

    c = _square(s1,s1)
   
    t2 = timer
    t_total += t2-t1
    digits_processed += len(s1)
   
    multiplied = multiplier(s1,s1)
    multiplied = left(multiplied,len(multiplied)-1)
   
    if c <> multiplied then
        color 12
        print
        print "ERROR "
        color 7
        print s1
        print "                  " ; multiplied
        print
        print "Calculated      = " ; c
        print "Difference      = " ; minus(c,multiplied)
        print "time to process = " ; t_total
        sleep
        'exit do
    else
        do
            if len(Remainder1) < len(total1)then Remainder1 = " "+Remainder1
        loop until len(Remainder1) = len(total1)
        print "number         = " ; s1
        print "_square result = " ; total1
        print "Sqr Over       = " ; Remainder1
        print "Result         = " ; c
        print "actual num^2   = " ; multiplied
        print
        print "No errors so far!"
        print "Time to process  (seconds) = " ; t_total
        print "Number of digits processed = " ; digits_processed
        t_total = 0
        digits_processed = 0
        print "( press a space to pause | pause and then esc to exit ) "
        print
        sleep(10000)
    end if
   
    if inkey = chr(32) then
        color 14
        print "!!!!~~PAUSED~~!!!!  (press esc to exit) "
        sleep
        print
        color 7
    end if
    if inkey = chr(27) then exit do
loop
color 11
print "!!!!~~FINISHED~~!!!!"
color 7
sleep
END

'=======================================
'=======================================
' **************************************
' Dodicats plus and minus (integer) functions
Function plus(NUMber1 As String,NUMber2 As String) As String
    Dim As String NUM1,num2
    Swap num1,number1
    Swap num2,number2
               
        Dim As Long lenf,lens
        Dim As Byte flag
        Dim As String part1,part2
         'set up tables
            Dim As Ubyte Qmod(0 To 19)
            Dim bool(0 To 19) As Ubyte

For z As Integer=0 To 19
    Qmod(z)=cubyte(z Mod 10+48)
    bool(z)=cubyte(-(10<=z))
Next z

'macro insert a character into a string unused yet
#macro insert(s,char,position)
 part1=Mid$(s,1,position-1)
 part2=Mid$(s,position)
 s=part1+char+part2
 #endmacro
 
 #macro finish(three)
  three=Ltrim(three,"0")
        If three="" Then Return "0"
       If flag=1 Then Swap NUM2,NUM1
       Swap num1,number1:Swap num2,number2
       Return three
       Exit Function
 #endmacro
 lenf=Len(NUM1)
 lens=Len(NUM2)
 If lens>lenf Then
 Swap NUM2,NUM1
 Swap lens,lenf
 flag=1
 Endif

       
        'lenf=Len(NUM1)
        'lens=Len(NUM2)
       ' maxlen=lenf
        Dim diff As Long=lenf-lens-Sgn(lenf-lens)
        Dim As String two,three',one
        'swap three,("0"+NUM1)
        three="0"+NUM1
        two=String(lenf-lens,"0")+NUM2
        'one=NUM1
        Dim As Long n2
        Dim As Ubyte addup,addcarry
        Dim As Ubyte ten=10
        Dim As Ubyte ninetysix=96
        Dim As Ubyte fortyeight=48
        Dim As Ubyte zero=0
        addcarry=zero
           
         For n2=lenf-1 To diff Step -1
          addup=two[n2]+NUM1[n2]-ninetysix
            three[n2+1]=Qmod(addup+addcarry)
            addcarry=bool(addup+addcarry)
        Next n2
       
        If addcarry=zero Then
        finish(three)
        Endif
        If n2=-1 Then
        three[0]=addcarry+fortyeight
         finish(three)
        Endif

        For n2=n2 To 0 Step -1
             addup=two[n2]+NUM1[n2]-ninetysix
               three[n2+1]=Qmod(addup+addcarry)
            addcarry=bool(addup+addcarry)
        Next n2
        three[0]=addcarry+fortyeight
    finish(three)
End Function
'===============================================================
'===============================================================
Function minus(number1 As String,number2 As String) As String
    Dim As String copyfirstnum=number1,copysecondnum=number2
        Dim maxlen As Long             
        Dim As Long lenf,lens
        Dim sign As String * 1
        Dim As String part1,part2
        Dim bigger As Byte
         'set up tables
            Dim As Ubyte Qmod(0 To 19)
            Dim bool(0 To 19) As Ubyte

For z As Integer=0 To 19
    Qmod(z)=cubyte(z Mod 10+48)
    bool(z)=cubyte(-(10>z))
Next z
         #macro compare(s,v)
If Len(s)>len(v) Then bigger= -1:Goto fin
    If Len(s)<len(v) Then bigger =0:Goto fin
    If s>v Then
        bigger=-1
    Else
        bigger= 0
    End If
    fin:
#endmacro
'macro insert a character into a string
#macro insert(s,char,position)
 part1=Mid$(s,1,position-1)
 part2=Mid$(s,position)
 s=part1+char+part2
 #endmacro
 
 
compare(copysecondnum,copyfirstnum)
If bigger Then
sign="-"
Swap copysecondnum,copyfirstnum
Endif
       
        lenf=Len(copyfirstnum)
        lens=Len(copysecondnum)
       
        maxlen=lenf
        Dim diff As Long=lenf-lens-Sgn(lenf-lens)
        Dim As String one,two,three
     
        three=copyfirstnum
       
        two=String(lenf-lens,"0")+copysecondnum
        one=copyfirstnum
        Dim As Long n2
        Dim As Ubyte takeaway,subtractcarry
        Dim As Ubyte ten=10
        Dim z As Long
        subtractcarry=0
        Do
         For n2=maxlen-1 To diff Step -1
             'cc=cc+1
           
           takeaway= one[n2]-two[n2]+ten-subtractcarry
           three[n2]=Qmod(takeaway)
           
            subtractcarry=bool(takeaway)
           
        Next n2
        If subtractcarry=0 Then Exit Do
        If n2=-1 Then Exit Do
        For z=n2 To 0 Step -1
            takeaway= one[z]-two[z]+ten-subtractcarry
            three[z]=Qmod(takeaway)
           
            subtractcarry=bool(takeaway)
            Next z
        Exit Do
        Loop
        three=Ltrim(three,"0")
        If three="" Then Return "0"
       Return sign+three
       
End Function
'==================================================
'==================================================
'**************************************************
'Albert's multiply function
function multiplier(byref num1 as string, byref num2 as string) as string
    dim as string number1,number2
    dim as string answer,outtext
    dim as string int1,frac1,int2,frac2
    dim as ulongint dec,dec1,len1,len2
    dim as string str1
    dim as string sign1,sign2,outsign
   
    number1 = num1
    number2 = num2
   
    sign1 = left(number1,1)
    if sign1 = "+" or sign1 = "-" then number1 = mid(number1,2) else sign1 = ""
   
    sign2 = left(number2,1)
    if sign2 = "+" or sign2 = "-" then number2 = mid(number2,2) else sign2 = ""
   
    if (sign1 = sign2) then outsign = ""
    if (sign1 <> sign2) then outsign = "-"
   
    dec = instr(1,number1,".")
    if dec > 0 then
        int1 = left(number1,dec-1)
        frac1 = mid(number1,dec+1)
    else
        int1 = number1
        frac1 = ""
    end if
   
    dec = instr(1,number2,".")
    if dec > 0 then
        int2 = left(number2,dec-1)
        frac2 = mid(number2,dec+1)
    else
        int2 = number2
        frac2 = ""
    end if

    dec = len(frac1)+len(frac2)
    number1 = int1+frac1
    number2 = int2+frac2

    'swap numbers so that bigger number is number1 and smaller is number2
    if len(number2) > len(number1) then swap number1,number2
    if len(number1) = len(number2) then
        if val(left(number2,1)) > val(left(number1,1)) then swap number1,number2
    end if

    'make numbers equal multiple of 4 bytes
    do
        str1 = str(len(number1)/4)
        dec1 = instr(1,str1,".")
        if dec1 <> 0 then number1 = "0" + number1
    loop until dec1 = 0
    do
        str1 = str(len(number2)/4)
        dec1 = instr(1,str1,".")
        if dec1 <> 0 then number2 = "0" + number2
    loop until dec1 = 0
   
    'convert the numeric strings to use pointers
    'convert number1
    dim as uinteger ptr uip1
    uip1 = cptr(uinteger ptr,strptr(number1))
    dim as Uinteger val1
    for a as integer = 0 to len(number1)-1 step 4
        val1 = ((number1[a]-48)*1000) + ((number1[a+1]-48)*100) + ((number1[a+2]-48)*10) + (number1[a+3]-48)
        *uip1 = val1
        uip1+=1
    next
    'convert the numeric strings to use pointers
    'convert number2
    dim as uinteger ptr uip2
    uip2 = cptr(uinteger ptr,strptr(number2))
    dim as Uinteger val2
    for a as integer = 0 to len(number2)-1 step 4
        val2 = ((number2[a]-48)*1000) + ((number2[a+1]-48)*100) + ((number2[a+2]-48)*10) + (number2[a+3]-48)
        *uip2 = val2
        uip2+=1
    next

    'create accumulator
    answer = "0000" + string(len(number1)+len(number2),"0")
    'dimension vars for the mul
    dim as integer ptr start1,stop1,start2,stop2 'use longint because the pointers go negative
    dim as uinteger ptr outplace
    dim as ulongint carry
    dim as string result
    dim as integer ptr inc1,inc2
    dim as ulongint total
    dim as ulongint blocknumber1 = len(number1)/4
    dim as ulongint blocknumber2 = len(number2)/4
    dim as ulongint outblocks = len(answer)/4
   
    'set initial accumulator place
    outplace = cptr(uinteger ptr , strptr(answer)) + (outblocks - 1)
    'set initial pointers into number1
    start1 = cptr(integer ptr , strptr(number1))+(blocknumber1-1)
    stop1 =  cptr(integer ptr , strptr(number1))+(blocknumber1-1)
    'set initial pointers into number2
    start2 = cptr(integer ptr , strptr(number2))+(blocknumber2-1)
    stop2 =  cptr(integer ptr , strptr(number2))+(blocknumber2-1)
    'zero the carry
    carry = 0
   
    'begin looping thru strings multiplying
    do
        'set total to zero
        total = 0
        'we are going to be incrementing thru number2 while decrementing thru number1
        'working in opposite directions from start1 to stop1 and start2 to stop2
        'inc1 works from right to left in the number1 string
        'inc2 works from start2 to stop2 working from left to right, with start2 decrementing each loop.
        inc1 = start1
        inc2 = start2
        do
            'total += ( (number2[inc2]-48) * (number1[inc1]-48) )
            total += *inc2 * *inc1
            inc2 += 1
            inc1 -= 1
        loop until inc2 = stop2+1
       
        total = total + carry
        carry = 0
       
        result = str(total)
        carry = val(left(result,len(result)-4))
        result = right(result,4)
        *outplace = val(result)

        outplace -= 1
       
        'each loop we need to decrement stop1
        'if stop1 goes negative we reset it to zero and decrement stop2
        stop1 -= 1
        if stop1 < cptr(integer ptr,strptr(number1)) then
            stop1 += 1
            stop2 -=1
            if stop2 < cptr(integer ptr,strptr(number2)) then stop2 += 1
        end if
        'each loop we decrement start2 to the left
        start2 -= 1
        'if start2 goes negative we reset it to zero and decrement start1
        'start1 is the rightmost digit of number1 we need to multiply
        if start2 < cptr(integer ptr,strptr(number2)) then
            start2 += 1
            start1 -= 1
            if start1 < cptr(integer ptr,strptr(number1)) then start1 += 1
        end if
   
    loop until outplace = cptr(uinteger ptr,strptr(answer))+1
   
    'put in the carry at the end
    if carry > 0  then *outplace = carry else *outplace = 0
   
    'convert answer back to ascii
    for a as ulongint = 1 to outblocks-1 step 1
        val1 = *outplace
        outplace +=1
        outtext = outtext + right("0000" + str(val1),4)
    next   
   
    'put in the decimal point
    outtext = left(outtext,len(outtext)-dec) + "." +  mid(outtext,(len(outtext)-dec)+1)
    'trim leading zeros
    outtext = trim(outtext,"0") 'if multiplying by 1, we have a zero in front.
    outtext = outsign + outtext
return outtext

end function

deltarho[1859]
Posts: 2846
Joined: Jan 02, 2017 0:34
Location: UK

Re: Number Trick

Postby deltarho[1859] » Aug 31, 2020 0:48

@albert

I am sorry albert but I cannot get excited about squaring huge numbers; I have my projects as well. One goal is to write some Blake2b code to initialize, stream chunks of data and then finalize. Blake3 is supposed to be even faster. I would like to use it in Encrypternet which currently uses SHA256 in a separate thread of execution on decryption - SHA256 takes longer than AES128-CBC.
albert
Posts: 5951
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Number Trick

Postby albert » Aug 31, 2020 1:30

@deltarho[1859]

If your working on encryption... Take a look at my "Vari_Cyph" on the "Projects" section of the forum...

viewtopic.php?f=8&t=27407&p=258031&hilit=Vari_Cyph#p258031

It scrambles the message bits into a bunch of garbage...
You can select a message chunk of 8 to 1024 bytes..
Then you select a garbage multiplier of 1 to 128 times the message bits..

Far better encryption than what's currently being used around the world..

It outputs binary values 0000 to 1111 ( hex ) and then turns them into a chr().. But you can change the output chrs , to any 16 characters you want..


With "Vari_Cyph"

The key is the scramble order..

There's a button , to copy the output back to the input..
So you can cypher multiple times with the same or different keys..

If you cypher 3 times with different keys , then you need to decypher in the proper order , with the correct keys at each step..
Last edited by albert on Aug 31, 2020 2:03, edited 1 time in total.
albert
Posts: 5951
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Number Trick

Postby albert » Aug 31, 2020 1:59

It's impossible to decypher "Vari_Cyph"
Last edited by albert on Aug 31, 2020 2:04, edited 1 time in total.

Return to “General”

Who is online

Users browsing this forum: No registered users and 11 guests