Basicprogramming(.org) > Code and examples

Palindrome Lychrel Numbers

<< < (2/7) > >>

B+:
Hi Tomaaz,

HA! I started converting and modifying in SmallBASIC because it would run faster than JB but I started getting very strange results. A real head scratcher until I started printing intermediate results to debug code and realized the error of my ways. SmallBASIC has a precision of 14 decimal places before it gets real with exponents.

So Ruby, like Python can do extended integer math like JB (and probably faster) and SmallBASIC sure can't.

How long did Ruby take to the 500 rounds to see if the Palindrome number was Lychrel like?

Running my revised JB code now.

EDIT: 8 mins later got same set of numbers, dang it I forgot to change j test!

B+:
Confirmed one new number. (Why aren't the code tags working???) :???


--- Code: ---'palindrome Lychrel numbers for JB 2016-01-15 revise better copy than one posted B+=MGA
print "Palindrome Lychrel(-like in 500 rounds) numbers:"
global pcount
while pcount<100
    i=i+1
    if i=reverse(i) then
        t=i+reverse(i)
        for j=1 to 500
            scan
            p=reverse(t)
            if p=t then exit for else t=t+p
        next j
        if j>500 then call show i
    end if
wend
print:print "Palindrome Lychrel count is ";pcount

sub show pal
    print using("#######",pal);
    pcount=pcount+1
    if pcount mod 10 =0 then print
end sub

function reverse(num)
    snum$=str$(num)
    for I=1 to len(snum$)
        p$=mid$(snum$,I,1)+p$
    next
    reverse=val(p$)
end function



--- End code ---

Output
--- Code: ---Palindrome Lychrel(-like in 500 rounds) numbers:
   4994   8778   9999  11811  19591  22822  23532  23632  23932  24542
  24742  24842  24942  26362  27372  29792  29892  33933  34543  34743
  34943  39493  44744  46064  46164  46364  46564  46964  47274  47574
  48284  48584  48684  48884  49394  49794  53935  61916  62826  64046
  64346  64446  64846  66466  67976  68486  68986  73537  78587  79297
  82928  83038  83638  83838  84448  87378  87578  88388  89198  92229
  94449  94549  95359  95759  97479  98089  98289  98389  98589  98989
  99999 118811 119911 193391 219912 235532 236632 239932 246642 247742
 253352 259952 263362 269962 278872 289982 293392 294492 306603 317713
 339933 347743 348843 351153 361163 362263 364463 373373 376673 378873

Palindrome Lychrel count is 100

--- End code ---

Can you figure the missing number?

Tomaaz:

--- Quote from: B+ on January 15, 2016, 10:33:47 PM ---How long did Ruby take to the 500 rounds to see if the Palindrome number was Lychrel like?

--- End quote ---

The whole program (calculating and printing all 100 numbers) takes:

50 rounds

Python - 7s.
Ruby -  8s.

500 rounds

Python - 1min. 30s.
Ruby - 6min.

B+:
Yep! way better times JB takes a couple of minutes for 50 rounds and maybe 8 minutes for 500 rounds while I surf Internet.

Here is code to find the number eliminated going from 50 rounds to 500, kind of ridiculous but I needed practice. I thought it would be a snap but strings from TLOAD were loaded with junk besides the numbers and spaces.


--- Code: ---'find missing number.bas for SmallBASIC 0.12.2 2016-01-15 [B+=MGA]

dim a1(120),a2(120)
tload "pl1.txt",pl1,1
tload "pl2.txt",pl2,1
build="":idx1=1:idx2=1
for i=1 to len(pl1)
  if instr("1234567890",mid(pl1,i,1)) then
    build=build+mid(pl1,i,1)
  else
    if len(build) then
      insert a1,idx1,build
      idx1++
      build=""
    end if
  end if
next
build=""
for i=1 to len(pl2)
  if instr("1234567890",mid(pl2,i,1)) then
    build=build+mid(pl2,i,1)
  else
    if len(build) then
      insert a2,idx2,build
      idx2++
      build=""
    end if
  end if
next
for i=1 to (idx1-1)
  if a1(i)<>a2(i) then
    print a1(i);" is the first number eliminated by increasing rounds from 50 to 500."
    exit for
  end if
next
pause

--- End code ---

Here is code I used to see how many rounds (after 50 and before 500) that one number took to become palindrome.


--- Code: ---'Lychrel test for JB 2016-01-15 [B+=MGA]
while 1
    print:input "Enter a number to test if Lychrel like in 500 rounds <10 quits ";test
    if test<10 then end
    t=test+reverse(test)
    for j=1 to 500
        p=reverse(t)
        if p=t then exit for else t=t+p
    next j
    if j>500 then print test;" is Lychrel-like." else print "Palindrome found for ";test;" in ";j;" rounds."
wend

function reverse(num)
    snum$=str$(num)
    for I=1 to len(snum$)
        p$=mid$(snum$,I,1)+p$
    next
    reverse=val(p$)
end function


--- End code ---

Ha, 52 rounds to become palindrome for the one number eliminated by increasing from 50 to 500.

Tomaaz:

--- Quote from: B+ on January 16, 2016, 01:08:36 AM ---Yep! way better times JB takes a couple of minutes for 50 rounds and maybe 8 minutes for 500 rounds while I surf Internet.

--- End quote ---

I wasn't really thinking about speed when I created these scripts. So, here is a new version in Ruby:


--- Code: ---k, x = 100, 1
while k > 0
while x != x.to_s.reverse.to_i
x += 1
end
y, z = x, 50
y = y + y.to_s.reverse.to_i
while y.to_s != y.to_s.reverse and z > 0
y = y + y.to_s.reverse.to_i
z -= 1
end
if z == 0
puts x
k -= 1
end
x += 1
end

--- End code ---

This one takes 0.3 sec for 50 rounds, 2 sec for 500 and 5 min for 5000 rounds.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version