Teaching students is fun, but it can turn embarrassing, and that is what happened to me a few days ago. It was the first meeting of a university course, so I was covering something very basic: how to count the zeroes at the end of n! written in base b. The number of times a prime p divides n! is
⌊pn⌋+⌊p2n⌋+⌊p3n⌋+⋯
and the same formula, used carefully, gives the number of zeroes at the end of n!.
After explaining it I worked out the number of trailing zeroes of 200! in decimal, then asked with an evil smile for the number of trailing zeroes of 100! in hexadecimal. I already knew the answer is 24. To my surprise a student replied 24 within minutes, so I congratulated him. A minute later I checked his script and found that he had counted the trailing zeroes of 100! in decimal instead of hexadecimal, and both answers happened to be 24. I was a bit embarrassed, and now I want to know why the two answers agreed.
For an integer b≥2, let Z(n,b) be the number of zeroes that run backwards from the last digit of n! written in base b. Given n and x, count the pairs (b1,b2) with 2≤b1<b2 such that Z(n,b1)=Z(n,b2)=p for some integer p≥x.
The input has several lines. Each line contains two integers n (1≤n≤100000) and x (2≤x≤2500), separated by a space. The last line contains two zeroes and is not processed. Excluding that line, the input has at most 1000 lines.
For each line of input print the number of pairs (b1,b2) on its own line. Every input is such that no output value exceeds 5×1018.