Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bounding this program to determine the sum of reciprocal integers not containing zero

Let A denote the set of positive integers whose decimal representation does not contain the digit 0. The sum of the reciprocals of the elements in A is known to be 23.10345.

Ex. 1,2,3,4,5,6,7,8,9,11-19,21-29,31-39,41-49,51-59,61-69,71-79,81-89,91-99,111-119, ...

Then take the reciprocal of each number, and sum the total.

How can this be verified numerically?

Write a computer program to verify this number.

Here is what I have written so far, I need help bounding this problem as this currently takes too long to complete:

Code in Java

import java.util.*; 

public class recip
{
    public static void main(String[] args)
    {
        int current = 0; double total = 0;

        while(total < 23.10245)
        {
            if(Integer.toString(current).contains("0"))
            {
                current++;
            }
            else
            {
                total = total + (1/(double)current);
                current++;
            }
            System.out.println("Total: " + total);
        }
    }
}
like image 684
Bobby S Avatar asked Oct 02 '10 21:10

Bobby S


2 Answers

This is not that hard when approached properly.

Assume for example that you want to find the sum of reciprocals of all integers starting (i.e. the left-most digits) with 123 and ending with k non-zero digits. Obviously there are 9k such integers and the reciprocal of each of these integers is in the range 1/(124*10k) .. 1/(123*10k). Hence the sum of reciprocals of all these integers is bounded by (9/10)k/124 and (9/10)k/123.

To find bounds for sum of all reciprocals starting with 123 one has to add up the bounds above for every k>=0. This is a geometric serie, hence it can be derived that the sum of reciprocals of integers starting with 123 is bounded by 10*(9/10)k/124 and 10*(9/10)k/123.

The same method can of course be applied for any combination of left-most digits. The more digits we examine on the left, the more accurate the result becomes. Here is an implementation of this approach in python:

def approx(t,k):
    """Returns a lower bound and an upper bound on the sum of reciprocals of
       positive integers starting with t not containing 0 in its decimal
       representation.
       k is the recursion depth of the search, i.e. we append k more digits
       to t, before approximating the sum. A larger k gives more accurate
       results, but takes longer."""
    if k == 0:
      return 10.0/(t+1), 10.0/t
    else:
        if t > 0:
            low, up = 1.0/t, 1.0/t
        else:
            low, up = 0, 0
        for i in range(10*t+1, 10*t+10):
            l,u = approx(i, k-1)
            low += l
            up += u
    return low, up

Calling approx(0, 8) for example gives the lower and upper bound: 23.103447707... and 23.103448107.... which is close to the claim 23.10345 given by the OP.

There are methods that converge faster to the sum in question, but they require more math. A much better approximation of the sum can be found here. A generalization of the problem are the Kempner series.

like image 69
Accipitridae Avatar answered Oct 06 '22 00:10

Accipitridae


For all values of current greater than some threshold N, 1.0/(double)current will be sufficiently small that total does not increase as a result of adding 1.0/(double)current. Thus, the termination criterion should be something like

 while(total != total + (1.0/(double)current))

instead of testing against the limit that is known a priori. Your loop will stop when current reaches this special value of N.

like image 29
Philip Starhill Avatar answered Oct 05 '22 23:10

Philip Starhill