It does a lot of magic with modulo. My reasoning is as follows:
A=[a;b]
c=#A
If c is a multiple of 5, there is always the same number of multiples of 5 in A (c/5).
Otherwise, it depends on c%5 and a%5.
c%5==1:
(The sets are modulo 5 to make my point easier to see.)
a%5==0: {0,1,2,3,4,0} *
a%5==1: {1,2,3,4,0,1}
a%5==2: {2,3,4,0,1,2}
a%5==3: {3,4,0,1,2,3}
a%5==4: {4,0,1,2,3,4}
There's c/5+1 zeroes when a%5==0.
c%5==2:
a%5==0: {0,1,2,3,4,0,1} *
a%5==1: {1,2,3,4,0,1,2}
a%5==2: {2,3,4,0,1,2,3}
a%5==3: {3,4,0,1,2,3,4}
a%5==4: {4,0,1,2,3,4,0} *
There's c/5+1 zeroes when a%5 in {0,4}.
c%5==3:
a%5==0: {0,1,2,3,4,0,1,2} *
a%5==1: {1,2,3,4,0,1,2,3}
a%5==2: {2,3,4,0,1,2,3,4}
a%5==3: {3,4,0,1,2,3,4,0} *
a%5==4: {4,0,1,2,3,4,0,1} *
There's c/5+1 zeroes when a%5 in {0,3,4}.
c%5==4:
a%5==0: {0,1,2,3,4,0,1,2,3} *
a%5==1: {1,2,3,4,0,1,2,3,4}
a%5==2: {2,3,4,0,1,2,3,4,0} *
a%5==3: {3,4,0,1,2,3,4,0,1} *
a%5==4: {4,0,1,2,3,4,0,1,2} *
There's c/5+1 zeroes when a%5 in {0,2,3,4}.
Notice that if we extend the sets again, they will all have one more zero, since c%5==0 in that case.
We see that:
1. If a%5==0, there's always an extra zero.
2. The second half of the set begins at a number that depends on c%5. Specifically, {0}∪[6-c%5;4]
Et cetera for extending to an arbitrary power of 5.
EDIT: I'm not exactly sure what you people are doing. What the above does is count how many pental numbers in A end in some number of zeroes.
EDIT 2: Oh... I see, now.
EDIT 3: Fixed [125;126] bug:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
unsigned count_round_numbers(unsigned a,unsigned b,unsigned base,unsigned max_power){
unsigned zeroes=0,
power=base,
c=b-a+1;
for (unsigned i=1;i<=max_power;i++,power*=base){
zeroes+=c/power;
unsigned mod_c=c%power,
mod_a=a%power;
if (mod_c && (!mod_a || mod_a>=power-mod_c+1))
zeroes++;
}
return zeroes;
}
unsigned count_trailing_zeroes_in_fact_b_div_fact_a(unsigned a,unsigned b){
unsigned zeroes=0;
if (a==b){
unsigned temp=a;
while (!(temp%10)){
zeroes++;
temp/=10;
}
}else
//2,000,000,000 == 1304400000000 (13 digits) in pental
//2,000,000,000 == 1110111001101011001010000000000 (31 digits) in binary
zeroes=std::min(count_round_numbers(a,b,2,31),count_round_numbers(a,b,5,13));
return zeroes;
}
| |