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 153153 * [ Sol2] ( https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_004/sol2.rb )
154154 * Problem 005
155155 * [ Sol1] ( https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_005/sol1.rb )
156+ * Problem 006
157+ * [ Sol1] ( https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_006/sol1.rb )
156158 * Problem 020
157159 * [ Sol1] ( https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_020/sol1.rb )
158160 * Problem 021
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 )
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 = 0
32+ answer = solution? ( 10 )
33+
34+
35+ p answer
You can’t perform that action at this time.
0 commit comments