Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
total block
total = 0
for i in range (1,11):
total += pow(i-1,2) + pow(i,2)
print total
@codian

This comment has been minimized.

Copy link

@codian codian commented Nov 3, 2011

ruby 구현 붙여봅니다. ^^

    puts (1..10).inject(0) { |total, i| total + (i-1)**2 + i**2 }
@hyukhur

This comment has been minimized.

Copy link

@hyukhur hyukhur commented Nov 3, 2011

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);
}];
@nephilim

This comment has been minimized.

Copy link

@nephilim nephilim commented Nov 3, 2011

scala 구현 붙여봅니다. (fupfin님이 시켰어요. 엉엉)

(0/:(1 to 10)){ (sum,n) => sum + n*n + (n-1)*(n-1) }
@codian

This comment has been minimized.

Copy link

@codian codian commented Nov 4, 2011

@nephilim , @fupfin 님 멋쟁이

@nephilim

This comment has been minimized.

Copy link

@nephilim nephilim commented Nov 4, 2011

@codian 흐허~ :)

@nephilim

This comment has been minimized.

Copy link

@nephilim nephilim commented Nov 4, 2011

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