Squares

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

Re: Squares

Post by albert »

@fxm

I deleted the last post i made cause it didn't compress.. This formula compresses 57% after 40 loops

Code: Select all


screen 19

dim as longint v1
dim as string n , c
do 
    
    randomize
    
    dim as longint n1 = int( rnd * 65536 )
    
        v1 = n1 \ 256
        
        n = str(frac(n1/256))
        n = mid(n,3)
        n = left(n,2)
        
        print
        print n1
        
        ' try to recreate n1 , using only v1 and n
        for b as longint = 0 to 65535 step 1
            
            c = str(frac(b/256))
            c = mid(c,3)
            c = left(c,2)
            
            if b  \  256 = v1  and c = n then print b : exit for
        
        next
        
        sleep
        
loop until inkey = chr(27)

sleep
end

albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Richard

if i do :

n = int( rnd * 65535 ) mod 256

There's 256 mod values that equal it
How do you tell which of the 256 mod values it is?

I tried
mods = n
for a as longint = mods to 65535 step mods , it doesn't work..
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Post by Richard »

fxm wrote:Note: If the question is not very understandable or changing, we can hardly help.
Albert wrote:You guys;
I'm trying to round the 16 bit input ( 0 to 65535) to 1 or 2 decimal digits , and then try to recreate the input with just those 2 digit places.
No one can possibly help if it is both mathematically impossible and unspecified. Indeed, if we only consider rational solutions we will not guess or suggest the impossible.

If you throw away valuable information during the rounding process then it will not be possible to recover that lost information. If it was possible, we would not need memory, we could reinvent everything again every time.

16 bits can take 2^16 = 65536 different patterns. If you reduce it to 15 bits, = 32768 different patterns, then you have lost one bit of information. That could be a zero or a 1 and you have no way of telling which it was.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Richard
@fxm

I got it figured out... Using two mods , But it doesn't compress..

Code: Select all


screen 19

dim as longint v1 , v2
dim as longint n1 = -1
do 
    
        n1+=1
        
        v1 = n1 mod 256
        v2 = n1 mod 257
        
        print
        print n1 , v1 , v2
        
        for b as longint = 0 to 65535 step 1
            
            if (b  mod 256) = v1  and (b mod 257) = v2 then print b : exit for
            
        next
        
        if n1 mod 10 = 0 then print "press key for next 10   press esc to exit " : sleep
        
        if inkey = chr(27) then end
        
loop

sleep
end

@Richard

if i do :

n = int( rnd * 65535 ) mod 256

There's 256 mod values that equal it
How do you tell which of the 256 mod values it is?
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Post by Richard »

@ Albert.
n = int( rnd * 65535 ) mod 256
n = Int( Rnd * 65536 ); generates random 16 bit Uintegers from 0 to 65535.
x = n Mod 256; keeps only the 8 bits on the right and throws away the most significant 8 bits.
Those missing bits are y = n \ 256, they cannot be recovered.
LoByte(n) and HiByte(n) are faster and do the same thing as Div and Mod.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Albert.
If you keep a record of the divs as well as the mods you can get the original number back.

Code: Select all


#define join(a,b) (a) or (b) shl 8


randomize
print "num","num mod 256","num\256","return"
for z as long=1 to 20
dim as long n= int( rnd * 65535 ) 
print n,n mod 256,n\256,join(n mod 256,n\256)
next
sleep

  
Richard, with your new found interest in the ogin, I thought you would be representing your island.
https://www.skiffieworlds2019.com/
They have a huge beer tent and live music.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

I turned it into a formula that will compress....

Code: Select all


screen 19

#define join(a,b) (a) or (1024*b) 

do

    print
    
    randomize
    
    print "num","num mod 256","num\1024","return"
    
    for z as long = 1 to 20
           dim as long n = int( rnd * 65535 )
           print  n , n mod 256 , n \ 1024 , join(n mod 256 , n \ 1024 )
    next
    
    sleep

loop until inkey=chr(27)
  
It should compress 60% after 40 loops...
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

DATA COMPRESSION

Post by albert »

@Dodicat

I got it returning correct , and the output is only 4 digits , for 5 digit input. Should compress 90+% after 40 loops

11 bits out for 16 bits in !!!

Code: Select all


screen 19

#define join(a,b) (a) or (8192*b) 
do

    print
    randomize
    print "num","num mod 256","num\8192","return"
    
    for z as long = 1 to 20
        dim as long n = int( rnd * 65536 )
        print  n , n mod 256 , n \ 8192 , join(n mod 256 , n \ 8192 )
    next
    
    sleep

loop until inkey=chr(27)  

!!~~Now you can compress the "Universe of Data" down to 11 bits.~~!!
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Mistake there Albert-sorry
you pass wrongly to the macro.
You should either bracket off what you pass to the macro. or bracket off inside the macro what you have passed.
Say for a value of n=62287

Code: Select all



#define join(a,b) (a) or 8192*(b)   'correct-bracketted off each parameter 
#define join2(a,b) (a) or 8192*b    'wrong, b is not bracketed

var n=62287

print (n mod 256) or (8192*(n\8192))  'correct

print (n mod 256) or 8192*n\8192      'wrong

print

print join( n mod 256,n\8192)
print join2( n mod 256,n\8192)
sleep

 
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

I'm getting bad values with vars...

Code: Select all


screen 19

#define join(a,b) (a) or  (8192*b)
do

    print
    
    randomize
    
    print "  num" , "num mod 256" , "num\8192" , "return"
    
    dim as longint v1
    dim as longint v2
    
        dim as long n = int( rnd * 65536 )
        
        v1 = n mod 256
        v2 = n \ 8192
        
        'works with  ( n \ 8192 )  but not with v2 ????
        print  n , n mod 256 , n \ 8192 , join(n mod 256 , n \ 8192 ) ; "   Right answer join( n mod 256 , n \ 8192)" 
        print  n ,  v1               ,  v2          , join(      v1          ,      v2       ) ; "   Wrong answer  join( v1 , v2)"
        
        print
    
    sleep

loop until inkey=chr(27)  

dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Exactly Albert.
The macro should be
#define join(a,b) (a) or (8192*(b))
where the parameters are in brackets.
otherwise sending n \ 8192 to the macro means it is calculating (n mod 256) or 8192*n\8192 in the macro, instead of (n mod 256) or (8192*(n\8192))
when brackets are in place.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

#define join(a,b) a or (8192*b) ' returns correct answer for join( n mod 256 , n \ 8192 ) but not for join( v1 , v2 )

#define join(a,b) a or (b*8192) ' returns wrong all the time..

#define join(a,b) (a) or (8192*(b)) , returns wrong all the time..
#define join(a,b) (a) or ( (b)*8192 ), returns wrong all the time..

run the code and have a look , keep pressing a key for the next number. press esc to quit

Code: Select all


screen 19

#define join(a,b) a or (8192*b)  ' returns correct answer for join( n mod 256 , n \ 8192 ) but not for join( v1 , v2 )

do

    print
    
    randomize
    
    print "  num" , "num mod 256" , "num\8192" , "return"
    print "         " , " v1"  , " v2"
    print
    dim as longint v1
    dim as longint v2
    
        dim as long n = int( rnd * 65536 )
        
        v1 = n mod 256
        v2 = n \ 8192
        
        'works with  ( n \ 8192 )  but not with v2 ????
        print  n , n mod 256 , n \ 8192 , join(n mod 256 , n \ 8192 ) ; "   Right answer join( n mod 256 , n \ 8192)" 
        print  n ,  v1               ,  v2          , join(      v1          ,       v2     ) ; "   Wrong answer  join( v1 , v2)"
        
        print
    
    sleep

loop until inkey=chr(27)

dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

A correct macro form is
#define join(a,b) (a) or 8192*(b)
It gives an unwanted answer, but the macro is correctly worked out for all a and b whether join(n mod 256 , n \ 8192 ) or join( v1,v2 )
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

How is it returning "n" for #define join(a,b) a or ( 8192 * b ) ???

It also returns "n" for #define join(a,b) ( 8192 * b ) , without an (a)

You pass it a = ( n mod 256) and b = ( n \ 8192 ) , how is it returning "n" ?


Somehow ; the ill formed macro , is returning the correct answer....
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

It is returning n because the faulty macro is calculating this.

Code: Select all

 
do
 dim as long n = int( rnd * 65536 )
 print n,(n mod 256) or 8192*n\8192 'is calculated in macro
 sleep
 loop until inkey=chr(27) 
Or thereabouts.
Locked