Skip to content

Commit

Permalink
r/difference-of-squares: download exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Apr 16, 2024
1 parent b7cba71 commit 0238bef
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
26 changes: 26 additions & 0 deletions r/difference-of-squares/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"authors": [
"cmiller01"
],
"contributors": [
"jonmcalder",
"katrinleinweber",
"kytrinyx",
"Scientifica96",
"zacchaeusluke"
],
"files": {
"solution": [
"difference-of-squares.R"
],
"test": [
"test_difference-of-squares.R"
],
"example": [
".meta/example.R"
]
},
"blurb": "Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.",
"source": "Problem 6 at Project Euler",
"source_url": "https://projecteuler.net/problem=6"
}
1 change: 1 addition & 0 deletions r/difference-of-squares/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"r","exercise":"difference-of-squares","id":"42fd99e2e04547a083cda1f2e6264fa3","url":"https://exercism.org/tracks/r/exercises/difference-of-squares","handle":"vpayno","is_requester":true,"auto_approve":false}
38 changes: 38 additions & 0 deletions r/difference-of-squares/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Help

## Running the tests

Tests require the `{testthat}` package to be installed in R.
To run the tests for an exercise, simply execute the `test_<exercise_name>.R` script from within the exercise's directory.

This can be conveniently done with [testthat's `auto_test` function](https://testthat.r-lib.org/reference/auto_test.html). Because exercism code and tests are in the same folder, use this same path for both `code_path` and `test_path` parameters. On the command-line, you can also run `Rscript test_<exercise_name>.R`.

See the [tests page](https://exercism.org/docs/tracks/r/tests) for more information.

## Submitting your solution

You can submit your solution using the `exercism submit difference-of-squares.R` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [R track's documentation](https://exercism.org/docs/tracks/r)
- The [R track's programming category on the forum](https://forum.exercism.org/c/programming/r)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

To get help if you're having trouble, you can try one of the following resources:

- [StackOverflow](https://stackoverflow.com/questions/tagged/r) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
- [#rstats](https://twitter.com/search?q=%23rstats) is the hashtag to use if you are asking for help or guidance on Twitter. The R community is very active on Twitter and always try to help those who are new to R.
- [/r/rstats](https://www.reddit.com/r/rstats) is the R subreddit.
- [RStudio Community](https://community.rstudio.com/) is another active and helpful R community.
37 changes: 37 additions & 0 deletions r/difference-of-squares/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Difference of Squares

Welcome to Difference of Squares on Exercism's R Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.

The square of the sum of the first ten natural numbers is
(1 + 2 + ... + 10)² = 55² = 3025.

The sum of the squares of the first ten natural numbers is
1² + 2² + ... + 10² = 385.

Hence the difference between the square of the sum of the first ten natural numbers and the sum of the squares of the first ten natural numbers is 3025 - 385 = 2640.

You are not expected to discover an efficient solution to this yourself from first principles; research is allowed, indeed, encouraged.
Finding the best algorithm for the problem is a key skill in software engineering.

## Source

### Created by

- @cmiller01

### Contributed to by

- @jonmcalder
- @katrinleinweber
- @kytrinyx
- @Scientifica96
- @zacchaeusluke

### Based on

Problem 6 at Project Euler - https://projecteuler.net/problem=6
5 changes: 5 additions & 0 deletions r/difference-of-squares/difference-of-squares.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 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) {
}
22 changes: 22 additions & 0 deletions r/difference-of-squares/test_difference-of-squares.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
source("./difference-of-squares.R")
library(testthat)

test_that("difference of squares 0", {
input <- 0
expect_equal(difference_of_squares(input), 0)
})

test_that("difference of squares 5", {
input <- 5
expect_equal(difference_of_squares(input), 170)
})

test_that("difference of squares 10", {
input <- 10
expect_equal(difference_of_squares(input), 2640)
})

test_that("difference of squares 100", {
input <- 100
expect_equal(difference_of_squares(input), 25164150)
})

0 comments on commit 0238bef

Please sign in to comment.