Skip to content

Instantly share code, notes, and snippets.

@betaprojects
Last active September 11, 2020 21:14
Show Gist options
  • Save betaprojects/c3d4aaa9b546cd1b53c8a13c649120db to your computer and use it in GitHub Desktop.
Save betaprojects/c3d4aaa9b546cd1b53c8a13c649120db to your computer and use it in GitHub Desktop.
Project Euler & HackerRank problem 1 solution: Multiples of 3 and 5 - solved using Python
def sumn(n, d): # Sum natural numbers ≤ n that are divisible by d
n //= d
return d*n*(n+1) // 2
L = int(input('Enter an upper bound? '))
a, b = 3, 5
s = sumn(L-1, a) + sumn(L-1, b) - sumn(L-1, a*b)
print ("Sum of multiples of", a, "or", b, "below", L, "=", s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment