Algorithms: count the number of multiples of the number A that perfectly divides the number B

What is the best way to count the number of multiples of the number A that perfectly divides the number B.

This is the secondary problem for one of the questions I am solving in codechef.
This is the best thing that comes to mind.

int func (int A, int B) {
int res = 0;
int i = 1;
while (A * i <= B) {
yes (! (B% (A * i))) {
res ++;
}
i ++;
}
come back;
}

However, this is the waiting time for some of the cases. They tell me that this can be solved in O (sqrt (n)). Any help would be appreciated.