From 853e207ccdf8fe2e8441a36d7b1ea5bb0ffcf537 Mon Sep 17 00:00:00 2001 From: Victor Payno Date: Mon, 15 Apr 2024 20:19:15 -0700 Subject: [PATCH] r/difference-of-squares: 1st iteration --- r/README.md | 1 + r/difference-of-squares/README.md | 7 +- .../difference-of-squares.R | 14 +++- r/difference-of-squares/run-tests-r.txt | 65 +++++++++++++++++++ 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 r/difference-of-squares/run-tests-r.txt diff --git a/r/README.md b/r/README.md index 84b40ddc..eb30fb56 100644 --- a/r/README.md +++ b/r/README.md @@ -16,3 +16,4 @@ - [raindrops](./raindrops/README.md) - [leap](./leap/README.md) - [reverse-string](./reverse-string/README.md) +- [difference-of-squares](./difference-of-squares/README.md) diff --git a/r/difference-of-squares/README.md b/r/difference-of-squares/README.md index ccfd4e14..47e92a6f 100644 --- a/r/difference-of-squares/README.md +++ b/r/difference-of-squares/README.md @@ -34,4 +34,9 @@ Finding the best algorithm for the problem is a key skill in software engineerin ### Based on -Problem 6 at Project Euler - https://projecteuler.net/problem=6 \ No newline at end of file +Problem 6 at Project Euler - https://projecteuler.net/problem=6 + +### My Solution + +- [difference-of-squares.R](./difference-of-squares.R) +- [run-tests](./run-tests-r.txt) diff --git a/r/difference-of-squares/difference-of-squares.R b/r/difference-of-squares/difference-of-squares.R index 3a2053c3..1f38d682 100644 --- a/r/difference-of-squares/difference-of-squares.R +++ b/r/difference-of-squares/difference-of-squares.R @@ -1,5 +1,17 @@ # this is a stub function that takes a natural_number # and should return the difference-of-squares as described # in the README.md -difference_of_squares <- function(natural_number) { +square_of_sum <- function(number) { + sum <- number * (number + 1) / 2 + square <- sum ^ 2 + + return(square) +} + +sum_of_squares <- function(number) { + return(number * (number + 1) * (2 * number + 1) / 6) +} + +difference_of_squares <- function(number) { + return(square_of_sum(number) - sum_of_squares(number)) } diff --git a/r/difference-of-squares/run-tests-r.txt b/r/difference-of-squares/run-tests-r.txt new file mode 100644 index 00000000..94317cff --- /dev/null +++ b/r/difference-of-squares/run-tests-r.txt @@ -0,0 +1,65 @@ +Running automated test file(s): + + +=============================================================================== + +Running: ../../.github/citools/r/r-test + +Running R Tests + +R versions: + + R version 4.3.3 (2024-02-29) -- "Angel Food Cake" + Copyright (C) 2024 The R Foundation for Statistical Computing + Platform: x86_64-pc-linux-gnu (64-bit) + + R is free software and comes with ABSOLUTELY NO WARRANTY. + You are welcome to redistribute it under the terms of the + GNU General Public License versions 2 or 3. + For more information about these matters see + https://www.gnu.org/licenses/. + + + Rscript (R) version 4.3.3 (2024-02-29) + + + ============================================================================== + +Running: Rscript test_difference-of-squares.R + +Test passed 😀 +Test passed 😸 +Test passed 😸 +Test passed 🥇 + +real 0m0.581s +user 0m0.527s +sys 0m0.055s + + + ============================================================================== + +Exit code: 0 + +real 0m0.766s +user 0m0.609s +sys 0m0.167s + +real 0m0.769s +user 0m0.611s +sys 0m0.169s + +=============================================================================== + +Running: misspell ./difference-of-squares.R ./test_difference-of-squares.R + +real 0m0.018s +user 0m0.019s +sys 0m0.009s + +=============================================================================== + +/home/vpayno/git_vpayno/exercism-workspace/r + +=============================================================================== +