Here you have a simple double loop:
for i=1;i<=n;i++ for j=1; j<=n/6; j++
therefore, if you calculate how many times the loop body will be executed (i.e. how many times this line of code sum = sum + 1; will be executed), you will see the following:
n * n / 6 = nΒ² / 6
which in terms of the notation big O:
About (NΒ²)
because we donβt care about the constant term, because when n grows, the constant term does not make a (big) difference if it is there or not!
When and only when you are fully aware of what I'm saying, you can better understand this pleasant question: Big O, how do you calculate / approximate it?
However, please note that such questions are more suitable for Theoretical Computer Science , rather than SO.
gsamaras
source share