Skip to content

Commit 612f4bf

Browse files
committed
Feat: Project Euler Problem 6
1 parent e2f6bd9 commit 612f4bf

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@
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

project_euler/problem_006/sol1.rb

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

0 commit comments

Comments
 (0)