Thread: Find the Value
View Single Post
Old 2007-07-10, 04:08   #7
m_f_h
 
m_f_h's Avatar
 
Feb 2007

24×33 Posts
Default

Quote:
Originally Posted by davar55 View Post
[Would someone double-check this value?]
it seems ok - I believe the following code is correct:
Code:
cnt0(n) = { local( cnt=0, m=divrem(n,10)); if( n<10, return(1));
/* Let n = 10 m[1] + m[2]. Count 0's in m[1] and multiply this by m[2]+1: This
 * is the number of 0's in { 10 m[1],..., n } minus 1 (trailing 0 of 10 m[1]).
 */
  n=m; while( n=divrem(n[1],10), cnt += !n[2] ); cnt *= (m[2] + 1);
/* now add the number of 0's occuring in last position in {0,...,10 m[1]}
 * (this is equal to 1+m[1]) plus 10 * the number of 0's in { 1,...,m[1]-1 }.
 */
  cnt+1+m[1]+10*(cnt0( m[1]-1 )-1)
}
m_f_h is offline   Reply With Quote