Created
November 3, 2011 00:24
-
-
Save benelog/1335412 to your computer and use it in GitHub Desktop.
total block
This file contains hidden or 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
total = 0 | |
for i in range (1,11): | |
total += pow(i-1,2) + pow(i,2) | |
print total |
objective-c 구현 붙여 봅니다. :-)
__block NSUInteger sTotal = 0;
[[[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(1, 10)] enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
sTotal += pow(idx-1, 2) + pow(idx, 2);
}];
scala 구현 붙여봅니다. (fupfin님이 시켰어요. 엉엉)
(0/:(1 to 10)){ (sum,n) => sum + n*n + (n-1)*(n-1) }
@codian 흐허~ :)
fupfin님이 자기가 생각한 것도 짜라고 또 scala 코딩시켰어요. 어흐엉 사각형의 개수가 매 번 (4n- 4)개 씩 늘어난다는 것에 착안한 방법입니다.
ps. 미투에서 자신의 알고리즘이 너무 아름답다며 감탄하고 계시다는... ㅡ..ㅡ;
(List(1)/:(2 to 10)){ (sum, n) => (4*n - 4 + sum.head)::sum }.reduceLeft(_ + _)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ruby 구현 붙여봅니다. ^^