Squares

General FreeBASIC programming questions.
Locked
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Richard's example a₀·x⁰ + a₁·x¹ + a₂·x² + a₃·x³
I have added another coefficient (5 in all), permutated them and shown the resulting graphs.(120 in total)
There is a strange kind of pattern (press space or escape)

Code: Select all

screen 20,32
#include "string.bi"
dim as double minx,maxx,miny,maxy,x
dim as integer xres,yres
screeninfo xres,yres
minx=-5
maxx=5

miny=-20
maxy=20

#macro sketch(_function,colour)
var cc=colour
For x =minx To maxx Step (maxx-minx)/1000
    Dim As Double x1=(xres)*(x-minx)/(maxx-minx)
    Dim As Double y1=(yres)*(_function-maxy)/(miny-maxy)
   if x=minx then Pset(x1,y1),cc else line -(x1,y1),cc
Next x
#endmacro

#macro axis(colour)
    line(0,(yres-(miny/(miny-maxy))*yres))-(xres,(yres-(miny/(miny-maxy))*yres)),colour 'x axis
        line(((minx/(minx-maxx))*xres),0)-(((minx/(minx-maxx))*xres),yres),colour 'y axis
        draw string(0,(yres-(miny/(miny-maxy))*yres)),format(minx),colour
        draw string(xres-8-8*(len(format((maxx)))),(yres-(miny/(miny-maxy))*yres)),format(maxx),colour
         draw string (xres/2,0),format(maxy),colour
        draw string (xres/2,yres-16),format(miny),colour
#endmacro

Sub Permutate(s As String,perm() as string,OptionalStop as string="")
    Dim As Integer p,i,j,result
    Dim As String s2=s
    Redim perm(0)
    For p1 As Integer =0 To Len(s2)-2
        For p2 As Integer =p1 + 1 To Len(s2)-1
            If s2[p1]>s2[p2] Then Swap s2[p1],s2[p2]
        Next p2
    Next p1
    Do
        p=p+1
        redim preserve perm(1 to p)
        perm(p)=s2
        if s2=OptionalStop then goto skip
        Do
            For i=Len(s2)-2 To 0 Step -1
                If s2[i] <s2[i+1] Then Exit For
            Next
            If i <0 Then Result=0:Exit Do
            j =Len(s2)-1
            While s2[j] <= s2[i]: j -=1 : Wend
            Swap s2[i], s2[j]
            i +=1
            j =Len(s2)-1
            While i <j
                Swap s2[i], s2[j]
                i +=1
                j -=1
            Wend
            result=-1:Exit Do
        Loop
    Loop Until result=0
    skip:
    Redim Preserve perm(1 To p)
End Sub

Function polyeval(Coefficients() As Double,Byval x As Double) As Double
    Dim As Double acc
    For i As Long=Ubound(Coefficients) To Lbound(Coefficients) Step -1
        acc=acc*x+Coefficients(i)
    Next i
    Return acc
End Function

redim shared as string p()
permutate("12345",p()) 'get 120 different states


'set a reference polynomial.
do
    cls
randomize
#define dd(d1,d2) rnd*d1-rnd*d2
dim as double D(1 to 5)
d(1)=dd(5,8)
d(2)=dd(5,5)
d(3)=dd(3,2)
d(4)=dd(1,1)
d(5)=dd(1,1)
'======================
dim as double B(1 to 5) 'working polynomial


windowtitle "Coefficients   " &d(1) &"   " &d(2) &"    " &d(3) &"   " &d(4) &"   " &d(5)
axis(rgb(200,200,200))
for z as long=lbound(p) to ubound(p)
  for z2 as long=1 to len(p(z))
      b(valint(mid(p(z),z2,1)))=d(z2)
  next z2
  sketch(polyeval(B(),x),rgb(rnd*255,rnd*255,rnd*255))
next z
sleep
if inkey=chr(27) then end
loop


sleep 
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Squares

Post by Tourist Trap »

dodicat wrote:Richard's example a₀·x⁰ + a₁·x¹ + a₂·x² + a₃·x³
I have added another coefficient (5 in all), permutated them and shown the resulting graphs.(120 in total)
There is a strange kind of pattern (press space or escape)
Image

I don't understand dodi, you do

Code: Select all

GENERATE_POLY::
   FOR N=1 TO 5
       PERMUTE_COEFF(DEG5_POLY)
       SHOW_POLY
   NEXT N
So I suppose that you have done a little more, such as:

Code: Select all

 FOR M=1 TO 3
   GENERATE_POLY::
      FOR N=1 TO 5
          PERMUTE_COEFF(DEG5_POLY)
          SHOW_POLY
      NEXT N
NEXT M
If it's right, and if we do concentrate on one pass only, your five poly generated by permutation of the coefficients have already a nice pattern with common nodes. Then yes, if it is about solving a system of 5 equations, and if we obtain this result of 3 obvious nodes in common almost each times, then it's quite a curious fact.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Squares

Post by Tourist Trap »

Richard wrote:It is important to recognise when a change of base will change the game. Physics and measurement are dependent on standards. It does not matter what base we use for a computation, the physics still works. Three-toed Sloths and five fingered humans all obey the same laws of physics.
All of this is very sound and I agree totally. Our decimal base doesn't allow us to see anything deep until what we have to say pass the test of base change.

However, given a function that operates on the digits of the numbers, this would still be of some interest to find multipliers that reverse the digit strings in any bases. And then find the law that give the multiplier value expressed as a function of the choosen base:
rev_mult = f(base)

rev_mult is not unique in base 10, it's more likely a vector: (1, 4, 9). So what is the similar vector in base 16, and so on... Maybe, when observed for all bases, it's something simple or almost random (very complicated)? Still in my todo list to play with this as soon as I can ;)
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

There is one common point (node) for every one of the polynomials.
Here are permutations of 6 coefficients (720 curves),
All the curves have one common node, and that node can be easily calculated.

Code: Select all

screen 20,32
color ,rgb(255,255,255)
#include "string.bi"
dim as double minx,maxx,miny,maxy,x
dim as integer xres,yres
screeninfo xres,yres
minx=-3
maxx=-minx

miny=-50
maxy=-miny

#macro sketch(_function,colour)
var cc=colour
For x =minx To maxx Step (maxx-minx)/500
    Dim As Double x1=(xres)*(x-minx)/(maxx-minx)
    Dim As Double y1=(yres)*(_function-maxy)/(miny-maxy)
   if x=minx then Pset(x1,y1),cc else line -(x1,y1),cc
Next x
#endmacro

#macro axis(colour)
    line(0,(yres-(miny/(miny-maxy))*yres))-(xres,(yres-(miny/(miny-maxy))*yres)),colour 'x axis
        line(((minx/(minx-maxx))*xres),0)-(((minx/(minx-maxx))*xres),yres),colour 'y axis
        draw string(0,(yres-(miny/(miny-maxy))*yres)),format(minx),colour
        draw string(xres-8-8*(len(format((maxx)))),(yres-(miny/(miny-maxy))*yres)),format(maxx),colour
         draw string (xres/2,0),format(maxy),colour
        draw string (xres/2,yres-16),format(miny),colour
#endmacro

Sub Permutate(s As String,perm() as string,OptionalStop as string="")
    Dim As Integer p,i,j,result
    Dim As String s2=s
    Redim perm(0)
    For p1 As Integer =0 To Len(s2)-2
        For p2 As Integer =p1 + 1 To Len(s2)-1
            If s2[p1]>s2[p2] Then Swap s2[p1],s2[p2]
        Next p2
    Next p1
    Do
        p=p+1
        redim preserve perm(1 to p)
        perm(p)=s2
        if s2=OptionalStop then goto skip
        Do
            For i=Len(s2)-2 To 0 Step -1
                If s2[i] <s2[i+1] Then Exit For
            Next
            If i <0 Then Result=0:Exit Do
            j =Len(s2)-1
            While s2[j] <= s2[i]: j -=1 : Wend
            Swap s2[i], s2[j]
            i +=1
            j =Len(s2)-1
            While i <j
                Swap s2[i], s2[j]
                i +=1
                j -=1
            Wend
            result=-1:Exit Do
        Loop
    Loop Until result=0
    skip:
    Redim Preserve perm(1 To p)
End Sub

Function polyeval(Coefficients() As Double,Byval x As Double) As Double
    Dim As Double acc
    For i As Long=Ubound(Coefficients) To Lbound(Coefficients) Step -1
        acc=acc*x+Coefficients(i)
    Next i
    Return acc
End Function

function map(a as double,b as double,x as double,c as double,d as double) as double
    return ((d)-(c))*((x)-(a))/((b)-(a))+(c)
end function

redim shared as string p()
permutate("123456",p()) 'get 120 different states

dim as double sum
'set a reference polynomial.
do
    cls
randomize
#define dd(d1,d2) rnd*d1-rnd*d2
#define pp int(rnd*5)
dim as double D(1 to 6)
d(1)=dd(pp,pp)
d(2)=dd(pp,pp)
d(3)=dd(pp,pp)
d(4)=dd(pp,pp)
d(5)=dd(pp,pp)
d(6)=dd(pp,pp)
sum=d(1)+d(2)+d(3)+d(4)+d(5)+d(6)
'======================
dim as double B(1 to 6) 'working polynomial


windowtitle " " &d(1) &",  " &d(2) &",   " &d(3) &",  " &d(4) &",  " &d(5) & ",   " &d(6)
axis(rgb(0,0,0))
'common node
'line(xres/2+(1/maxx)*xres/2,0)-(xres/2+(1/3)*xres/2,yres),0 'x=1
circle(xres/2+(1/maxx)*xres/2,map(miny,maxy,sum,768,0)),10,0

for z as long=lbound(p) to ubound(p)
  for z2 as long=1 to len(p(z))
      b(valint(mid(p(z),z2,1)))=d(z2)
  next z2
  sketch(polyeval(B(),x),rgb(rnd*255,rnd*255,rnd*255))
next z
sleep
if inkey=chr(27) then end
loop


sleep 
Strange?
And the coefficients are very random.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Squares

Post by Tourist Trap »

dodicat wrote:There is one common point (node) for every one of the polynomials.
Here are permutations of 6 coefficients (720 curves)
I see now , thanks! It's not just the circular permutation but all of them.

Yes strange. Moreover I count 11 common points, or the other nodes are not exactly meeting in one unique point each?
dodicat wrote: All the curves have one common node, and that node can be easily calculated.
What is the formula you get for the main node? ( I'm lazy tonight or I could just give it to Maxima ;-) )

Edit:
Ok, maybe no math software needed here. The equation that gives the common node is something like this:
(a0 - b1 - ... a719)*X^6 + (b0 - b1 - ...b719)*X^5 + ... (g0 - g1 - ... g719) == 0
Let's say this can be written as:
A*X^6 + B*X^5 + .... G == 0

Then obviously (from what we feel when watching the animation) one of the solution (which is the main node X coordinate) is written on the form of a ratio. I guess something like that:
Xmain_node = A/G

Solving for a degree 1 or 2 should be sufficent for finding the right ratio for all degrees I think.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

The main node is
x=1,y=algebraic sum of the coefficients.
another set of nodes at x=0 and x=-1.

Why this should be for a completely random set of coefficients I cannot say yet.
At the moment I'll call it the Horsetail hypothesis.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Squares

Post by Tourist Trap »

dodicat wrote: Why this should be for a completely random set of coefficients I cannot say yet.
At the moment I'll call it the Horsetail hypothesis.
The Freebasic horsetail hypothesis you mean !


Sorry for switching again to multwise reversor. Here is something to dig around base 16. Non trivial multiplier are &h3, &h7, &hF here below:

Code: Select all

'CTRL + C will generally break (I mean stop) the program if you need to quit

/' for bases up to 36 if needed
#include "crt.bi"
var n => 12
dim as zstring*100 buffer
_itoa(n, @buffer, 16)
? buffer
'/

function ReverseString(byref S as string) as string
    dim as string   returnValue => ""
    for strpos as integer = len(s) to 1 step -1
        returnValue &= mid(S,strpos,1) 
    next strpos
    '
    return returnValue
end function

for mult as integer = 2 to 16
    for base16value as integer = 16^3 to 16^4 - 1
        var testValue => str(hex(cUint(base16value)))
        var multValue => ReverseString(str(hex(cUint(mult*base16value))))
        if testValue=multValue then
            ? "found result:"
            ? "base16--->", "mult="; hex(mult) , hex(base16value), ReverseString(multValue)
            ? "base10--->", "mult="; cUint(mult) , cUint(base16value), ReverseString(str(mult*base16value))
        end if
    next base16value
next mult

'(eof)
Edit:
Ok, I think I get some pattern.
For base = 10, we get multwise reversor list = (9, 4, 1)
For base = 16, we get (F, 7, 3, 1)

So for base = b, I guess we have ( b - 1, (b - 1)\2 , ( (b - 1)\2 )\2 ..... 1 ).

The value 2 seems to be missing in base 10 or what?
Arf, I'm wrong. Octals behave differently than said above:

Code: Select all

function ReverseString(byref S as string) as string
    dim as string   returnValue => ""
    for strpos as integer = len(s) to 1 step -1
        returnValue &= mid(S,strpos,1) 
    next strpos
    '
    return returnValue
end function

for mult as integer = 2 to (8 - 1)
    for base8value as integer = 8^4 to 8^5 - 1
        var testValue => str(oct(cUint(base8value)))
        var multValue => ReverseString(str(oct(cUint(mult*base8value))))
        if testValue=multValue then
            ? "found result:"
            ? "base8---->", "mult="; oct(mult) , oct(base8value), ReverseString(multValue)
            ? "base10--->", "mult="; cUint(mult) , cUint(base8value), ReverseString(str(mult*base8value))
        end if
    next base8value
next mult

'(eof)
Last edited by Tourist Trap on Aug 09, 2017 20:39, edited 1 time in total.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Yea, the FB horsetail hypothesis.

Any polynomial will pass through the point (1,sum of it's coefficients)).

It is in fact not magic, but an extension of a power series at 1.

For instance adding up the coefficients of the sine series at x=1 gives, of course, the sine of 1 radian.

Code: Select all


Function fac(Byval num As ulongint) As ulongint 'factorial
    Dim temp As ulongint
    If num <= 0 Then
        fac = 1
        Exit Function
    End If
    temp = 1
    For n as integer = 1 To num
        temp = temp * n
    Next
    fac = temp
End Function
'Taylor coefficients of sin(1) -- add 'em up
Dim As Double s=1-1/fac(3)+1/fac(5)-1/fac(7)+1/fac(9)-1/fac(11)+1/fac(13)-1/fac(15)+1/fac(17) 
Print s
Print Sin(1)
Sleep
 
So it is a property of all polynomials.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Squares

Post by Tourist Trap »

dodicat wrote: Any polynomial will pass through the point (1,sum of it's coefficients)).
You're right. It's a clear explanation. Even more simply said, at x=1, we have y=sum_coef for all of those polynomials. So at x=1 they are all brothers.

But what about the other nodes? -1, I see. 0 also. But why the nodes degenerate there in 4 and 6 groups?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

At x=0, a y value of a node will be one of the coefficients, so you will get 6 nodes.
The reason, expanding at x=0, all terms of x^1, x^2, x^3, ... will be zero.
All you have for a y value is the constant term, one of the six coefficients.
You get all six of course during a run.

At x=-1, you get six nodes above the x axis and six nodes reflected below it.

Expanding at x=-1 gives 6 sign changes for each coefficient.
Seems like that anyway.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Squares

Post by Tourist Trap »

Hi, in the code below I try to get the multwise reversor that are mentionned a few thread above. I have a problem with my REALLOCATE... I'm not too much a specialist of this. But here it's quite weird. HOwever, overall the program works (brute force). What I don't know is, if you have got a sequence of multipliers, are they stable for all the interval of the base (intervals of the form base^n, base^(n+1) - 1)... ?

Code: Select all

#include "crt.bi"   ''to retrieve _itoa function

dim as integer workingbase
const as integer wbmin => 16
const as integer wbmax => 16
dim as integer ptr  multList(wbmin to wbmax)

function ReverseString(byref S as string) as string
    var stringReturnValue => ""
    for strpos as integer = len(s) to 1 step -1
        stringReturnValue &= mid(S,strpos,1) 
    next strpos
    '
    return stringReturnValue
end function

for workingbase = wbmin to wbmax
    multList(workingbase) = allocate(1*sizeOf(integer))
    multList(workingbase)[0] = 0
    for mult as integer = 2 to (workingbase - 1)
        for value as integer = workingbase^3 to workingbase^4 - 1
            dim as zstring*100 valuebufferstr
            _itoa(value, @valuebufferstr, workingbase)
            
            dim as zstring*100 resultbufferstr
            _itoa(cUint(mult*value), @resultbufferstr, workingbase)
            resultbufferstr => ReverseString(resultbufferstr)
            
            if valuebufferstr=resultbufferstr then
                '? "found result:"
                dim as zstring*100 multbufferstr
                _itoa(mult, @multbufferstr, workingbase)

                ? "base"; _ 
                    workingbase; _ 
                    "---->", _ 
                    "mult="; _ 
                    mult, _ 
                    valuebufferstr, _ 
                    ReverseString(resultbufferstr)
                ? "( mult="; cUint(mult) , cUint(value), str(mult*value); ")"

                multList(workingbase)[0] += 1
                multList(workingbase) = _ 
                reallocate(multList(workingbase), (multList(workingbase)[0])*sizeOf(integer))
                multList(workingbase)[(multList(workingbase)[0])] = mult
                ? "**"; multList(workingbase)[0], multList(workingbase)[(multList(workingbase)[0])]
            end if
        next value
    next mult
next workingbase

for index as integer = lBound(multList) to uBound(multList)
    ? "base"; index; ":", 
    for pindex as integer = 0 to multList(index)[0]
        ? multList(index)[pindex], 
    next pindex
    ?
    deallocate(multList(index))
next index


'(eof)
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Squares

Post by Tourist Trap »

dodicat wrote: Expanding at x=-1 gives 6 sign changes for each coefficient.
Seems like that anyway.
Ok. So in fact for x=0, we would have (deg + 1) horsetail's nodes. If deg=6, we will meet a0, a1, .. a6 in turn. With additionally some kind of equal hair flow at each node.

Interesting enough the case x=-1 seems to solve a combinatorics question. But I still have to find it. You know, something like, given 6 pigeons and 7 slots, if 6 slots are always alternatively open or closed, and the 7th always free, what is the sum of the pigeons on the open slots? (as said, this is not the right question, but something like that would be answered by the x=-1 case).

Aditional question: if the coefficients are constrained to 0 or 1, does it plot/count something particular? (a0 = 0 or 1, etc... besides the above permutation)
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Polynomial degree 7 (8 terms)
Gives 40320 curves.
Set the coefficients line 111 (the reference polynomial).
Set the x axis and y axis limits lines 12 and 15.
There are hints of nodes at -2 and +2 sometimes.
Coefficients shown on the console.
I had to spew out all these combinations to see those extra nodes.

Code: Select all

screen 20,32
color ,rgb(255,255,255)
#include "string.bi"
#include "crt.bi"

#define irange(f,l) int(Rnd*((l+1)-(f))+(f))
#define frange(f,l) (Rnd*((l)-(f))+(f))
dim as double minx,maxx,miny,maxy,x
dim as integer xres,yres
screeninfo xres,yres

minx=-3
maxx=-minx

miny=-100
maxy=-miny

#macro sketch(_function,colour)
var cc=colour
For x =minx To maxx Step (maxx-minx)/500
    Dim As Double x1=(xres)*(x-minx)/(maxx-minx)
    Dim As Double y1=(yres)*(_function-maxy)/(miny-maxy)
   if x=minx then Pset(x1,y1),cc else line -(x1,y1),cc
Next x
#endmacro

#macro axis(colour)
    line(0,(yres-(miny/(miny-maxy))*yres))-(xres,(yres-(miny/(miny-maxy))*yres)),colour 'x axis
        line(((minx/(minx-maxx))*xres),0)-(((minx/(minx-maxx))*xres),yres),colour 'y axis
        draw string(0,(yres-(miny/(miny-maxy))*yres)),format(minx),colour
        draw string(xres-8-8*(len(format((maxx)))),(yres-(miny/(miny-maxy))*yres)),format(maxx),colour
         draw string (xres/2,0),format(maxy),colour
        draw string (xres/2,yres-16),format(miny),colour
#endmacro

function map(a as double,b as double,x as double,c as double,d as double) as double
    return ((d)-(c))*((x)-(a))/((b)-(a))+(c)
end function

 type bar
    as single start,finish,variable
    as uinteger fillcol,rimcol
    end type

sub ShowBar(startx as integer,starty as integer,length as integer,thickness as integer,B as bar)
    '#define map(a,b,x,c,d) ((d)-(c))*((x)-(a))/((b)-(a))+(c)
    var xpos=map(b.start,b.finish,b.variable,startx,(startx+length))
    line(startx,starty)-(xpos,starty+thickness),B.fillcol,bf
    line(startx-1,starty-1)-(startx+length+1,starty+thickness+1),b.rimcol,b
end sub

Sub Permutate(s As String,perm() as string,OptionalStop as string="")
    Dim As Integer p,i,j,result
    Dim As String s2=s
    Redim perm(0)
    For p1 As Integer =0 To Len(s2)-2
        For p2 As Integer =p1 + 1 To Len(s2)-1
            If s2[p1]>s2[p2] Then Swap s2[p1],s2[p2]
        Next p2
    Next p1
    Do
        p=p+1
        redim preserve perm(1 to p)
        perm(p)=s2
        if s2=OptionalStop then goto skip
        Do
            For i=Len(s2)-2 To 0 Step -1
                If s2[i] <s2[i+1] Then Exit For
            Next
            If i <0 Then Result=0:Exit Do
            j =Len(s2)-1
            While s2[j] <= s2[i]: j -=1 : Wend
            Swap s2[i], s2[j]
            i +=1
            j =Len(s2)-1
            While i <j
                Swap s2[i], s2[j]
                i +=1
                j -=1
            Wend
            result=-1:Exit Do
        Loop
    Loop Until result=0
    skip:
    Redim Preserve perm(1 To p)
End Sub

Function polyeval(Coefficients() As Double,Byval x As Double) As Double
    Dim As Double acc
    For i As Long=Ubound(Coefficients) To Lbound(Coefficients) Step -1
        acc=acc*x+Coefficients(i)
    Next i
    Return acc
End Function


redim shared as string p()
permutate("01234567",p()) 'get 40320 different states

dim as double sum
dim as bar Bar
Bar.start=1
Bar.finish=40320
Bar.fillcol=rgb(200,100,0)
Bar.rimcol=0


do
    cls
randomize
'set a reference polynomial. irange - integers,  frange floats.
dim as double D(0 to 7)
d(0)=irange(0,7)
d(1)=irange(0,7)
d(2)=irange(0,7)
d(3)=irange(0,7)
d(4)=irange(-7,7)
d(5)=irange(0,7)
d(6)=irange(0,7)
d(7)=irange(0,7)
sum=d(0)+d(1)+d(2)+d(3)+d(4)+d(5)+d(6)+d(7)
'======================
dim as double B(0 to 7) 'working polynomial

dim as string cfs=" " &d(0) &",  " &d(1) &",   " &d(2) &",  " &d(3) &",  " &d(4) & ",   " &d(5) & ",   " &d(6) & ",   " &d(7)
puts(cfs)

axis(rgb(0,0,0))
'common node
circle(map(minx,maxx,1,0,xres),map(miny,maxy,sum,yres,0)),10,0

for z as long=lbound(p) to ubound(p)
  for z2 as long=0 to len(p(z))-1
      B(valint(chr(p(z)[z2])))=d(z2)
  next z2
  sketch(polyeval(B(),x),rgb(rnd*255,rnd*255,rnd*255))
   Bar.variable=z 
   ShowBar(350,200,300,10,Bar)
next z
axis(rgb(0,0,0))
sleep
if inkey=chr(27) then end
loop


sleep 
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Post by Richard »

Reverse the digits of a number by multiplying by m.
Solves an equation of form; ( ax³ + bx² + cx + d ) * m = dx³ + cx² + bx + a
for positive integer values of a, b, c, d . . . & m, for values less than base x.
The end digits may not be zero. The multiplier m is greater than 1.
A brute force search for all valid solutions in bases >= 2 gives the following table.

Code: Select all

'------------------------------------------------------------------------
'  Count number of solutions for digit reversal when * m
'
'                        number of digits
'    Digits 2       3       4       5       6       7       8       9
' Base 
'    2      0       0       0       0       0       0       0       0
'    3      0	    0       1       1       1       1       2       2
'    4      0	    0       1       1       1       1       2       2
'    5      1	    1       3       3       5       5      10      10
'    6      0	    0       2       2       2       2       4       4
'    7      1	    1       3       3       5       5      10      10
'    8      1	    1       5       5       8       8      16      16
'    9      1	    1       4       4       6       6      12      12
'   10      0	    0       2       2       2       2       4       5
'   11      4	    5      12      13      29      31      76      83
'   12      0	    0       4       4       4       4       8      15
'   13      2	    2       5       5       9       9      18
'   14      2	    2       6       8      12      12      22
'   15      2	    2       8       8      13      13      40
'   16      0	    0       3       3       3       3      39
'   17      4	    6      14      15      31      36     129
'   18      0	    0       4       4       5       8      52
'   19      7	    8      24      25      78      81     371
'   20      2	    2       8       9      13      13
'   21      2	    2       8       8      13      20
'   22      0	    0       2       4       5      13
'   23      6	    9      19      21      41      89
'   24      1	    1       9       9      12      28
'   25      2	    2       6       6      10      86
'   26      3       4       9      11      19
'   27      4       4      13      15      32
'   28      0       0       4       4       4
'   29     11      12      41      42     157
'   30      0       0       6       8      13
'   31      6       6      20      20
'   32      2       3      10      12
'   33      1       1       5       5
'   34      5       5      18      19
'   35      8       9      27      26
'   36      0       0       7       7
'   37      3       3       9       8
'   38      2       7      11      19
'   39      6      12      24      31
'   40      0       0       6       7
'   41     15      19      61
'   42      0       0       6
'   43      9      12      30
'   44      4       5      13
'   45      2       2       9
'   46      0       0       4
'   47      8      12      25
'   48      1       1      12
'   49     10      11      40
'   50      4       5      15
'------------------------------------------------------------------------
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Squares

Post by Tourist Trap »

Richard wrote:Reverse the digits of a number by multiply by m.
Solve an equation of form; ( ax³ + bx² + cx + d ) * m = dx³ + cx² + bx + a
for positive integer values of a, b, c, d . . . & m, for values less than base x.
The end digits may not be zero. The multiplier m is greater than 1.
Nice. I've also made some brute force identification of the candidate for being valid multipliers (reversewise) for bases from 2 to 32.
Why I say "candidate" is for I'm testing only an interval of numbers (I do n*m with m obviously in a small interval), I'm not sure of what happens below or even above.
The analogy with base 10 lets me think that there is an lower bound for the numbers to be multiplied if they want to be possibly reversed, and also once you are above this lower bound a search only in one full interval (base^n, base^(n+1) - 1) is sufficent. But I'm not 100% sure. Moreover you show in your listing that sometimes there is a lower bound, and sometimes reversal is possible with one digit only.

Base 23, 29 are more apt for this multwise reverse operation. Probably base 41 also and more, but I couldn't go beyond 32 with ITOA.

Whatever here is the list I obtain:
BASE: MULTIPLIERS (values)
base 2: none
base 3: 2
base 4: 3
base 5: 2, 4
base 6: 2, 5
base 7: 3, 6
base 8: 2, 3, 5, 7
base 9: 2, 4, 8
base 10: 4, 9
base 11: 2, 3, 5, 7, 10
base 12: 2, 3, 5, 11
base 13: 5, 6, 12
base 14: 2, 4, 6, 13
base 15: 2, 3, 4, 7, 11, 14
base 16: 3, 7, 15
base 17: 2, 4, 5, 8, 10, 11, 16
base 18: 2, 5, 8, 17
base 19: 3, 4, 6, 7, 9, 18
base 20: 2, 3, 4, 6, 9, 19
base 21: 2, 5, 6, 10, 13, 20
base 22: 10, 21
base 23: 2, 3, 4, 5, 7, 11, 13, 15, 17, 22
base 24: 2, 3, 4, 5, 7, 11, 19, 23
base 25: 4, 9, 12, 24
base 26: 2, 8, 10, 11, 12, 25
base 27: 2, 3, 6, 8, 13, 26
base 28: 3, 6, 13, 27
base 29: 2, 4, 5, 8, 9, 14, 16, 19, 28
base 30: 2, 4, 5, 9, 14, 29
base 31: 3, 7, 10, 11, 15, 30
base 32: 2, 3, 4, 5, 7, 10, 15, 31

Code: Select all

'multipliers that reverse digits of base-n numbers

#include "crt.bi"   ''to retrieve _itoa function

screenRes 600, 540, 32
Width 600\8, 540\16 

dim as integer workingbase
const as integer wbmin => 2
const as integer wbmax => 32
dim as longint multList(wbmin to wbmax)
dim as longint multListMore(wbmin to wbmax)

function ReverseString(byref S as string) as string
    var stringReturnValue => ""
    for strpos as integer = len(s) to 1 step -1
        stringReturnValue &= mid(S,strpos,1) 
    next strpos
    '
    return stringReturnValue
end function

for workingbase = wbmin to wbmax
    dim as integer lastmult => 0
    '
    for mult as integer = 2 to (workingbase - 1)
        for value as integer = workingbase^3 to workingbase^4 - 1
            if inkey()=chr(27) then 
                ? "break"
                getKey()
                end
            end if
            
            dim as zstring*255 valuebufferstr
            _itoa(value, @valuebufferstr, workingbase)
            
            dim as zstring*255 resultbufferstr
            _itoa(cUint(mult*value), @resultbufferstr, workingbase)
            resultbufferstr => ReverseString(resultbufferstr)
            
            if valuebufferstr=resultbufferstr then
                '? "found result:"
                dim as zstring*255 multbufferstr
                _itoa(mult, @multbufferstr, workingbase)

                '? "base"; _ 
                '    workingbase; _ 
                '    "---->", _ 
                '    "mult="; _ 
                '    mult, _ 
                '    valuebufferstr, _ 
                '    ReverseString(resultbufferstr)
                '? "( mult="; cUint(mult) , cUint(value), str(mult*value); ")"

                if lastmult<>mult then
                    if len(str(multList(workingbase)))<11 then
                        multList(workingbase) *= 10^(len(str(mult)) + 1)
                        multList(workingbase) += mult
                        lastmult = mult
                        '? mult, multList(workingbase)
                    else
                        multListMore(workingbase) *= 10^(len(str(mult)) + 1)
                        multListMore(workingbase) += mult
                        lastmult = mult
                        '? mult, multList(workingbase)
                    end if
                end if
            end if
        next value
    next mult
    '
    cls
    for index as integer = lBound(multList) to uBound(multList)
        ? "base"; iif(len(str(index))=1, space(1), ""); index; ":     "; multList(index)
        if multListMore(index)<>0 then
            ? "(more)"; index; ":   "; multListMore(index)
        end if
    next index
next workingbase


'(eof)
Locked