>slow(1 sec time limit on a crap machine)
Your algorithm is O(n), ¿how big is B-A? You need to make it O(1)
Supposing N<2^32, the maximum `smallest positive integer which doesn't divide N' is less than 30 (¿how did I obtain that?)
Instead of a linear walk, work with the extremes. Given a number N, ¿how many numbers n<=N are not divisible by 2? Their strength will be 2
Now consider the numbers that are divisible by 2, ¿how many are not by 3? That's easy, two thirds.
...
The numbers that are divisible by 2, 3, 4, 5, 6 but not by 7: six sevenths
(be careful with modulus)
(I'll let you figure out the composite numbers)
Compute the strength of the numbers from 2 to 30. Store in an array.
1 2
|
for( int K=2; K<30; ++K)
total += how_many(N, K) * (strength[K]+1);
|
how_many counts
n<=N | mod(n,b)== 0, b<K and mod(n,K)!=0
Be careful to not recount the numbers from 2 to 30