Переглянути джерело

Difference of squares problem done

master
Lachlan Jacob 5 роки тому
джерело
коміт
25d0fd4e8a

+ 9
- 0
exercism/fsharp/difference-of-squares/DifferenceOfSquares.fs Переглянути файл

@@ -0,0 +1,9 @@
module DifferenceOfSquares

let square n = pown n 2

let squareOfSum (number: int): int = square ([1..number] |> List.sum)

let sumOfSquares (number: int): int = [1..number] |> List.sumBy square

let differenceOfSquares (number: int): int = abs ((sumOfSquares number) - (squareOfSum number))

+ 21
- 0
exercism/fsharp/difference-of-squares/DifferenceOfSquares.fsproj Переглянути файл

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Compile Include="DifferenceOfSquares.fs" />
<Compile Include="DifferenceOfSquaresTests.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2" />
<PackageReference Include="FsUnit.xUnit" Version="3.8.1" />
</ItemGroup>

</Project>

+ 45
- 0
exercism/fsharp/difference-of-squares/DifferenceOfSquaresTests.fs Переглянути файл

@@ -0,0 +1,45 @@
// This file was auto-generated based on version 1.2.0 of the canonical data.

module DifferenceOfSquaresTests

open FsUnit.Xunit
open Xunit

open DifferenceOfSquares

[<Fact>]
let ``Square of sum 1`` () =
squareOfSum 1 |> should equal 1

[<Fact>]
let ``Square of sum 5`` () =
squareOfSum 5 |> should equal 225

[<Fact>]
let ``Square of sum 100`` () =
squareOfSum 100 |> should equal 25502500

[<Fact>]
let ``Sum of squares 1`` () =
sumOfSquares 1 |> should equal 1

[<Fact>]
let ``Sum of squares 5`` () =
sumOfSquares 5 |> should equal 55

[<Fact>]
let ``Sum of squares 100`` () =
sumOfSquares 100 |> should equal 338350

[<Fact>]
let ``Difference of squares 1`` () =
differenceOfSquares 1 |> should equal 0

[<Fact>]
let ``Difference of squares 5`` () =
differenceOfSquares 5 |> should equal 170

[<Fact>]
let ``Difference of squares 100`` () =
differenceOfSquares 100 |> should equal 25164150


+ 43
- 0
exercism/fsharp/difference-of-squares/README.md Переглянути файл

@@ -0,0 +1,43 @@
# Difference Of Squares

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.

## Hints
For this exercise the following F# features come in handy:
- The [range operator](https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/operators.%5B-..-%5D%5B%5Et%5D-function-%5Bfsharp%5D) allows you to succinctly create a range of values.
- [List.sumBy](https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/list.sumby%5B't,%5Eu%5D-function-%5Bfsharp%5D) is a condensed format to apply a function to a list and then sum the results.


## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

## Autoformatting the code

F# source code can be formatted with the [Fantomas](https://github.com/fsprojects/fantomas) tool.

After installing it with `dotnet tool restore`, run `dotnet fantomas .` to format code within the current directory.

## Further information

For more detailed information about the F# track, including how to get help if
you're having trouble, please visit the exercism.io [F# language page](http://exercism.io/languages/fsharp/resources).

## Source

Problem 6 at Project Euler [http://projecteuler.net/problem=6](http://projecteuler.net/problem=6)


Завантаження…
Відмінити
Зберегти