There is sequence 1,12,123,1234,..., 12345678910,.... Now you are given two integers A and B, you have to find the number of integers from A th number to B th(inclusive) number, which are divisible by 3.
For example, let A=3. B=5. So, the numbers in the sequence are,123, 1234, 12345. And 123, 12345 are divisible by 3. So, the result is 2.
WHAT'S the result while A=10 & B=110?
Problem related to programming
A man is not finished when he's defeated, he's finished when he quits.
Re: Problem related to programming
As far as I remember, this problem can be solved in O(n) complexity. We can easily see $F_i$ is divisible by 3 iff $i=3k/3k+2$. Then it is easy to determine how many $F_i$ is divisible by $3$ for $0\leq i \leq B,A$. And then determining $G_A-G_{B-1}$ if we denote $G_x=\sum^{x}_{i=0,3|F_i} 1$. I would have given my code (as I solved it earlier) But I have lost it so .
And the answer you needed is 67.
And the answer you needed is 67.
Please read Forum Guide and Rules before you post.
Use $L^AT_EX$, It makes our work a lot easier!
Nur Muhammad Shafiullah | Mahi
Use $L^AT_EX$, It makes our work a lot easier!
Nur Muhammad Shafiullah | Mahi
Re: Problem related to programming
Define a function $\gamma$ by \[\gamma(n)=1\text{ if }n \equiv2\pmod3\]
otherwise, \[\gamma(n)=0\]
The number of such numbers divisible by $3$ is with $a$ digits is \[f(a)=2\lfloor{\frac {a}{3}}\rfloor+\gamma(a)\]
For numbers between $a$ and $b$, the required number is \[f(b)-f(a-1)\]
$O(1)$ solution
otherwise, \[\gamma(n)=0\]
The number of such numbers divisible by $3$ is with $a$ digits is \[f(a)=2\lfloor{\frac {a}{3}}\rfloor+\gamma(a)\]
For numbers between $a$ and $b$, the required number is \[f(b)-f(a-1)\]
$O(1)$ solution
One one thing is neutral in the universe, that is $0$.