## Squares

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

### Re: Squares

@Dodicat

Thanks i got it working...

I didn't have the lib in the dir i was working from...
So i copied it to the desktop and it worked..
albert
Posts: 5307
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Squares

I still think my grid compressor is a good idea..
You tally the number of 0's or 1's in each row and column...

1 0 1 1 1 1 1 . 3 each row and column = 0 to 7 = 3 bits..
1 1 0 0 1 1 1 . 3
0 1 1 0 0 1 1 . 3
0 1 0 1 1 0 0 . 3
1 0 0 0 1 0 0 . 3
0 0 1 1 1 0 0 . 3
0 1 0 1 0 0 1 . 3
-------------
3 3 3 3 3 3 3

Compresses 49 bits to 42 bits.

To decompress you have to jimmy the bits around till all the columns and rows equal out.

But the idea can recurse , so you can keep compressing the output over and over..till all the data in the universe compresses to a single 7 x 7 grid.

Call it G77_ZIP
Posts: 1609
Joined: May 24, 2007 22:10
Location: The Netherlands

### Re: Squares

Compress the universe to a 1 x 1 grid and you have your singularity!
albert
Posts: 5307
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Squares

For the grid there's a total possible of ( 127 ^ 7 ) or 49 bits...

But most of those aren't practicle..and never occur in actual data...

For the compression to 42 bits...there's 7 bits less , that means that there might be 127 duplicate grids..
But like i said , all the possible grid combo's are probably not utilized in actual data. So in the end it might actually be plausible...

Like: for a grid of 49 0's , you would never see that in actual data , except in a pict file.. where you have lot of black in the picture..
Same way with all 48 0's and 1 , 1 , it would never occur in normal files. ( that's 1 ^ 49 values you can forget about.. )
Same way with 47 0's and 2 , 1's , you can probably cancel all those out...
same way with 3 1's and up to 8 1's..( maybe more. ) you can cancel all those out...

So in the end, there might only be 42 bits worth of combos in actual use.

What i'm stuck on is ; how to jimmy the bits around to equal the grid..

49 bits to 42 bits = 14.28% compression per run...
albert
Posts: 5307
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Squares

@Dodicat

With your "zlib" code : when you set it to do s[ place ] = int( rnd*256 ) , it expands the data instead of compressing it..

It Turns 1,000 bytes into 1,016 bytes -1.09% compression

And your comprerssion % code was wrong. should be ( 100 - ( 100 / ( len(s) / len(compressed ) ) ) ; "%"

Code: Select all

`Namespace Zlibrary#inclib "zlib"Extern "C"    Declare Function compressBound(Byval sourceLen As Ulong) As Ulong    Declare Function uncompress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long    Declare Function compress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As LongEnd ExternFunction getpassedinfo(text As String,Byref passed_length As Integer) As String    Dim As String var1,var2    Dim As Integer pst    #macro splice(stri,char,var1,var2)    pst=Instr(stri,char)    var1="":var2=""    If pst<>0 Then        var1=Mid(stri,1,pst-1)        var2=Mid(stri,pst+1)    Else        var1=stri    End If    #endmacro    splice(text,"|",var1,var2)    text=var2    passed_length=Valint(var1)    Return textEnd Function'=================   UNPACK ===============Function unpack(file As String) As String    Dim As Integer passed_length    Dim As String text=getpassedinfo(file,passed_length)    Dim As Integer stringlength,destinationlength    stringlength=Len(text)    destinationlength =passed_length    Dim As Ubyte Ptr source    Dim As Ubyte Ptr  destination =Callocate(destinationlength,1)    source=@text[0]    Var mistake=uncompress(destination,@destinationlength, source, stringlength)    If mistake<>0 Then Print "There was an error":Sleep:End    Dim As String uncompressed    uncompressed=String(destinationlength,0)    For i As Integer = 0 To destinationlength- 1        uncompressed[i]=(destination[i])    Next    Deallocate destination    Return uncompressedEnd Function'===================  PACK ============Function pack(file As String) As String    Dim As String text=file    Dim As Integer stringlength,destinationlength    stringlength=Len(text)    destinationlength = compressBound(stringlength)    Dim As Ubyte Ptr source    Dim As Ubyte Ptr destination =Callocate(destinationlength,1)    source=@text[0]    Var mistake=compress(destination, @destinationlength, source, stringlength)    If mistake <>0 Then Print "There was an error"    Dim As String compressed    compressed=String(destinationlength,0)    For n As Integer=0 To destinationlength-1        compressed[n]=destination[n]    Next n    compressed=stringlength &"|"+compressed    Deallocate destination    Return compressedEnd FunctionEnd Namespace'==============  use =========screen 19RandomizeDim Shared As String s:s=String(1000,0)For z As Long=1 To 5    'make a million string    For n As Long=0 To Len(s)-1        s[n]=Int(Rnd*256)    Next       Print "original string"    Print Len(s)    Print       Dim As String compressed=Zlibrary.pack(s)       Print "packed string "    Print Len(compressed)    Print       Dim As String uncompressed=Zlibrary.unpack(compressed)       Print "Retrieve"    Print Len(uncompressed)    Print    Print "compression ratio  "; 100 - ( 100 / ( Len(s) / Len(compressed)) ) ; "%"    Print Iif(uncompressed=s,"OK","ERROR")    Print "-------------------------------"Next zPrint "Done"Sleepend`
Richard
Posts: 2963
Joined: Jan 15, 2007 20:44
Location: Australia

### Re: Squares

badidea wrote:Compress the universe to a 1 x 1 grid and you have your singularity!
The interesting question there is the size of the dictionary needed to regenerate the universe from 1 bit.

Many string theory physicists believe that the content of a volume of the universe is fully expressed as a hologram on the surface area of that volume. Converting 3D to 2D is a magnificent compression. Likewise, turning 2D into 1D would be very useful.
Posts: 1609
Joined: May 24, 2007 22:10
Location: The Netherlands

### Re: Squares

Richard wrote:
badidea wrote:Compress the universe to a 1 x 1 grid and you have your singularity!
The interesting question there is the size of the dictionary needed to regenerate the universe from 1 bit.

If you drop that dictionary, you get a big bang :-)
albert
Posts: 5307
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Squares

So with the grid idea:

You can probably cancel all the values with 1 to 8 1's in the entire grid.
Likewise you can probably cancel all the values of 1 to 8 0's in the entire grid.

Most of the possible grids never occur in real life file data..
Richard
Posts: 2963
Joined: Jan 15, 2007 20:44
Location: Australia

### Re: Squares

albert wrote:So with the grid idea:

You can probably cancel all the values with 1 to 8 1's in the entire grid.
Likewise you can probably cancel all the values of 1 to 8 0's in the entire grid.

Most of the possible grids never occur in real life file data..
When you compress data you must keep a record of the process you used, so you can reverse the process to regenerate the original data. That compression record is called a “dictionary” because it is often a list of long repeated words or sequences that are replaced with shorter sequences. When you find a good compressor it will produce a dictionary that is smaller than the original data. In effect, all compressors end up with a single set bit as their compressed data. It is the relative small size of the dictionary that decides how good a compressor really is.
albert
Posts: 5307
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Squares

@Richard

With the grid idea , there's 42 bits total...But each 3 bits is 0 to 7 ,

So for the grid there's 127 ^ 7 total possible grids. .49 bits worth..

For the summing of the grid there's 0 to 7 ^ 7 for the horizontal and ^ 7 for the vertical
Total number of grids that can be summed = ( 7 ^ 7 ^ 7 ) that's like " +e 041 " total possible grids..

Call it GOD_ZIP or YAH_ZIP...
dodicat
Posts: 6013
Joined: Jan 10, 2006 20:30
Location: Scotland

### Re: Squares

Hi Albert.
I don't know why zlib is not compressing your base 256 string, but random data doesn't compress very well anyway.
Also, windows zip doesn't compress it either.
7-zip doesn't compress it either.
So the closest thing to a black hole yet, by way of squeeze, is a random base 256 string.
albert
Posts: 5307
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Squares

@Richard

I made a mistake, it wouldn't be ( 7 ^ 7 ^ 7 ) it would be a 14 digit octal odometer , 7 ^ 14 , or 42 bits..

But there's only 7 bits difference, 49 bits of possible grids and 42 bit of solutions..
So that mean there are 7 bits worth of duplicate grids.

For those 127 duplicate grids , you could put in code , if a grid has a duplicate you could put a pointer to which 127 it is..
It' probably not possible that a grid has several solutions, so each 127 grids you could probably just use and extra bit to specify which of two grids it is.

It might be that there's 127 grids that equal a single sum solution??
That would be easier to to code for...
Then when that one grid comes up , you can add a byte to point to which 127 it is..

For 49 bits worth of grids , there's not enough memory or hard drive space to solve all 49 bits worth or grids to find the 127 duplicates..
Its possible that those 127 duplicate grids , would hardly ever or never show up??
albert
Posts: 5307
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Squares

@Richard

For the grid , there's ( 127 ^ 7 ) possibilities ( 49 bits ) , and 42 bits ( 7 ^ 14 ) sum solutions..

( 127 ^ 7 ) / ( 7 ^ 14) = 785 , total duplicates , is that correct??

785 duplicates out of 42 bits is a small number of duplicates.. it should be easy to code for...

The problem is the decompression , trying all the possible values , for each row and column..
albert
Posts: 5307
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Squares

@Dodicat

I found a way to compress a compression..
So it gets 30+% smaller each iteration..

If i get rich , from the idea , I'll cut you in for 50% of the earnings. ( cause I'm using your compress code. )

I still need to run some tests , That's why i needed a string compressor.
So i could run the output back into the input to compress it more , and more..

I don't know about using the "zlib"?
Maybe i would have to pay them as well?
So we might have to split the earnings 3 ways?

The problem i'm having is once you get above chr( 99 ) it compresses less and less. With chr( random * 256 ) it expands the data..
I still need to run some tests to see if i can get it working..

======================================================================================
The other problem i'm having is:
How to tie the program to a computer , so when someone buys the program , it can't be used on another computer..
To cut out people sharing the program to get out of buying it..

Maybe ; having a set space in the executable , to record the computer serial number?
Then check the serial number , each time the program runs?
Then if the serial number doesn't match up , then it would error and quit.
======================================================================================

I'll keep you posted....

You can turn ASCII chars into decimal and compress by 50% by turning it into chr( 0 to 99 ) then compress it and it gets 30% smaller..
That's why i was asking Richard for a base 256 to base 10 converter. so i could keep making the number smaller and smaller.
My method of turning each asc chr into 0 to 255 is too big , a base 256 to base 10 converter would make the number smaller.
albert
Posts: 5307
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Squares

You turn a file into hexadecimal ( A thru P )

Then you turn two letters at a time into a single chr( ) , then compress it , then turn those bytes back into hex A-P

it keeps getting smaller and smaller...