Created
April 7, 2018 20:17
-
-
Save BlueInkAlchemist/8e85a5831d6e3afff5d01521a269a139 to your computer and use it in GitHub Desktop.
Sum all multiples of 3 & 5 up to 1000, using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1. Get the upper bounds for the arithmetic series. | |
## Ensure that no limit reaches 1000. | |
LIMIT=999 | |
upper_for_three = LIMIT // 3 | |
upper_for_five = LIMIT // 5 | |
upper_for_fifteen = LIMIT // 15 | |
# 2. Calculate sums from within individual bounds. | |
sum_three = 3*upper_for_three*(1 + upper_for_three) / 2 | |
sum_five = 5*upper_for_five*(1 + upper_for_five) / 2 | |
sum_fifteen = 15*upper_for_fifteen*(1 + upper_for_fifteen) / 2 | |
# 3. Calculate total of all sums. | |
total = sum_three + sum_five - sum_fifteen | |
# 4. Print the result in Terminal. | |
print(int(total)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment