Read opened thread for this problem to find out better solution. It's really interesting.
I was asking what is wrong with my code, tho i made mistake.
I tried looking for other codes to see how they where made, i keep crossing on PHP code, so i open thread
I think you can solve it in constant time on paper (or with one expression in your programming language), no need for loops
I have so low mathematical knowledge. Don't know these formulas at all.
For 3 and 5, yeah, you can for any bound (inclusive, use if you don't want to include it), it's just:
Which simplifies down to (the floor symbols indicate integer division):
For , this gives . For , we get as expected.
You can see the general pattern - sum up the multiples of each number required (here 3 and 5), and then subtract any numbers which are multiples of any two of them at the same time. You can generalize this to more than two numbers, but it gets pretty messy as you need individual sums to get the correct result for different numbers, so it gets impractical if you need to consider many different divisors.
If they have shown me how to do it from 0 - 1000 id know how to do it from 0 - 10000^^.
But yea, its kinda too much for me since you most know good math for those problems. I didn't get far with it, 3/10 problems solved and am pleased since i don't understand 70% of those questions.
Thanks on good mood.