Finding the Beast in Nonpositive Integers

On Facebook, my friend Mike just opined that the percentage of nonpositive integers that contain the number 666 is just below one tenth of a percent.

Well, I had to see how close he was … at least how close in a 32-bit ecosystem.

thebeast.c

( note that I have changed each program since the original post, altering the divisor used to control the floating-point calculation from 111 to 654321. I also added in an extra calculation and output call to printf() after the loop for the final statistics, if you let the program run that long. )

// The Beast

// Copyright (C) 2012 by Jim Lawless ... MIT / X11 license
// See: http://www.mailsend-online.com/license2012.php

// Approximate the percentage of nonpositive integers
// ( addressable by C ) that contain 666 ... the mark
// of the beast!

#include <stdio.h>
#include <limits.h>
#include <string.h>

//
// This was compiled with a 32-bit C compiler ... I'll adapt
// to 64-bit later ... like I'm gonna wait around that long...

int main(int argc,char **argv) {
   char wrk[22];
   double percentage;
   long counter,beasts=0;
   for(counter=-666;counter>INT_MIN;counter--) {
      sprintf(wrk,"%d",counter);
      if(strstr(wrk,"666")!=NULL) {
         beasts++;
      }
      if( (counter%654321)==0) {
         percentage=((double) beasts)/(-((double)counter));
         printf("Percentage: %-15f     Counter: %-10d     Total found: %d\r",
            percentage,counter,beasts);
      }
   }
   percentage=((double) beasts)/(-((double)counter));
   printf("Percentage: %-15f     Counter: %-10d     Total found: %d\r",
            percentage,counter,beasts);
   printf("\n\n");
}

The number seems to hover ( at least initially ) at around .45% then, it moved up a tad to .56 % … then, back down. Here’s the output at an early stage on my machine:

Percentage: 0.004580            Counter: -51037038      Total found: 233728

Someone might want to double-check my math.

AAARGH!

Mike just changed his story and said he meant nonnegative integers. So, here ya go … a companion piece of code:

thebeastreturns.c

// The Beast Returns!

// Copyright (C) 2012 by Jim Lawless ... MIT / X11 license
// See: http://www.mailsend-online.com/license2012.php

// Approximate the percentage of nonnegative integers
// ( addressable by C ) that contain 666 ... the mark
// of the beast!

#include <stdio.h>
#include <limits.h>
#include <string.h>

//
// This was compiled with a 32-bit C compiler ... I'll adapt
// to 64-bit later ... like I'm gonna wait around that long...

int main(int argc,char **argv) {
   char wrk[22];
   double percentage;
   long counter,beasts=0;
   for(counter=666;counter<INT_MAX;counter++) {
      sprintf(wrk,"%d",counter);
      if(strstr(wrk,"666")!=NULL) {
         beasts++;
      }
      if( (counter%654321)==0) {
         percentage=((double) beasts)/((double)counter);
         printf("Percentage: %-15f     Counter: %-10d     Total found: %d\r",
            percentage,counter,beasts);
      }
   }
   percentage=((double) beasts)/((double)counter);
   printf("Percentage: %-15f     Counter: %-10d     Total found: %d\r",
      percentage,counter,beasts);

   printf("\n\n");
}

…and, the output so far is:

Percentage: 0.004580            Counter: 51037038       Total found: 233728

Anyone care to bet that the number of occurrences for the set of all nonpositive integers matches the number of occurrences for all nonnegative integers? 😉

Advertisements

About Jim Lawless

I've been programming computers for about 36 years ... 30 of that professionally. I've been a teacher, I've worked as a consultant, and have written articles here and there for publications like Dr. Dobbs Journal, The C/C++ Users Journal, Nuts and Volts, and others.
This entry was posted in Technology and tagged , . Bookmark the permalink.

3 Responses to Finding the Beast in Nonpositive Integers

  1. Mike says:

    : … … …
    nob: 0
    god: 10
    total: 10
    pct: 0.00000000

    : … … …
    nob: 0
    god: 100
    total: 100
    pct: 0.00000000

    : … … …
    nob: 1
    god: 999
    total: 1000
    pct: 0.10000000

    : … … …
    nob: 19
    god: 9981
    total: 10000
    pct: 0.19000000

    : … … …
    nob: 280
    god: 99720
    total: 100000
    pct: 0.28000000

    : … … …
    nob: 3700
    god: 996300
    total: 1000000
    pct: 0.37000000

    : … … …
    nob: 45991
    god: 9954009
    total: 10000000
    pct: 0.45991000

    : … … …
    nob: 549739
    god: 99450261
    total: 100000000
    pct: 0.54973900

    : … … …
    nob: 6394870
    god: 993605130
    total: 1000000000
    pct: 0.63948700

    : … … …
    nob: 72915400
    god: 9927084600
    total: 10000000000
    pct: 0.72915400

  2. Mike says:

    Argh myself for that previous post, I pasted in the wrong output. Here’s the total for the first 100,000,000,000 numbers:

    nob: 818,740,081
    god: 99,181,259,919
    pct: 0.81874008

    Looks like it is converging on 1%. That makes sense because each order of magnitude has 1% of the numbers containing the combo but you have to take away the 1% of the prior matches.

    Runs pretty quickly, too.

    real 5m15.237s
    user 5m15.083s
    sys 0m0.141s

  3. Mike says:

    Some days my typing defeats me. Each order of magnitude adds 1 in 1000, which is 0.1%, not 1.0%. I’m going to go to bed before I dig myself any deeper.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s