Skip to content

6 done in Kotlin #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Kotlin/6.Sum_square_difference/bartonstaley.kt
Original file line number Diff line number Diff line change
@@ -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<String>) {
println("Sum square difference of 100: " + sumSquareDifference(100))
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: | | | | | | | | |
Expand Down