Determine the number of integers in the range L to R (endpoints inclusive) whose sum of digits is divisible by 3.
EXPLANATION:
A well known theorem from elementary number theory goes as follows:
A number is divisible by 3 if and only if the sum of its digits is divisible by 3.
Thus, the number of integers in the range [L, R] whose sum of digits is divisible by 3 is equal to the number of integers in the range divisible by 3.
Another well known theorem from elementary number theory says:
The number of integers in the range [1, N] divisible by d is equal to \lfloor\frac N d \rfloor, where \lfloor x \rfloor is the largest integer less than x.
Therefore, the number of integers in the range [L, R] divisible by 3= number of integers in the range [1,R] divisible by 3- number of integers in the range [1,L-1] divisible by 3= \lfloor\frac R 3\rfloor -\lfloor\frac {L-1} 3\rfloor.
for i in range(int(input())):
l,r=map(int,input().split())
if l>r or r<3:
print(0)
continue
if(l==r and l%3==0):
print(1)
continue
if(l==r and l%3!=0):
print(0)
continue
if((r-l)<3 and r%3==0):
print(1)
continue
if((r-l)<3 and l%3==0):
print(1)
continue
if((r-l)<3):
print(0)
continue
for j in range(l,r+1):
print("j is ",j)
if(j%3==0):
break
for k in range(r,l,-1):
print(‘k is’,k)
if(k%3==0 and k!=0):
break
print((int(k//3)-int(j//3))+1)
my code is failing last test case can anyone please suggest the issue