Balanced Bases

   Another problem I came across while browsing the EIS for sequences that needed more terms, is a problem with balanced bases A049364. What is the smallest number that is digitally balanced in all bases 2, 3, ... n?

I wrote a routine that tested whether a number was balanced, and most importantly if it is not it increments the number to a point where it is closer to one that is! I found a base 5 balanced number in seconds, but then discovered subsequent solutions must be EXTREMELY large! Here are all the known solutions so far:


2
  • Base 2: 10

  • 572
  • Base 2: 1000111100
  • Base 3: 210012

  • 8410815
  • Base 2: 100000000101011010111111
  • Base 3: 120211022110200
  • Base 4: 200011122333

  • 59609420837337474
  • Base 2: 11010011110001100111001111010010010001101011100110000010

  • Base 3: 101201112000102222102011202221201100

  • Base 4: 3103301213033102101223212002

  • Base 5: 1000001111222333324244344


  • The next solution is greater than 22185312344622607538702383625075608111920737991870030337 so could be a bit tough to find, but probably not out of reach. :) Here is the NTL code I wrote, feel free to continue the search:
    #include <math.h>
    BOOL IsBalanced(int b, ZZ &n) {
        double ln = log(n); // natural log
        // log_b(n) = ln(n)/ln(b)
        int lb = (int)(ln / log(b));
        // Since it must be balanced, base b digits should be
        // multiples of base...
        if(lb % b != (b-1)) {
            while( (lb % b) != (b-1) ) lb++;      
            // set n = b^lb;
            power(n, b, lb);
            // could improve this by adding 0,0,0,1,1,1,etc...
            return FALSE;
        }
    
        ZZ nCopy = n;
        int *hit = new int [b];
        memset(hit, 0, b * sizeof(int));
    
        // Early break-out...
        ZZ x;
        power(x, b, lb);
        for(int z=lb; z>1; z--) {
            int r = Long(nCopy/x) % b;
            hit[r]++;
            if(hit[r] > (lb+1)/b) {
                n -= n%x;
                n += x;
                delete [] hit;
                return FALSE;
            }
            nCopy %= x;
            x /= b;
        }
        nCopy = n;
        memset(hit, 0, b * sizeof(int));
        while(nCopy > 0) {
            hit[nCopy%b]++;
            nCopy /= b;
        }
        int target = (lb+1)/b;
    
        for(int i=0; i<b; i++)
            if(hit[i] != target) 
                break;
    
        delete [] hit;
        
        if(i<b) return FALSE;
        return TRUE;
    }
    
    void BalanceTest(void) {
        ZZ i;
        i = 500; // starting point
    
        int targetBal = 6; // target balance base...
        for(;;) {
            cout << "i=" << i << "     \r" << flush;
            if(kbhit()) break;
    
            int gotit = 1;
            if(i%2 ==0) {
                for(int c=2; c<=targetBal; c++) {
                    if(!IsBalanced(c, i)) {
                        gotit = 0;
                        break;
                    }
                }
            }
            else {
                for(int c=targetBal; c>=2; c--) {
                    if(!IsBalanced(c, i)) {
                        gotit = 0;
                        break;
                    }
                }
            }
    
            if(gotit) {
                cout << targetBal << "bal=" << i << "   \n" << flush;
                break;
            }
            // Every now and then check to make sure i
            // is possible for middle bases and adjust
            // accordingly...
            if(rand()%10 == 0) {
                IsBalanced(3+rand()%(targetBal-3), i);
            }
    
            i++;
        }
        cout << "Last i = " << i << "   \n" << flush;
    }
    

    Joe K. Crump | Number Theory Home