You are using float division where integer division will do:
int((n-1)/3.0)
is better expressed as
(n-1)//3
e.g. use Python's floor division operator, don't use a float, then floor.
Using integer floor division won't run into rounding issues as you stretch floating point arithmetic beyond its limits.
You can see this rounding error happen when you push n large enough:
>>> n = 100000000
>>> int((int((n-1)/15.0)+1) * (int((n-1)/15.0)/2.0) * 15)
333333316666665
>>> ((n-1)//15+1) * ((n-1)//15//2) * 15
333333316666665
>>> n = 1000000000
>>> int((int((n-1)/15.0)+1) * (int((n-1)/15.0)/2.0) * 15)
33333333166666664
>>> ((n-1)//15+1) * ((n-1)//15//2) * 15
33333333166666665
The extra 1 there comes purely from the fact that you ran into the limits of floats:
>>> (int((n-1)/15.0)+1) * (int((n-1)/15.0)/2.0)
2222222211111111.0
>>> ((n-1)//15+1) * ((n-1)//15//2)
2222222211111111
>>> (int((n-1)/15.0)+1) * (int((n-1)/15.0)/2.0) * 15
3.3333333166666664e+16
I'm not sure why you keep subtracting one from n; that's not needed at all and leads to incorrect results. Perhaps you were trying to compensate for float rounding errors? The correct formulas are:
(((n // 3) + 1) * (n // 3)) // 2 * 3
(((n // 5) + 1) * (n // 5)) // 2 * 5
(((n // 15) + 1) * (n // 15)) // 2 * 15
giving me the correct output for any n:
>>> n = 1000000000
>>> sum_of_multiples_3 = (((n // 3) + 1) * (n // 3)) // 2 * 3
>>> sum_of_multiples_5 = (((n // 5) + 1) * (n // 5)) // 2 * 5
>>> sum_of_multiples_15 = (((n // 15) + 1) * (n // 15)) // 2 * 15
>>> sum_of_multiples_3 + sum_of_multiples_5 - sum_of_multiples_15
233333334166666668
I'd have used a function here to calculate multiples:
def sum_of_multiples(n, k):
k_in_n = n // k
return ((k_in_n + 1) * k_in_n) // 2 * k
so you can verify that it works by comparing to a brute-force sum:
>>> sum(range(0, 10000 + 1, 3)) == sum_of_multiples(10000, 3)
True
>>> sum(range(0, 10000 + 1, 5)) == sum_of_multiples(10000, 5)
True
>>> sum(range(0, 10000 + 1, 15)) == sum_of_multiples(10000, 15)
True
then use that to calculate the answer:
>>> sum_of_multiples(n, 3) + sum_of_multiples(n, 5) - sum_of_multiples(n, 15)
233333334166666668