Created
August 19, 2012 03:30
-
-
Save kiyotune/3391590 to your computer and use it in GitHub Desktop.
Project Euler 問題28
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/env perl | |
use strict; | |
use warnings; | |
my $to = shift; #arg[0] 1001 | |
my $sum = 0; | |
for(my $n=1; $n<=$to; $n+=2){ | |
$sum += get_sum_corner($n); | |
} | |
print "(n = $to) Ans: $sum\n"; | |
sub get_sum_corner{ | |
my $n = shift; | |
my $f; | |
if($n == 1) | |
{ | |
$f = 1; | |
} | |
else | |
{ | |
$f = 4*$n*$n - ($n-1)*6; | |
} | |
#print "n=$n: sum = $f\n"; | |
return($f); | |
} | |
# (n = 1001) Ans: 669171001 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
一番外側の4点のみを考える。
n * n (nは奇数):
(4) = (3)-(n-1) .. (3) = (2)-(n-1)
: :
(1) = n^2 .. (2) = (1)-(n-1)
(1)+(2)+(3)+(4)を式で書くと
f = n^2+(n^2-(n-1))+((n^2-(n-1))-(n-1))+(((n^2-(n-1))-(n-1))-(n-1))
展開
n は奇数
n = 1 の時: f = 1
n > 1 の時: f = 4_n^2-(n-1)_6