#!/usr/bin/perl

# http://www.spiegel.de/forum/wissenschaft/raetsel-der-woche-nur-nullen-und-einsen-erlaubt-thread-616323-1.html

use strict; use warnings;
use Math::BigInt; # lib => 'GMP';

my $divisor = 2017;

my $i=1;
my %nn; # Hash-Tabelle: $nn{$key} = $value

while (1) {
	my $n = Math::BigInt->new('1' x $i);
	my ($div, $rest) = $n->bdiv($divisor); # modifies n, we don't care
	my $intrest = $rest->numify();
	if (exists $nn{$intrest}) {
		my $i2 = $nn{$intrest};
		my $s = '1' x ($i - $i2) . '0' x $i2;
		print "Resultat: ", $s, "\n";
		$n = Math::BigInt->new($s);
		$n->bdiv($divisor);
		print "Division durch $divisor: ", $n->bstr(), "\n";
		exit(0);
	} else {
		if (0 && $rest->is_zero()) {
			print "Resultat: ", '1' x $i, "\n";
			print print "Division durch $divisor: ", $n->bstr(), "\n";
			exit(0);
		}
		$nn{$intrest} = $i;
	}
	$i++;
}
