[Request] For a random binary string (1010001110101...) of a given length n, on average how much space would you save by describing it numerically rather than writing it out?

With your latest edit (edit 2), the amount of time it would take you to describe it (excluding how long it takes to say 0's instead of 0, or eight instead of 1, 0 etc) it would, on average take you n+1 items to describe, meaning one more item that it would take normally.

what this means is that for an 8 digit binary number it would take you 9 digits on average to describe it.

Likewise for a 16 digit binary number it would take you 17 on average to describe it with this "compression" technique. That's not to say in some cases it isn't better, but on average for any possible length it will always take one more item to describe it

Here is an example of what would happen if you "compressed" a 5 bit number with the working

+/u/compilebot c++

#include <iostream>
#include <cmath>
#include <string>
#include <bitset>

using namespace std;

void Compress(string, int *, int);
float calcAverage(int *, int);

int main(){
    const static int bitLength=5;

    int *data = new int[int(pow(2,bitLength))];//declare memory

    for(int i=0; i<pow(2,bitLength); i++){//cycle through each binary item
        data[i]=bitLength;
        Compress(std::bitset<bitLength>( i ).to_string(), data, i);

        //uncomment the line underneath to see workings
        cout << std::bitset<bitLength>( i ).to_string() << " Has Length of " << data[i] << ", ";
    }

    int Average = calcAverage(data, int(pow(2,bitLength)));

    cout << endl << "Average length is " << Average << " Over " << pow(2,bitLength) << ' ' << bitLength << " bit numbers"<< endl;


    return 0;
}

void Compress(string in, int *data, int upTo){
    char current='z';
    for(int i=0; i<in.length(); i++){
        if(in[i] == current){
            data[upTo]--;
        }else{
            data[upTo]++;
            current=in[i];
        }
    }
}

float calcAverage(int *Data, int arraySize){
    int Total=0;
    for(int i=0; i<arraySize; i++){
        Total+=Data[i];
    }

    return float(Total/arraySize);
}
/r/theydidthemath Thread