Some solutions for the julia path
This commit is contained in:
23
julia/difference-of-squares/.exercism/config.json
Normal file
23
julia/difference-of-squares/.exercism/config.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"authors": [
|
||||
"SaschaMann"
|
||||
],
|
||||
"contributors": [
|
||||
"bovine3dom",
|
||||
"cmcaine"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"difference-of-squares.jl"
|
||||
],
|
||||
"test": [
|
||||
"runtests.jl"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.jl"
|
||||
]
|
||||
},
|
||||
"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
julia/difference-of-squares/.exercism/metadata.json
Normal file
1
julia/difference-of-squares/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"julia","exercise":"difference-of-squares","id":"43726f141cb245cbb9d9c8b9cbdc4dc7","url":"https://exercism.org/tracks/julia/exercises/difference-of-squares","handle":"Kimawari","is_requester":true,"auto_approve":false}
|
||||
34
julia/difference-of-squares/HELP.md
Normal file
34
julia/difference-of-squares/HELP.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# Help
|
||||
|
||||
## Running the tests
|
||||
|
||||
To run the tests, run this command from within the exercise directory:
|
||||
|
||||
```bash
|
||||
$ julia runtests.jl
|
||||
```
|
||||
|
||||
## Submitting your solution
|
||||
|
||||
You can submit your solution using the `exercism submit difference-of-squares.jl` 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 [Julia track's documentation](https://exercism.org/docs/tracks/julia)
|
||||
- The [Julia track's programming category on the forum](https://forum.exercism.org/c/programming/julia)
|
||||
- [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, we recommend that you submit your code and request mentoring :)
|
||||
|
||||
If you don't want to do that for whatever reason, then you can find the wider Julia community channels [here](https://julialang.org/community/).
|
||||
34
julia/difference-of-squares/README.md
Normal file
34
julia/difference-of-squares/README.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# Difference of Squares
|
||||
|
||||
Welcome to Difference of Squares on Exercism's Julia 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
|
||||
|
||||
- @SaschaMann
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @bovine3dom
|
||||
- @cmcaine
|
||||
|
||||
### Based on
|
||||
|
||||
Problem 6 at Project Euler - https://projecteuler.net/problem=6
|
||||
14
julia/difference-of-squares/difference-of-squares.jl
Normal file
14
julia/difference-of-squares/difference-of-squares.jl
Normal file
@@ -0,0 +1,14 @@
|
||||
"Square the sum of the first `n` positive integers"
|
||||
function square_of_sum(n)
|
||||
return sum(1:n)^2
|
||||
end
|
||||
|
||||
"Sum the squares of the first `n` positive integers"
|
||||
function sum_of_squares(n)
|
||||
return sum((1:n).^2)
|
||||
end
|
||||
|
||||
"Subtract the sum of squares from square of the sum of the first `n` positive ints"
|
||||
function difference(n)
|
||||
return square_of_sum(n) - sum_of_squares(n)
|
||||
end
|
||||
27
julia/difference-of-squares/runtests.jl
Normal file
27
julia/difference-of-squares/runtests.jl
Normal file
@@ -0,0 +1,27 @@
|
||||
using Test
|
||||
|
||||
include("difference-of-squares.jl")
|
||||
|
||||
@testset verbose = true "tests" begin
|
||||
@testset "Square the sum of the numbers up to the given number" begin
|
||||
@test square_of_sum(1)::Integer == 1
|
||||
@test square_of_sum(5)::Integer == 225
|
||||
@test square_of_sum(10)::Integer == 3025
|
||||
@test square_of_sum(100)::Integer == 25502500
|
||||
end
|
||||
|
||||
@testset "Sum the squares of the numbers up to the given number" begin
|
||||
@test sum_of_squares(1)::Integer == 1
|
||||
@test sum_of_squares(5)::Integer == 55
|
||||
@test sum_of_squares(10)::Integer == 385
|
||||
@test sum_of_squares(100)::Integer == 338350
|
||||
end
|
||||
|
||||
@testset "Subtract sum of squares from square of sums" begin
|
||||
@test difference(0)::Integer == 0
|
||||
@test difference(1)::Integer == 0
|
||||
@test difference(5)::Integer == 170
|
||||
@test difference(10)::Integer == 2640
|
||||
@test difference(100)::Integer == 25164150
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user