Skip to content

Commit 2fd4ee7

Browse files
authored
Merge pull request #201 from domix80/Problem_006
Feat: Project Euler Problem 6
2 parents e2f6bd9 + bb5df87 commit 2fd4ee7

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

project_euler/problem_006/sol1.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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

0 commit comments

Comments
 (0)