Skip to content

Instantly share code, notes, and snippets.

@kiyotune
Created August 20, 2012 03:57
Show Gist options
  • Save kiyotune/3400540 to your computer and use it in GitHub Desktop.
Save kiyotune/3400540 to your computer and use it in GitHub Desktop.
Project Euler 問題29
#!/usr/bin/perl
use strict;
use warnings;
use Math::BigInt;
my ($amin, $amax, $bmin, $bmax) = @ARGV; # 2 100 2 100
my $len = length((Math::BigInt->new($amax)**$bmax)->as_hex()) - 2;
my %arr;
my $v;
for(my $i=$amin; $i<=$amax; $i++)
{
for(my $j=$bmin; $j<=$bmax; $j++)
{
$v = (Math::BigInt->new($i) ** $j)->as_hex(); #prefix '0x'
$v = substr($v, 2);
$v = sprintf("%0".$len."s", $v);
#print "$v\n";
$arr{$v} = 1;
}
}
my @arr = sort {$a cmp $b} keys %arr;
print "($amin<=a<=$amax, $bmin<=b<=$bmax) Ans: ".(scalar @arr)."\n";
#(2<=a<=100, 2<=b<=100) Ans: 9183
@kiyotune
Copy link
Author

32bit OS だと use bigint はダメなのね...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment