@@ -0,0 +1,13 @@ | |||
module ArmstrongNumbers | |||
let intFromChar a = int a - int '0' | |||
// a ^ b where they are both ints | |||
let rec intPow (a: int) (b: int) = | |||
if b = 0 then 1 else a * (intPow a (b - 1)) | |||
let isArmstrongNumber (number: int): bool = | |||
let len = String.length (string (number)) | |||
number = | |||
(Seq.toList (string (number)) | |||
|> Seq.fold (fun a c -> a + (intPow (intFromChar c) len)) 0) |
@@ -0,0 +1,21 @@ | |||
<Project Sdk="Microsoft.NET.Sdk"> | |||
<PropertyGroup> | |||
<TargetFramework>netcoreapp3.0</TargetFramework> | |||
<IsPackable>false</IsPackable> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<Compile Include="ArmstrongNumbers.fs" /> | |||
<Compile Include="ArmstrongNumbersTests.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.1" /> | |||
<PackageReference Include="FsUnit.xUnit" Version="3.8.1" /> | |||
</ItemGroup> | |||
</Project> |
@@ -0,0 +1,45 @@ | |||
// This file was auto-generated based on version 1.1.0 of the canonical data. | |||
module ArmstrongNumbersTests | |||
open FsUnit.Xunit | |||
open Xunit | |||
open ArmstrongNumbers | |||
[<Fact>] | |||
let ``Zero is an Armstrong number`` () = | |||
isArmstrongNumber 0 |> should equal true | |||
[<Fact>] | |||
let ``Single digit numbers are Armstrong numbers`` () = | |||
isArmstrongNumber 5 |> should equal true | |||
[<Fact>] | |||
let ``There are no 2 digit Armstrong numbers`` () = | |||
isArmstrongNumber 10 |> should equal false | |||
[<Fact>] | |||
let ``Three digit number that is an Armstrong number`` () = | |||
isArmstrongNumber 153 |> should equal true | |||
[<Fact>] | |||
let ``Three digit number that is not an Armstrong number`` () = | |||
isArmstrongNumber 100 |> should equal false | |||
[<Fact>] | |||
let ``Four digit number that is an Armstrong number`` () = | |||
isArmstrongNumber 9474 |> should equal true | |||
[<Fact>] | |||
let ``Four digit number that is not an Armstrong number`` () = | |||
isArmstrongNumber 9475 |> should equal false | |||
[<Fact>] | |||
let ``Seven digit number that is an Armstrong number`` () = | |||
isArmstrongNumber 9926315 |> should equal true | |||
[<Fact>] | |||
let ``Seven digit number that is not an Armstrong number`` () = | |||
isArmstrongNumber 9926314 |> should equal false | |||
@@ -0,0 +1,32 @@ | |||
# Armstrong Numbers | |||
An [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that is the sum of its own digits each raised to the power of the number of digits. | |||
For example: | |||
- 9 is an Armstrong number, because `9 = 9^1 = 9` | |||
- 10 is *not* an Armstrong number, because `10 != 1^2 + 0^2 = 1` | |||
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153` | |||
- 154 is *not* an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190` | |||
Write some code to determine whether a number is an Armstrong number. | |||
## 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 | |||
Wikipedia [https://en.wikipedia.org/wiki/Narcissistic_number](https://en.wikipedia.org/wiki/Narcissistic_number) | |||
@@ -0,0 +1,29 @@ | |||
# Sum Of Multiples | |||
Given a number, find the sum of all the unique multiples of particular numbers up to | |||
but not including that number. | |||
If we list all the natural numbers below 20 that are multiples of 3 or 5, | |||
we get 3, 5, 6, 9, 10, 12, 15, and 18. | |||
The sum of these multiples is 78. | |||
## 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 | |||
A variation on Problem 1 at Project Euler [http://projecteuler.net/problem=1](http://projecteuler.net/problem=1) | |||
@@ -0,0 +1,9 @@ | |||
module SumOfMultiples | |||
let isMultiple factors number = | |||
List.fold (fun a x -> (a || number % x = 0)) false factors | |||
let sum (factors: int list) (upperBound: int): int = | |||
let goodFactors = List.filter (fun i -> i <> 0) factors | |||
let numbers = [ for i in 1..(upperBound - 1) -> i] | |||
List.sum (List.filter (fun el -> isMultiple goodFactors el) numbers) |
@@ -0,0 +1,21 @@ | |||
<Project Sdk="Microsoft.NET.Sdk"> | |||
<PropertyGroup> | |||
<TargetFramework>netcoreapp3.0</TargetFramework> | |||
<IsPackable>false</IsPackable> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<Compile Include="SumOfMultiples.fs" /> | |||
<Compile Include="SumOfMultiplesTests.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> |
@@ -0,0 +1,73 @@ | |||
// This file was auto-generated based on version 1.5.0 of the canonical data. | |||
module SumOfMultiplesTests | |||
open FsUnit.Xunit | |||
open Xunit | |||
open SumOfMultiples | |||
[<Fact>] | |||
let ``No multiples within limit`` () = | |||
sum [3; 5] 1 |> should equal 0 | |||
[<Fact>] | |||
let ``One factor has multiples within limit`` () = | |||
sum [3; 5] 4 |> should equal 3 | |||
[<Fact>] | |||
let ``More than one multiple within limit`` () = | |||
sum [3] 7 |> should equal 9 | |||
[<Fact>] | |||
let ``More than one factor with multiples within limit`` () = | |||
sum [3; 5] 10 |> should equal 23 | |||
[<Fact>] | |||
let ``Each multiple is only counted once`` () = | |||
sum [3; 5] 100 |> should equal 2318 | |||
[<Fact>] | |||
let ``A much larger limit`` () = | |||
sum [3; 5] 1000 |> should equal 233168 | |||
[<Fact>] | |||
let ``Three factors`` () = | |||
sum [7; 13; 17] 20 |> should equal 51 | |||
[<Fact>] | |||
let ``Factors not relatively prime`` () = | |||
sum [4; 6] 15 |> should equal 30 | |||
[<Fact>] | |||
let ``Some pairs of factors relatively prime and some not`` () = | |||
sum [5; 6; 8] 150 |> should equal 4419 | |||
[<Fact>] | |||
let ``One factor is a multiple of another`` () = | |||
sum [5; 25] 51 |> should equal 275 | |||
[<Fact>] | |||
let ``Much larger factors`` () = | |||
sum [43; 47] 10000 |> should equal 2203160 | |||
[<Fact>] | |||
let ``All numbers are multiples of 1`` () = | |||
sum [1] 100 |> should equal 4950 | |||
[<Fact>] | |||
let ``No factors means an empty sum`` () = | |||
sum [] 10000 |> should equal 0 | |||
[<Fact>] | |||
let ``The only multiple of 0 is 0`` () = | |||
sum [0] 1 |> should equal 0 | |||
[<Fact>] | |||
let ``The factor 0 does not affect the sum of multiples of other factors`` () = | |||
sum [3; 0] 4 |> should equal 3 | |||
[<Fact>] | |||
let ``Solutions using include-exclude must extend to cardinality greater than 3`` () = | |||
sum [2; 3; 5; 7; 11] 10000 |> should equal 39614537 | |||