File tree Expand file tree Collapse file tree
project_euler/problem_006 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #Project Euler Problem 6: #https://projecteuler.net/problem=6
2+
3+ #Sum square difference
4+
5+ #The sum of the squares of the first ten natural numbers #is,
6+ # 1^2 + 2^2 + ... + 10^2 = 385
7+ #The square of the sum of the first ten natural numbers #is,
8+ # (1 + 2 + ... + 10)^2 = 55^2 = 3025
9+ #Hence the difference between the sum of the squares of #the first ten
10+ #natural numbers and the square of the sum is 3025 - 385 = 2640.
11+ #Find the difference between the sum of the squares of the first one
12+ #hundred natural numbers and the square of the sum.
13+
14+ def solution ( num = 10 )
15+ x = 1
16+ y = 1
17+ result = 1
18+ gap = 3
19+ while y < num
20+ x += gap
21+ gap += 2
22+ y += 1
23+ result += x
24+ end
25+ r_n_pow2_plus_n_pow2 = result
26+ r_sum_n_pow2 = ( ( ( num / 2 ) + 0.5 ) * num ) ** 2
27+
28+ r_sum_n_pow2 - r_n_pow2_plus_n_pow2
29+ end
30+
31+ answer = solution ( )
32+ p answer
You can’t perform that action at this time.
0 commit comments