## Lychrel Numbers

### September 1, 2015

We begin with functions `r`

and `s`

, described above, and `p?`

that determines if its argument is a palindrome:

(define (r n) (let loop ((n n) (z 0)) (if (zero? n) z (loop (quotient n 10) (+ (* z 10) (modulo n 10)))))) (define (s n) (+ n (r n))) (define (p? n) (= n (r n)))

Then we write the function that either returns the chain that proves its input is not a lychrel number or dies if it reaches the bound *b*, which defaults to 100:

(define (lychrel n . b) (let ((b (if (null? b) 100 (car b)))) (let loop ((b b) (z n) (xs (list n))) (if (zero? b) (list n #f) (let ((x (s z))) (if (p? x) (reverse (cons x xs)) (loop (- b 1) x (cons x xs))))))))

Here are some examples:

> (lychrel 281) (281 463 827 1555 7106 13123 45254) > (lychrel 196) (196 #f)

And here we compute the list of lychrel numbers less than a thousand:

> (do ((n 1 (+ n 1))) #f (when (not (cadr (lychrel n 1000))) (display n) (newline))) 196 295 394 493 592 689 691 788 790 879 887 978 986

We set a high bound because the claim that a number is a Lychrel number requires a rather high confidence, though chains ending in a palindrome after more than 100 steps are uncommon.

You can run the program at http://ideone.com/ndemYe.

Advertisements

In Python.

In Scala.

Checks until 1000000000L

I hope that my understanding is correct what chain of numbers was required.

F# solution:

190L is not Lychrel number -> 45254L

191L is not Lychrel number -> 191L

192L is not Lychrel number -> 6996L

193L is not Lychrel number -> 233332L

194L is not Lychrel number -> 2992L

195L is not Lychrel number -> 9339L

Lychrel number -> 196L

197L is not Lychrel number -> 881188L

198L is not Lychrel number -> 79497L

199L is not Lychrel number -> 3113L

200L is not Lychrel number -> 202L

201L is not Lychrel number -> 303L

202L is not Lychrel number -> 202L

203L is not Lychrel number -> 505L

204L is not Lychrel number -> 606L

205L is not Lychrel number -> 707L

206L is not Lychrel number -> 808L

207L is not Lychrel number -> 909L

208L is not Lychrel number -> 1111L

209L is not Lychrel number -> 1111L

210L is not Lychrel number -> 222L

211L is not Lychrel number -> 323L

212L is not Lychrel number -> 212L

213L is not Lychrel number -> 525L

214L is not Lychrel number -> 626L

215L is not Lychrel number -> 727L

216L is not Lychrel number -> 828L

217L is not Lychrel number -> 929L

218L is not Lychrel number -> 1331L

219L is not Lychrel number -> 2442L

220L is not Lychrel number -> 242L

221L is not Lychrel number -> 343L

222L is not Lychrel number -> 222L

223L is not Lychrel number -> 545L

224L is not Lychrel number -> 646L

225L is not Lychrel number -> 747L

226L is not Lychrel number -> 848L

227L is not Lychrel number -> 949L

228L is not Lychrel number -> 1551L

229L is not Lychrel number -> 2662L

230L is not Lychrel number -> 262L

231L is not Lychrel number -> 363L

232L is not Lychrel number -> 232L

233L is not Lychrel number -> 565L

234L is not Lychrel number -> 666L

235L is not Lychrel number -> 767L

236L is not Lychrel number -> 868L

237L is not Lychrel number -> 969L

238L is not Lychrel number -> 1771L

239L is not Lychrel number -> 2882L

240L is not Lychrel number -> 282L

241L is not Lychrel number -> 383L

242L is not Lychrel number -> 242L

243L is not Lychrel number -> 585L

244L is not Lychrel number -> 686L

245L is not Lychrel number -> 787L

246L is not Lychrel number -> 888L

247L is not Lychrel number -> 989L

248L is not Lychrel number -> 1991L

249L is not Lychrel number -> 5115L

250L is not Lychrel number -> 505L

251L is not Lychrel number -> 707L

252L is not Lychrel number -> 252L

253L is not Lychrel number -> 1111L

254L is not Lychrel number -> 4444L

255L is not Lychrel number -> 6666L

256L is not Lychrel number -> 8888L

257L is not Lychrel number -> 11011L

258L is not Lychrel number -> 1221L

259L is not Lychrel number -> 2332L

260L is not Lychrel number -> 545L

261L is not Lychrel number -> 747L

262L is not Lychrel number -> 262L

263L is not Lychrel number -> 2662L

264L is not Lychrel number -> 4884L

265L is not Lychrel number -> 45254L

266L is not Lychrel number -> 88555588L

267L is not Lychrel number -> 13431L

268L is not Lychrel number -> 1441L

269L is not Lychrel number -> 2552L

270L is not Lychrel number -> 585L

271L is not Lychrel number -> 787L

272L is not Lychrel number -> 272L

273L is not Lychrel number -> 5115L

274L is not Lychrel number -> 9559L

275L is not Lychrel number -> 44044L

276L is not Lychrel number -> 8836886388L

277L is not Lychrel number -> 15851L

278L is not Lychrel number -> 1661L

279L is not Lychrel number -> 2772L

280L is not Lychrel number -> 2662L

281L is not Lychrel number -> 45254L

282L is not Lychrel number -> 282L

283L is not Lychrel number -> 2552L

284L is not Lychrel number -> 4774L

285L is not Lychrel number -> 6996L

286L is not Lychrel number -> 8813200023188L

287L is not Lychrel number -> 233332L

288L is not Lychrel number -> 1881L

289L is not Lychrel number -> 2992L

290L is not Lychrel number -> 2552L

291L is not Lychrel number -> 6996L

292L is not Lychrel number -> 292L

293L is not Lychrel number -> 2992L

294L is not Lychrel number -> 9339L

Lychrel number -> 295L

296L is not Lychrel number -> 881188L

297L is not Lychrel number -> 79497L

298L is not Lychrel number -> 3113L

299L is not Lychrel number -> 5335L

300L is not Lychrel number -> 303L

Haskell:

Somebody asked how to compute Lychrel numbers in Python at Stack Overflow today, and I wrote this program:

I’ve been getting in to monad programming lately, so here’s a solution that defines a “bounded computation” monad and then uses that to limit the length of sequence we look at. It’s a bit longer than Francesco’s solution above (but it does terminate for input eg. of 196):

Here’s one in Racket:

(define string-reverse (compose list->string reverse string->list))

(define (string-palindrome? s) (equal? (string-reverse s) s))

(define (f n) (+ n (string->number (string-reverse (number->string n)))))

(define (lychrel? n [tolerance 10000])

(let loop ([n n] [result (list n)] [i 1])

(if (>= i tolerance)

#t

(if (string-palindrome? (number->string n))

result

(let ([y (f n)]) (loop y (append result (list y)) (+ i 1)))))))

Apologies, I forgot to enclose that in source code tags!

Here’s another Haskell solution – to save some effort, perform all calculations on lists of digits, rather than the numbers themselves. We just need some conversions and an add with carry function (smallest digit comes first in the list representation):

#include

int main()

{

int num,rev=0,num1,val=0;

printf(“Enter the number\n”);

scanf(“%d”,&num);

num1 = num;

while(1)

{

for(;num1;val = val*10+num1%10,num1/=10);

if(num==val)

{

printf(“we get the num -> %d\n”,num);

break;

}

printf(“num become -> %d\n”,num);

num1 = num+val;

num = num1;

val=0;

}

}

O/P:

Enter the number

281

num become -> 281

num become -> 463

num become -> 827

num become -> 1555

num become -> 7106

num become -> 13123

Enter the number

261

num become -> 261

num become -> 423

we get the num -> 747

Enter the number

731

num become -> 731

we get the num -> 868

you can enter any number you will get it’s corresponding palindrome number