From 612f4bfa3226bd91c60b58deb07e1e06562157de Mon Sep 17 00:00:00 2001 From: domix80 Date: Sun, 19 Feb 2023 10:41:20 +0100 Subject: [PATCH 1/4] Feat: Project Euler Problem 6 --- DIRECTORY.md | 2 ++ project_euler/problem_006/sol1.rb | 35 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 project_euler/problem_006/sol1.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index 51a0e9e..59f47c0 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -153,6 +153,8 @@ * [Sol2](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_004/sol2.rb) * Problem 005 * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_005/sol1.rb) + * Problem 006 + * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_006/sol1.rb) * Problem 020 * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_020/sol1.rb) * Problem 021 diff --git a/project_euler/problem_006/sol1.rb b/project_euler/problem_006/sol1.rb new file mode 100644 index 0000000..28de45c --- /dev/null +++ b/project_euler/problem_006/sol1.rb @@ -0,0 +1,35 @@ +#Project Euler Problem 6: #https://projecteuler.net/problem=6 + +#Sum square difference + +#The sum of the squares of the first ten natural numbers #is, +# 1^2 + 2^2 + ... + 10^2 = 385 +#The square of the sum of the first ten natural numbers #is, +# (1 + 2 + ... + 10)^2 = 55^2 = 3025 +#Hence the difference between the sum of the squares of #the first ten +#natural numbers and the square of the sum is 3025 - 385 = 2640. +#Find the difference between the sum of the squares of the first one +#hundred natural numbers and the square of the sum. + +def solution?(num) + x = 1 + y = 1 + result = 1 + gap = 3 + while y < num + x += gap + gap += 2 + y += 1 + result += x + end + r_n_pow2_plus_n_pow2 = result + r_sum_n_pow2 = (((num / 2) + 0.5) * num) ** 2 + + r_sum_n_pow2 - r_n_pow2_plus_n_pow2 +end + +answer = 0 +answer = solution?(10) + + +p answer From 740ed1795a23c99a6aa8ac5cb608db17812daebe Mon Sep 17 00:00:00 2001 From: domix80 Date: Tue, 21 Feb 2023 16:24:55 +0100 Subject: [PATCH 2/4] Update DIRECTORY.md Co-authored-by: Amos Paribocci --- DIRECTORY.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 59f47c0..51a0e9e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -153,8 +153,6 @@ * [Sol2](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_004/sol2.rb) * Problem 005 * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_005/sol1.rb) - * Problem 006 - * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_006/sol1.rb) * Problem 020 * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_020/sol1.rb) * Problem 021 From 95930cc22925d40a7d9e004ec2838916676af0d3 Mon Sep 17 00:00:00 2001 From: domix80 Date: Tue, 21 Feb 2023 18:12:39 +0100 Subject: [PATCH 3/4] Update solution file and remove link from directory --- DIRECTORY.md | 2 -- project_euler/problem_006/sol1.rb | 8 +++----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 59f47c0..51a0e9e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -153,8 +153,6 @@ * [Sol2](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_004/sol2.rb) * Problem 005 * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_005/sol1.rb) - * Problem 006 - * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_006/sol1.rb) * Problem 020 * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_020/sol1.rb) * Problem 021 diff --git a/project_euler/problem_006/sol1.rb b/project_euler/problem_006/sol1.rb index 28de45c..a243852 100644 --- a/project_euler/problem_006/sol1.rb +++ b/project_euler/problem_006/sol1.rb @@ -11,7 +11,8 @@ #Find the difference between the sum of the squares of the first one #hundred natural numbers and the square of the sum. -def solution?(num) +def solution?() + num = 10 x = 1 y = 1 result = 1 @@ -28,8 +29,5 @@ def solution?(num) r_sum_n_pow2 - r_n_pow2_plus_n_pow2 end -answer = 0 -answer = solution?(10) - - +answer = solution?() p answer From bb5df875377e3c9d4dfb918e8b554cde9b6b3a19 Mon Sep 17 00:00:00 2001 From: domix80 Date: Tue, 21 Feb 2023 18:36:46 +0100 Subject: [PATCH 4/4] Update project_euler/problem_006/sol1.rb Co-authored-by: Amos Paribocci --- project_euler/problem_006/sol1.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_006/sol1.rb b/project_euler/problem_006/sol1.rb index a243852..b00c9e7 100644 --- a/project_euler/problem_006/sol1.rb +++ b/project_euler/problem_006/sol1.rb @@ -11,8 +11,7 @@ #Find the difference between the sum of the squares of the first one #hundred natural numbers and the square of the sum. -def solution?() - num = 10 +def solution(num=10) x = 1 y = 1 result = 1 @@ -29,5 +28,5 @@ def solution?() r_sum_n_pow2 - r_n_pow2_plus_n_pow2 end -answer = solution?() +answer = solution() p answer