Created
August 17, 2012 13:13
-
-
Save ichirin2501/3378631 to your computer and use it in GitHub Desktop.
Project Euler 26
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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