To Infinity, and Beyond!

Palindromes are numbers which are equal to their reverse. For example-

121 = 121 {121} = {121}

Calculate

n 1 n \sum_{n}^{\infty} \frac{1}{n}

where n n is a palindrome in base 10.


The answer is 3.37028.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

Brock Brown
Jan 7, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from fractions import Fraction as frac
def palindrome(number):
    s = str(number)
    return s == s[::-1]
total = 0
n = 1
infinity = 100000
while n <= infinity:
    if palindrome(n):
        total += frac(1, n)
    n += 1
print "Answer:", float(total)

This has to be a CS problem, I think because this is a fact found with the help of programming.

Kartik Sharma - 6 years, 5 months ago

Log in to reply

Updated to CS

Agnishom Chattopadhyay - 6 years, 5 months ago

Trying from 4-5 days at last gave off , can't do without computer science - I don't know anything about it

U Z - 6 years, 4 months ago

Log in to reply

That is the point exactly! School Education imparts the illussion in the students that every problem in the real world looks like a textbook problem and can be solved with analytical methods.

Agnishom Chattopadhyay - 6 years, 4 months ago

Log in to reply

@Agnishom Chattopadhyay I agree with you! It is so damn true.

Kartik Sharma - 6 years, 4 months ago

@Agnishom Chattopadhyay I am extremely sorry! I won't post any problems like these. Sorry :(

Vatsalya Tandon - 6 years, 4 months ago

Log in to reply

@Vatsalya Tandon Please do not be sorry. I'm glad that you posted this problem. Post more of these! :)

I was not criticising you, I was criticising the school system which is throwing us into the darkness.

Agnishom Chattopadhyay - 6 years, 4 months ago

good coding..........

Md Mohibullah - 6 years, 4 months ago

Shouldn't it be infinity. It contains the harmonic series \sum_{n=1}^inf = inf ?

Amogh Kamath - 6 years, 4 months ago

Log in to reply

It is very much a convergent series. Because of the very high rate of growth of the denominator, I would suppose.

http://en.wikipedia.org/wiki/Palindromic number#Sum of the reciprocals

Kp Govind - 6 years, 4 months ago

We can show convergence pretty quickly.

Consider the 2 k + 1 2k+1 digit numbers. The number of palindromes is 1 0 k + 1 10^{k+1} . Each reciprocal is bounded above by 1 1 0 2 k \frac{1}{ 10^{2k} } ,hence their sum is bounded above by 1 1 0 k 1 \frac{ 1}{ 10^{k-1} } . Hence, the sum of all reciprocals of palindromes with odd number of digits, is 10 1 1 10 = 100 9 \frac{10}{1 - \frac{1}{10}} = \frac{100}{9} . Similarly, for the 2 k 2k digit numbers, the number of palindromes is 1 0 k 10^k , and each reciprocal is bounded above by 1 1 0 2 k 1 \frac{1}{10^{2k-1} } . Hence their sum is bounded above by 1 1 0 k 1 \frac{1}{10^{k-1}} . Hence, the sum of all reciprocals of palindromes with even number of digits is 1 1 1 10 = 10 9 \frac{1}{1 - \frac{1}{10} } = \frac{10}{9} .

Hence, the sum is finite.

Calvin Lin Staff - 5 years, 8 months ago
Mehul Chaturvedi
Mar 14, 2015

Python:

1
2
3
4
5
6
7
8
def re(n):   #i'm making reverse function as re(n)
    return int(str(n)[::-1])
l=0
for m in range(1,999999):
    k=re(m)
    if k==m:
        l+=1.0/m
print 'answer is:',l

Did you learn Python at school ?

A Former Brilliant Member - 6 years, 3 months ago

Log in to reply

No, @Azhaghu Roopesh M I learned it 3 weeks ago ,with the help of sir, @Chew-Seong Cheong .And still i'm am not perfect

Mehul Chaturvedi - 6 years, 2 months ago
Arulx Z
May 17, 2015
1
2
3
4
5
double sum = 0;
for(int n = 1; n < 100000000;n++)
    if(Integer.toString(n).equals(new StringBuilder(Integer.toString(n)).reverse().toString()))
        sum += 1d / (double) n;
System.out.println(sum);

OMG Java code is smaller than Python!

Is that so:

1
reduce(lambda x,y: x + int(str(y)[::-1]==str(y))*(1.0/y), xrange(1,10**7),0)

Agnishom Chattopadhyay - 5 years, 8 months ago

Log in to reply

Haha ok :)

Arulx Z - 5 years, 8 months ago

No. It's not. Less line doesn't make it smaller. Anyway, great job with the code.

Horisadi Afyama - 6 years ago
Chuck Jerian
Mar 26, 2015

sum=0.0

for x in range(1,10):
    #print(x)
    sum += 1.0 / x
print(sum)
xsum=sum
for max in  [100,1000,10000,1000000]:
    sum=xsum
    for o in (0,1):
        for i in range(1,max):
            if o == 0:
                eii = 1
            else:
                eii = 10
            for ii in range(0,eii):
                x=str(i)
                y=x[::-1]
                if o == 1:
                    m = str(ii)
                else:
                    m = ""
                z=x+m + y
                #print(o,i,ii," ",z)

                sum += (1.0 / int(z))
    print(sum)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...