Skip to content

Instantly share code, notes, and snippets.

@ichirin2501
Created August 17, 2012 13:13
Show Gist options
  • Save ichirin2501/3378631 to your computer and use it in GitHub Desktop.
Save ichirin2501/3378631 to your computer and use it in GitHub Desktop.
Project Euler 26
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $ans = -1;
my $mx = 0;
for(2..999){
my ($rem, $i) = (1, 1);
my %h = (0 => 0);
while(1){
$rem = (10 * $rem) % $_;
last if exists($h{$rem});
$h{$rem} = $i++;
}
if( $rem > 0 && $mx < $i - $h{$rem} ){
$mx = $i - $h{$rem};
$ans = $_;
}
}
print "ans = $ans\n";
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use List::Util qw/reduce/;
sub recurring_cycle{
my $n = shift;
my ($rem, $i) = (1, 1);
my %h = (0 => 0);
while(1){
$rem = (10 * $rem) % $n;
last if exists($h{$rem});
$h{$rem} = $i++;
}
return 0 if $rem == 0;
return $i - $h{$rem};
}
my $res = reduce{ $a->[1] > $b->[1] ? $a : $b } map{ [$_, &recurring_cycle($_)] } 1..999;
print $res->[0],"\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment