### Author Topic: Palindrome Lychrel Numbers  (Read 7606 times)

#### ZXDunny

• Guest
##### Re: Palindrome Lychrel Numbers
« Reply #15 on: January 17, 2016, 08:32:51 pm »
Yeah, all numbers in SpecBAS are stored as extended precision floats so scientific notation cuts in at 15 digits and runs through to 19 digits in scientific precision.

Ah well. At least it works (after a fashion!):

Code: [Select]
`10 DEF FN r\$(a\$)=IIF\$(LEN a\$<=1,a\$,FN r\$(a\$(2 TO))+a\$(1))20 x=1,k=100: DO WHILE k>0: DO WHILE x<>VAL FN r\$(STR\$ x): x+=1: LOOP: y=x,z=50,y+=VAL FN r\$(STR\$ y): DO WHILE (STR\$ y<>FN r\$(STR\$ y)) AND z>0: y+=VAL FN r\$(STR\$ y),z-=1: LOOP: IF z=0 THEN PRINT x;" ";: k-=1: END IF: x+=1: LOOP `
It's basically Tomaaz' code converted to SpecBAS with a simple string reverser function defined.

D.

#### B+

• Guest
##### Re: Palindrome Lychrel Numbers
« Reply #16 on: January 17, 2016, 08:55:25 pm »
Hi D,

Do I detect a recursive call in your reverse function definition?

#### ZXDunny

• Guest
##### Re: Palindrome Lychrel Numbers
« Reply #17 on: January 17, 2016, 09:03:04 pm »
Hi D,

Do I detect a recursive call in your reverse function definition?

Heh, yes you do - it's a simple way to reverse a string without needing more than one line

D.

#### B+

• Guest
##### Re: Palindrome Lychrel Numbers
« Reply #18 on: January 17, 2016, 09:13:38 pm »
Hi D,

Since you are still here a\$(2 to) is some kind of looping through the characters?

Edit: or a\$(2 to)+a\$(1) is all one structure, I am not seeing how the string is being rebuilt backwards.
« Last Edit: January 17, 2016, 09:20:24 pm by B+ »

#### ZXDunny

• Guest
##### Re: Palindrome Lychrel Numbers
« Reply #19 on: January 18, 2016, 01:14:23 pm »
Hi D,

Since you are still here a\$(2 to) is some kind of looping through the characters?

Edit: or a\$(2 to)+a\$(1) is all one structure, I am not seeing how the string is being rebuilt backwards.

I don't do that - check the brackets

D.

#### B+

• Guest
##### Re: Palindrome Lychrel Numbers
« Reply #20 on: January 18, 2016, 02:02:09 pm »
Hi D,

Since you are still here a\$(2 to) is some kind of looping through the characters?

Edit: or a\$(2 to)+a\$(1) is all one structure, I am not seeing how the string is being rebuilt backwards.

I don't do that - check the brackets

D.

Oh, yeah I missed one  ) in between, nothing like fresh morning eyes, so SpecBAS has Python like string sections also...
wait this is different '2 to' is ? Time to consult manual... this is very interesting function definition!

#### ZXDunny

• Guest
##### Re: Palindrome Lychrel Numbers
« Reply #21 on: January 18, 2016, 06:16:07 pm »
Hi D,

Since you are still here a\$(2 to) is some kind of looping through the characters?

Edit: or a\$(2 to)+a\$(1) is all one structure, I am not seeing how the string is being rebuilt backwards.

I don't do that - check the brackets

D.

Oh, yeah I missed one  ) in between, nothing like fresh morning eyes, so SpecBAS has Python like string sections also...
wait this is different '2 to' is ? Time to consult manual... this is very interesting function definition!

String slicing in Sinclair BASIC was revolutionary at the time - while other BASICs were using LEFT\$/MID\$/RIGHT\$, we got the utterly beautiful "TO" argument.

When following any string element (whether it's a literal or variable or the result of a function), you can append a slicer.

a\$(1) references the first character in the string. a\$(2) the second and so on.
a\$(1 TO 10) returns the first ten characters.
a\$( TO 10) does the same.
a\$(3 TO ) returns the characters in a\$ from the third character on.

It's very, very nice.

D.

#### B+

• Guest
##### Re: Palindrome Lychrel Numbers
« Reply #22 on: January 18, 2016, 06:22:56 pm »
Yes nice to slice, but how you use it in recursive function is amazing.

I mean how it picks one character off adds it to front of a\$... until a\$ is reversed, I might be misunderstanding or likely NOT understanding how it works.
« Last Edit: January 18, 2016, 06:32:17 pm by B+ »

#### jj2007

• Guest
##### Re: Palindrome Lychrel Numbers
« Reply #23 on: January 18, 2016, 06:55:55 pm »
a\$(1) references the first character in the string. a\$(2) the second and so on.

So what was the syntax for string arrays then?

Dim some\$()
Let some\$(1)="String 1"
Let some\$(2)="String 2"

#### ZXDunny

• Guest
##### Re: Palindrome Lychrel Numbers
« Reply #24 on: January 18, 2016, 07:52:47 pm »
a\$(1) references the first character in the string. a\$(2) the second and so on.

So what was the syntax for string arrays then?

Dim some\$()
Let some\$(1)="String 1"
Let some\$(2)="String 2"

Back in the day, all strings were arrays of characters. DIM a\$(10) returned a ten-character string in a\$. SpecBAS is a little different (though still retains that behaviour) in that DIM is more flexible - originally, DIM a\$(10,10) returned a 10x10 grid of single characters, which with hindsight was just wrong.

In SpecBAS, arrays take priority - if you're daft enough to declare a\$ as a string and also as an array, you're fine until you try to slice it -

A\$ will return the variable A\$
A\$(1) will return the first string in the array a\$()
A\$(1 TO 5) will return the first 5 characters of the variable a\$
A\$(1, TO 3) will return the first 5 characters of the first string in the array a\$()

It's best to avoid it if possible by not using the same names for variables and arrays!

D.

#### B+

• Guest
##### Re: Palindrome Lychrel Numbers
« Reply #25 on: January 18, 2016, 08:53:59 pm »
...
Back in the day, all strings were arrays of characters. DIM a\$(10) returned a ten-character string in a\$. SpecBAS is a little different (though still retains that behaviour) in that DIM is more flexible - originally, DIM a\$(10,10) returned a 10x10 grid of single characters, which with hindsight was just wrong.

In SpecBAS, arrays take priority - if you're daft enough to declare a\$ as a string and also as an array, you're fine until you try to slice it -

A\$ will return the variable A\$
A\$(1) will return the first string in the array a\$()
A\$(1 TO 5) will return the first 5 characters of the variable a\$
A\$(1, TO 3) will return the first 5 characters of the first string in the array a\$()

It's best to avoid it if possible by not using the same names for variables and arrays!

D.

That has got to be typo in last example A\$(1, to 3) will return first 3 characters of the first string in the array a\$ (or A\$) not 5. Interesting it is not A\$(1)(3).
BTW, if a\$ is string and A\$ is array is SpecBAS case sensitive? (I am practicing with SpecBAS right now.)
BTW(2)     no SPACE\$(n) nor SPC\$(n) for n spaces
« Last Edit: January 18, 2016, 09:02:01 pm by B+ »

#### ZXDunny

• Guest
##### Re: Palindrome Lychrel Numbers
« Reply #26 on: January 19, 2016, 01:15:48 am »
...
Back in the day, all strings were arrays of characters. DIM a\$(10) returned a ten-character string in a\$. SpecBAS is a little different (though still retains that behaviour) in that DIM is more flexible - originally, DIM a\$(10,10) returned a 10x10 grid of single characters, which with hindsight was just wrong.

In SpecBAS, arrays take priority - if you're daft enough to declare a\$ as a string and also as an array, you're fine until you try to slice it -

A\$ will return the variable A\$
A\$(1) will return the first string in the array a\$()
A\$(1 TO 5) will return the first 5 characters of the variable a\$
A\$(1, TO 3) will return the first 5 characters of the first string in the array a\$()

It's best to avoid it if possible by not using the same names for variables and arrays!

D.

That has got to be typo in last example A\$(1, to 3) will return first 3 characters of the first string in the array a\$ (or A\$) not 5. Interesting it is not A\$(1)(3).

Yeah, should be first three chars! Nicely spotted.

Quote
BTW, if a\$ is string and A\$ is array is SpecBAS case sensitive? (I am practicing with SpecBAS right now.)
BTW(2)     no SPACE\$(n) nor SPC\$(n) for n spaces

10 LET a big orange bus=20

Is the same as

20 LET ABIGORANGEBUS=20

And who needs SPACE\$?

10 LET a\$=" "*20

D.

#### B+

• Guest
##### Re: Palindrome Lychrel Numbers
« Reply #27 on: January 19, 2016, 02:42:32 am »
print "Thanks, "*100+"D"

EDIT: hmm... even with line number added that might NOT work in SpecBAS.
« Last Edit: January 19, 2016, 07:18:02 pm by B+ »

#### B+

• Guest
##### Re: Palindrome Lychrel Numbers
« Reply #28 on: January 20, 2016, 05:23:05 pm »
Well finally manage a one liner recursive DEF for reverse function. I know in grand scheme of things this is trivial but I have found it interesting learning.

Code: [Select]
`'recursive reverse.bas for SmallBASIC 0.12.2 [B+=MGA] 2016-01-16 started post 2016-01-20'test definition of interesting idea for recursive function definition'deconstruct D's single line reverse function recursive definition, works!'func revs(s) 'works'  if len(s)=1 then '    revs=s '  else '    revs=revs(mid(s,2))+left(s,1)'  end if 'end'pack above into two line recursive definition, this needs no additional variable help '(as doing it in straight forward looping and rebuilding with MID would need' (at least one var for loop and recommend one for build var) and works!)'func revs(s):if len(s)=1 then revs=s else revs=revs(mid(s,2))+left(s,1)'end' OK try packing down to one-liner' def wont work the error: "PARAM COUNT ERROR@11 7=F 15, ' ha! So SmallBASIC finds D's one recursive DEF unbelievable too!'def revs(s)=iff(len(s)=1,s,revs(mid(s,2))+left(s,1) )'                     Holy Macaroni!!!'I was just about to give up on getting SmallBASIC version of D's one liner, 'but decide to try one more thing after trying to decode error message again'success!!!! from  one additional set of parenthesisdef revs(s)=iff( len(s)=1,s,( revs( mid(s,2) ) +left(s,1) ) )while 1  ?:input "Enter a string or number to reverse, just enter quits ";test  if len(test) then ? "reverse of '";test;"' is '";revs(test);"'" else exitwend`
« Last Edit: January 21, 2016, 12:40:11 am by B+ »

#### ZXDunny

• Guest
##### Re: Palindrome Lychrel Numbers
« Reply #29 on: January 20, 2016, 07:55:05 pm »
Love it

Nice job. Btw, that recursive function is a pretty simple one. You should see the version that doesn't use IIF!

Code: [Select]
`10 DEF FN a\$(s\$)=VAL\$ """""+s\$(LEN s\$)+VAL\$ """"""""""+FN a\$(s\$( TO LEN s\$-1))""( TO 2+16*(LEN s\$>1))"( TO 2+47*(LEN s\$>0))`
You see what we had to put up with before SpecBAS came along...

D.