-6

The number fizzbuzz is composed of two numbers that is fizz and buzz.

fizz represents the numbers that are divisible by 3 and buzz are the numbers that are divisible by 5.

combining both fizzbuzz, are the numbers that is divisible by both 3 and 5. So how can we get all the fizzbuzz numbers sum in a given range.

vijay pratap
  • 11
  • 1
  • 8
  • Hi. Try calculating the first fizzbuzz number without using a loop, then to find the next one you only have to increase it by 15, and then another 15, etc. until you've reached or passed the maximum range value. – Lasse V. Karlsen Aug 22 '18 at 09:55
  • Thanks that will be better and more optimized solution. – vijay pratap Aug 22 '18 at 09:56
  • Hi, it is fine to have your current approach as part of the question (it is actually indicated to do so, in order to prove that you made some effort) -- as it was before the edit. You should just mention at the end of the question that "I am interested in a more optimized solution". – danbanica Aug 22 '18 at 09:58
  • If you need to ask about FizzBuzz, and similar exercises, that may indicate that you need to learn a bit more about the basics. Jeff Atwood, who invented the original FizzBuzz exercise, [wrote about that](https://blog.codinghorror.com/fizzbuzz-the-programmers-stairway-to-heaven/). – S.L. Barth is on codidact.com Aug 22 '18 at 10:01
  • 1
    Do you know anything about sum of arithmetic progression? – MBo Aug 22 '18 at 10:02
  • yes, I know about arithmetic progression – vijay pratap Aug 22 '18 at 10:05
  • I would suggest writing your current code and submitting to https://codereview.stackexchange.com/ – Reuben Mallaby Aug 22 '18 at 10:22
  • 1
    @vijay pratap So apply this knowledge to the sequence of values `k*15` – MBo Aug 22 '18 at 10:43

1 Answers1

2

The fizzbuzz numbers which are less or equal to N are integers between 0 and N that are divisible by 15. Their sum is sum(15*i for i = 1 to N/15) That's equal to 15*sum(i for i i=1 to N/15), or 15*(N/15)*(1+N/15)/2. (Note, here / means rounding down integer division).

The sum of fizzbuzz numbers in the range [a, b] (that is, numbers greater than or equal to a and less than or equal to b), is equal to the sum of the fizzbuzz numbers less than or equal to b minus the sum of fizzbuzz numbers less than or equal to a-1. That's 15*(b/15)*(1+b/15)/2 - 15*((a-1)/15)*(1+(a-1)/15)/2 using the result in the first paragraph.

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118