Last time I had stopped optimizing this the problem more because we were having a discussion where string concatination is a costly opperation. So yesterday I was talking with Nishchay how can about the previous blog and other things at that point we started on optimizing this further avoiding string contactination as well.

In Competetive programming, this is optimize even further, where % modulo operation is replaced by addtion and subtraction. This approach is also dicussed in same video which was shared in previous post.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
let c3=0
let c5=0
for (number = 1; number <= 100; number++) {
  c3++;
  c5++;
  var outcome = ""
  if(c3==3) {outcome += "fizz";c3=0}
  if(c5==5) {outcome += "buzz";c5=0}
  if(outcome) console.log(outcome)
  else console.log(number)
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
c3 = 0
c5 = 0
for number in range(1, 101):
    c3+=1
    c5+=1
    outcome = ""
    if(c3 == 3) :
        outcome += 'fizz'
        c3=0
    if(c5 == 5) :
        outcome += 'buzz'
        c5=0
    if(outcome != "") : print(outcome)
    else: print(number)

While discussion this approach, with Vijay and Anup there was discussion that string contactination is costly operation compared to modulo. And instead of going above approach last approach to the problem was much better.

So yesterday I and Nishchay were dicussing this problem and working on a solution where we could avoid string contactination as well. While doing I tried to combine all of the example from previous post and come up with something as below

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
let c3=0
let c5=0
for (number = 1; number <= 100; number++) {
  c3++;
  c5++;
  var outcome = ""
  if(c3==3 && c5==5) {console.log("fizzbuzz");c3=0;c5=0}
  else if(c3==3){ console.log("fizz"); c3=0}
  else if(c5==5){ console.log("buzz"); c5=0}
  else console.log(number)
}
1
2
3
4
5
6
7
8
9
c3 = 0
c5 = 0
for number in range(1, 101):
    c3+=1
    c5+=1
    if(c3 == 3 and c5 == 3): print('fizzbuzz')
    elif(c3 == 3): print('fizz')
    elif(c5 == 5): print('buzz')
    else: print(number)

While dicussing Nishchay pointed out that in this solution also we incrementing three times why not try to lower number of increments and addition and make it further optimize. Then we come up the below solution.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
var c3 = 3;
var c5 = 5;
for(number = 1; number <=100; number++){
  if(number == c5 && number == c3){
    console.log("fizzbuzz");
    c3 += 3;
    c5 += 5;
  } else if(number == c3){
    console.log("fizz");
    c3 += 3;
  } else if(number == c5){
    console.log("buzz");
    c5 += 5;
  } else console.log(number);
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
c3 = 3
c5 = 5
for number in range(1, 101) :
    if (number == c3 and number == c5) :
      print('fizzbuzz')
      c3 += 3
      c5 += 5
    elif (number == c3) : 
      print('fizz')
      c3 += 3
    elif(number == c5):
      print('buzz')
      c5 += 5
    else: print(number)

A small problem with task to optimize it help me to write two blog post :D. If we find more better approach then there might be a third post to cover that :D.