#!/usr/bin/perl

use Net::hostent;
use Math::BigInt;

$statcnt = 0;
$tested  = 0;
$tested2 = 0;
$hits    = 0;

my @string;
my $flag;
my $test = "";
my $testdepth = 599;


$| = 1;

if ( gethost( "www.x386.net" ) ) {
  print "Yea, dns works!\n";
} else {
  print "Trouble with dns!\n";
}

print "Google ";

open( EFILE, "< e.5mil.work3" );

print "Challange ";

$starttime  = time();
$starttime2 = time();

$string[1]  = getc EFILE;  
$string[2]  = getc EFILE;  
$string[3]  = getc EFILE;  
$string[4]  = getc EFILE;  
$string[5]  = getc EFILE;  
$string[6]  = getc EFILE;  
$string[7]  = getc EFILE;  
$string[8]  = getc EFILE;  
$string[9]  = getc EFILE;  
$string[10] = getc EFILE;

print "Number: 1\n";

for ( my $big_loop = 1; $big_loop <= 4999991; $big_loop++ ) {

  $statcnt++;
  if ( ( $statcnt % 1000 ) == 0 ) {
    $tested = $tested + $tested2;
    my $speed = $statcnt / ( time() - $starttime );
    my $todo = (( 4999991 - $statcnt ) / 4999991 ) * 100;
    my $etleft = ((( 4999991 - $statcnt ) / $speed ) / 60 ) / 60; 
    print "Progress: $todo % left - $speed prime tests a second\n";  
    print "  $tested possible primes found - $hits possible hits found\n";
    my $speed = $tested / ( time() - $starttime );
    print "  Testing $speed possible primes against DNS a second\n";
    my $speed = $tested2 / ( time() - $starttime2 );
    print "  Tested $speed primes a second for the last pass\n";
    if ( $speed < 2 ) {
      if ( $testdepth > 100 ) {
        $testdepth = $testdepth - 10;
        print "  Less math, more DNS...\n";
      }
    } elsif ( $speed >= 4 ) {
      $testdepth = $testdepth + 10;
      print "  More math, less DNS...\n";
    }
    print "  $etleft hours to go\n";
    $tested2 = 0;
    $starttime2 = time();
  }

  if ( $string[1] eq "0" ) {
    $string[1] = $string[2];
    $string[2] = $string[3];
    $string[3] = $string[4];
    $string[4] = $string[5];
    $string[5] = $string[6];
    $string[6] = $string[7];
    $string[7] = $string[8];
    $string[8] = $string[9];
    $string[9] = $string[10];
    $string[10] = getc EFILE;
    next;
  }

  if ( $string[10] eq "5" ) {
    $string[1] = $string[2];
    $string[2] = $string[3];
    $string[3] = $string[4];
    $string[4] = $string[5];
    $string[5] = $string[6];
    $string[6] = $string[7];
    $string[7] = $string[8];
    $string[8] = $string[9];
    $string[9] = $string[10];
    $string[10] = getc EFILE;
    next;
  }

  $test = $string[1] . $string[2] . $string[3] . $string[4] . $string[5] . $string[6] . $string[7] . $string[8] . $string[9] . $string[10];

  $flag = 0;
  $test = Math::BigInt->new($test);
  for ( my $loop_cnt = 2; $loop_cnt <= $testdepth; $loop_cnt++ ) {
    if ( ( $test % $loop_cnt ) == 0 ) {
      $flag = 1;
      last;
    }
  }
  
  $test = $string[1] . $string[2] . $string[3] . $string[4] . $string[5] . $string[6] . $string[7] . $string[8] . $string[9] . $string[10];

  if ( $flag == 0 ) {
    my $test_url = "www." . $test . ".com";
    $tested2++;
    if ( gethost( $test_url ) ) {
      print "HIT! $test_url\n";
      `echo $test_url >> google1.hits`;
      $hits++;
    }
  }

  $string[1] = $string[2];
  $string[2] = $string[3];
  $string[3] = $string[4];
  $string[4] = $string[5];
  $string[5] = $string[6];
  $string[6] = $string[7];
  $string[7] = $string[8];
  $string[8] = $string[9];
  $string[9] = $string[10];
  $string[10] = getc EFILE;
}

close ( EFILE );  

my $speed = $statcnt / ( time() - $starttime );
print "Processed $statcnt potential strings at $speed strings a second.\n";
print "Tested $tested potential primes, generated $hits hits.\n";
