diff --git a/Kotlin/6.Sum_square_difference/bartonstaley.kt b/Kotlin/6.Sum_square_difference/bartonstaley.kt new file mode 100644 index 0000000..0098460 --- /dev/null +++ b/Kotlin/6.Sum_square_difference/bartonstaley.kt @@ -0,0 +1,31 @@ +/* + Project Euler Question 6. Sum square difference + + Problem Statement: + + The sum of the squares of the first ten natural numbers is, + + 1^2 + 2^2 + ... + 10^2 = 385 + + The square of the sum of the first ten natural numbers is, + + (1 + 2 + ... + 10)^2 = 552 = 3025 + + Hence the difference between the sum of the squares of the first ten + natural numbers and the square of the sum is 3025 − 385 = 2640. + + Find the difference between the sum of the squares of the first one hundred + natural numbers and the square of the sum. + */ + +fun Int.square() = this * this + +fun sum(n: Int) = (n * (n + 1)) / 2 + +fun sumOfSquares(n: Int) = n * (n + 1) * (2 * n + 1) / 6 + +fun sumSquareDifference(n: Int) = sum(n).square() - sumOfSquares(n) + +fun main(args: Array) { + println("Sum square difference of 100: " + sumSquareDifference(100)) +} diff --git a/README.md b/README.md index 664b614..fa49e9e 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Happy Contributing! 😃 | 03 | [Largest prime factor](https://projecteuler.net/problem=3) | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | | :white_check_mark: | :white_check_mark: | | :white_check_mark: | | | | | 04 | [Largest palindrome product](https://projecteuler.net/problem=4) | :white_check_mark: | | | :white_check_mark: | | | :white_check_mark: | | | | | | | 05 | [Smallest multiple](https://projecteuler.net/problem=5) | :white_check_mark: | | | :white_check_mark: | | | | | :white_check_mark: | | | | -| 06 | [Sum square difference](https://projecteuler.net/problem=6) | :white_check_mark: | :white_check_mark: | | :white_check_mark: | | :white_check_mark: | :white_check_mark: | | | | | | +| 06 | [Sum square difference](https://projecteuler.net/problem=6) | :white_check_mark: | :white_check_mark: | | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | | | | | | | 07 | [10001st prime](https://projecteuler.net/problem=7) | :white_check_mark: | | | :white_check_mark: | | :white_check_mark: | :white_check_mark: | | | | | | | 08 | [Largest product in a series](https://projecteuler.net/problem=8) | | | | :white_check_mark: | | | | | | | | | | 09 | [Special Pythagorean triplet](https://projecteuler.net/problem=9) | :white_check_mark: | | | :white_check_mark: | | | | | | | | |