matherp
Posts: 101
Joined: Tue May 02, 2017 10:54 am

Re: Double base palindromes

Tue Sep 21, 2021 9:19 pm

If I cheat then I can get 85 mSec on the Pico at 250MHz in MMBasic. The CSUB capability of MMBasic is very powerful.

Code: Select all

CSub reverse
 00000000
 4B106800 401A0842 00404B0F 43024018 0893480E 480E4003 40020092 480D4313
 4002091A 011B480C 431A4003 01D2BA13 2201D403 421A085B 600BD0FC 604B2300
 46C04770 55555555 AAAAAAAA 33333333 CCCCCCCC 0F0F0F0F F0F0F0F0
End CSub
Timer =0
GoTo start
bc:
reverse x,z
If x=z Then
Inc T,x
EndIf
Return
start:
Option DEFAULT INTEGER
Dim y$
Dim q=Peek(varaddr y$)
For a=100001 To 900009 Step 200002
For b=0 To 90090 Step 10010
x=b+a
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Next :Next
For a=10001 To 90009 Step 20002
For b=0 To 9090 Step 1010
x=b+a
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Next :Next
For a=1001 To 9009 Step 2002
For b=0 To 990 Step 110
x=b+a
GoSub bc
Next b,a
For a=101 To 909 Step 202
For b=0 To 90 Step 10
x=b+a
GoSub bc
Next :Next
For x=11 To 99 Step 22
GoSub bc
Next
For x=1 To 9 Step 2
Inc T,x
Next
Print Timer
Print T

Code: Select all

void reverse(long long int * in, long long int * out)
{
    unsigned int x=*in;
    x = ((x >> 1) & 0x55555555u) | ((x & 0x55555555u) << 1);
    x = ((x >> 2) & 0x33333333u) | ((x & 0x33333333u) << 2);
    x = ((x >> 4) & 0x0f0f0f0fu) | ((x & 0x0f0f0f0fu) << 4);
    x = ((x >> 8) & 0x00ff00ffu) | ((x & 0x00ff00ffu) << 8);
    x = ((x >> 16) & 0xffffu) | ((x & 0xffffu) << 16);
    while(!(x & 1))x>>=1;
    *out = x;
}

ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: Double base palindromes

Wed Sep 22, 2021 2:11 am

hippy wrote:
Tue Sep 21, 2021 6:20 pm
Using pure Python -

Code: Select all

    def Test(n):
        global sum
        b = bin(n)[2:]
        for x in range(len(b) >> 1):
            if b[x] != b[-(x+1)]:
                return
        sum += n

Code: Select all

Sum is 872187
Run time is 0.768571 seconds @ 250.0 MHz
Here is a version in standard BBC Basic based on my earlier Go code:

Code: Select all

1000 REM euler36.bas -- Double-base palindromes
1010 REM Written 2021 by Eric Olson
1020 n%=1000000
1030 PRINT "euler36--Double-base palindromes"
1040 PRINT "Written 2021 by Eric Olson":PRINT
1050 PRINT "Summing palindromes from 1 up to ";n%;"...";

1100 fm%=@%:@%=&01020000
1110 tstart%=TIME
1120 po%=1:xo%=FNpalo(po%)
1130 pe%=1:xe%=FNpale(pe%)
1140 c%=0:done%=FALSE
1150 REPEAT
1160   IF xo%<xe% THEN
1170     IF xo%>=n% THEN
1175       done%=TRUE
1176     ELSE
1180       IF FNispal(xo%) THEN
1190         c%=c%+xo%
1200       ENDIF
1210       po%=po%+1:xo%=FNpalo(po%)
1215     ENDIF
1220   ELSE
1230     IF xe%>=n% THEN
1235       done%=TRUE
1236     ELSE
1240       IF FNispal(xe%) THEN
1250         c%=c%+xe%
1260       ENDIF
1270       pe%=pe%+1:xe%=FNpale(pe%)
1275     ENDIF
1280   ENDIF
1290 UNTIL done%
1300 tstop%=TIME
1310 PRINT "done":PRINT
1320 PRINT "Sum of all double-base palindromes: ";c%:@%=&090A
1330 PRINT "Calculation time ";(tstop%-tstart%)/100;" seconds"
1340 END

2000 DEF FNpale(i%)
2010 LOCAL x$,j%
2020 x$=STR$(i%)
2030 FOR j%=LEN(x$) TO 1 STEP -1
2040   x$=x$+MID$(x$,j%,1)
2050 NEXT j%
2060 =VAL(x$)

2100 DEF FNpalo(i%)
2110 LOCAL x$,j%
2120 x$=STR$(i%)
2130 IF LEN(x$)>1 THEN
2140   FOR j%=LEN(x$)-1 TO 1 STEP -1
2150     x$=x$+MID$(x$,j%,1)
2160   NEXT j%
2170 ENDIF
2180 =VAL(x$)

2200 DEF FNstrbin(i%)
2210 LOCAL x$,j%
2220 IF i%=0 THEN
2230   ="0"
2240 ELSE
2250   REPEAT
2260     x$=CHR$(48+(i% AND 1))+x$
2270     i%=i%>>1
2280   UNTIL i%=0
2290   =x$
2300 ENDIF

2400 DEF FNispal(i%)
2410 LOCAL x$,j%,m%
2420 x$=FNstrbin(i%)
2430 m%=LEN(x$)/2
2440 FOR j%=1 TO m%
2560   IF MID$(x$,j%,1)<>MID$(x$,LEN(x$)-j%+1,1) THEN
2570     =FALSE
2580   ENDIF
2590 NEXT j%
2600 =TRUE
Even though the program spends most of its time converting integers to binary strings using a hand-written function in Basic, I find the execution speed pretty good. For example, at the default 125 MHz clock speed I obtain

Code: Select all

>RUN
euler36--Double-base palindromes
Written 2021 by Eric Olson

Summing palindromes from 1 up to 1000000...done

Sum of all double-base palindromes: 872187
Calculation time 6.26 seconds
Note the program was written interactively on the Pico and illustrates that the line numbers in BBC Basic are used only for editing and not flow control. Does anyone know whether this code would run on the original BBC Micro?

I expect doubling the CPU clock speed would cut the runtime in half and crunching the source also speed it up a little. I've written a C extension that replaces FNstrbin but have not checked how much faster that makes the program run.

ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: Double base palindromes

Wed Sep 22, 2021 3:59 am

ejolson wrote:
Wed Sep 22, 2021 2:11 am
I've written a C extension that replaces FNstrbin but have not checked how much faster that makes the program run.
Rewriting FNstrbin as

Code: Select all

2200 DEF FNstrbin(i%)
2210 LOCAL x%
2220 SYS "strbin", i% TO x%
2230 =$$x%
where strbin is a system call to a library function written in C results in

Code: Select all

>RUN
cheat36--Double-base palindromes
Written 2021 by Eric Olson

Summing palindromes from 1 up to 1000000...done

Sum of all double-base palindromes: 872187
Calculation time 3.36 seconds
Further optimizing this as

Code: Select all

2200 DEF FNstrbin(i%)
2210 LOCAL x%
2220 SYS strbin%, i% TO x%
2230 =$$x%
where

Code: Select all

1025 strbin%=SYS "strbin"
leads to

Code: Select all

cheat36--Double-base palindromes
Written 2021 by Eric Olson

Summing palindromes from 1 up to 1000000...done

Sum of all double-base palindromes: 872187
Calculation time 3.26 seconds
This optimization shaves 3 seconds from the original 6.26 second run time; however, it can't be developed interactively on the Pico. Most versions of BBC Basic include an inline assembler that could be used for similar performance tuning. The built-in assembler for the Pico is not finished.

More information about adding C code implementing the strbin library call to the Pico version of BBC Basic may be found at

viewtopic.php?p=1916281#p1916281

matherp
Posts: 101
Joined: Tue May 02, 2017 10:54 am

Re: Double base palindromes

Wed Sep 22, 2021 8:33 am

With thanks to the brains on the backshed BB I've got the pure interpreted Basic version down to 260mSec. I'd be interested in how this code runs in BBC Basic which I think is a lighter weight interpreter and should logically be faster. MMBasic though does use some optimisation tricks like hashed variable lookup.

Code: Select all

Timer =0
GoTo start
bc:
y$=Bin$(x):w=Len(y$):z=w\2
For i=2 To z
If Peek(byte q+i)<>Peek(byte q+w+1-i) Then Return
Next
Inc T,x
Return
start:
Option DEFAULT INTEGER
Dim y$
Dim q=Peek(varaddr y$)
For a=100001 To 900009 Step 200002
For b=0 To 90090 Step 10010
x=b+a
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Inc x,1100
GoSub bc
Next :Next
For a=10001 To 90009 Step 20002
For b=0 To 9090 Step 1010
x=b+a
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Inc x,100
GoSub bc
Next :Next
For a=1001 To 9009 Step 2002
For b=0 To 990 Step 110
x=b+a
GoSub bc
Next b,a
For a=101 To 909 Step 202
For b=0 To 90 Step 10
x=b+a
GoSub bc
Next :Next
For x=11 To 99 Step 22
GoSub bc
Next
For x=1 To 9 Step 2
Inc T,x
Next
Print Timer
Print T

hippy
Posts: 10955
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Double base palindromes

Wed Sep 22, 2021 11:51 am

ejolson wrote:
Wed Sep 22, 2021 3:59 am
This optimization shaves 3 seconds from the original 6.26 second run time; however, it can't be developed interactively on the Pico. Most versions of BBC Basic include an inline assembler that could be used for similar performance tuning. The built-in assembler for the Pico is not finished.
I have never been clear on what the rules are for calling C or Assembler code from an interpreted language when it comes to benchmarking that language's performance. Ultimately one could code it all in C or Assembler, make a call to that, and claim it's just as fast as C or Assembler.

That can also apply to using PIO in any language and using Assembler in a C program.

I would personally consider it a bit of a cheat even if the language offers in-built user accessible native compilers or assemblers. That capability is a part of the language but it is still stepping outside the actual language being benchmarked.

I'm not even sure on '@micropthon.native' and '@micropython.viper' but would personally accept them as being a part of the MicroPython language, little different to using '-O' optimisations and other CFLAGS to cause C to speed things up.

I have never actually understood what the point of algorithmic challenges are when they get beyond determining optimal algorithm and move to evolving the fastest implementation but at least it's usually a civilised language war. Perhaps because players don't recognise what they are engaged in,

The implementation conclusions always seem to be the same; C is faster than interpreted languages, interpreted languages will execute faster by calling C code. That has pretty much always been self-evident and I don't see the need to endlessly keep proving it time after time.

ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: Double base palindromes

Wed Sep 22, 2021 2:12 pm

hippy wrote:
Wed Sep 22, 2021 11:51 am
The implementation conclusions always seem to be the same; C is faster than interpreted languages, interpreted languages will execute faster by calling C code. That has pretty much always been self-evident and I don't see the need to endlessly keep proving it time after time.
That right. For me the focus isn't about proving anything but understanding the tradeoffs between speed, maintainability, ease of use and algorithmic techniques. My experience is that writing and looking at the code others have written to solve a specific problem helps me learn how to do things and what's good for what much easier than reading the fine manual.

I would even claim that programming, like mathematics and music, is one of those subjects which is learned better by doing it yourself and through examples.

At another level, a careful analysis of how different hardware and software can be used to perform a specific task benefits even more those who design computers and programming languages. This is an opportunity to demonstrate and test new ideas and then refine them.

Good designs and system engineers don't happen by accident nor does a music student become an effective composer without the experience of having heard an orchestra perform their compositions.

Does anyone ever attend the symphony for the purpose of deciding whether tubas are better than piccolos? Is it possible this thread is the orchestra?
Last edited by ejolson on Wed Sep 22, 2021 2:32 pm, edited 2 times in total.

lurk101
Posts: 1066
Joined: Mon Jan 27, 2020 2:35 pm
Location: Cumming, GA (US)

Re: Double base palindromes

Wed Sep 22, 2021 2:27 pm

hippy wrote:
Wed Sep 22, 2021 11:51 am
I have never been clear on what the rules are for calling C or Assembler code from an interpreted language when it comes to benchmarking that language's performance. Ultimately one could code it all in C or Assembler, make a call to that, and claim it's just as fast as C or Assembler.
Clearly, it's cheating.
The implementation conclusions always seem to be the same; C is faster than interpreted languages, interpreted languages will execute faster by calling C code. That has pretty much always been self-evident and I don't see the need to endlessly keep proving it time after time.
You don't understand why a good pissing match attracts a crowd?

What I don't understand is why anyone would use Basic!
Last edited by lurk101 on Wed Sep 22, 2021 2:32 pm, edited 1 time in total.

lurk101
Posts: 1066
Joined: Mon Jan 27, 2020 2:35 pm
Location: Cumming, GA (US)

Re: Double base palindromes

Wed Sep 22, 2021 2:30 pm

ejolson wrote:
Wed Sep 22, 2021 2:12 pm
Does anyone ever attend the symphony for the purpose of deciding whether tubas are better than piccolos?
Sure, and violins are the best!

ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: Double base palindromes

Wed Sep 22, 2021 2:35 pm

lurk101 wrote:
Wed Sep 22, 2021 2:30 pm
ejolson wrote:
Wed Sep 22, 2021 2:12 pm
Does anyone ever attend the symphony for the purpose of deciding whether tubas are better than piccolos?
Sure, and violins are the best!
If the violin players didn't think so, it would not be a very good orchestra. But what if the tuba player agreed?

pidd
Posts: 2798
Joined: Fri May 29, 2020 8:29 pm
Location: Wirral, UK
Contact: Website

Re: Double base palindromes

Wed Sep 22, 2021 3:36 pm

lurk101 wrote:
Wed Sep 22, 2021 2:27 pm
What I don't understand is why anyone would use Basic!
Any language can be compiled or interpreted. Any compiler can optimise so its down to the performance of the compiler rather than simply the language or programmer - although some languages have gaping holes in their functionality.

Basic is widely acknowledged as one of the easier languages to learn, use and debug. Why make life more difficult?

Having a choice of interpreted or compiled is really good for debugging, unfortunately not all Basics have both and I've never come across a C interpreter (although some debugging tools have it internally).

For many years I programmed in assembly or Basic, I really couldn't understand why you would want to use one of the "weird" compromises in between. C offered me no advantage over assembly, in assembly the syntax was extremely simple and let me get on with programming instead of fighting a language - this has changed both assembly and C are more complex now, assembly more-so.

The only formal languages I hold qualifications in are C, C++ and Java and I find the last two too difficult to use reliably and efficiently on an occasional basis - if I was full-time programming then it might be different.

Just because GCC and similar compilers are ahead of the game in performance does not mean C is a better language. Most of what is written in C can be directly translated to Basic and vice-versa, they are only a different language, not a different technology. therefore there performance could be identical if the same amount of effort had been applied to a basic compiler as C.

All languages have moved on, when I first used C it didn't have floating point arithmetic - Basic did! For C you had to add on a non-standard (non-free) library and have all your numbers as strings. On the other hand, Basic used to have compulsory line numbers - which have pro's and con's, I can't think of any pro's of not having floating point capability.

lurk101
Posts: 1066
Joined: Mon Jan 27, 2020 2:35 pm
Location: Cumming, GA (US)

Re: Double base palindromes

Wed Sep 22, 2021 3:58 pm

I think choice of language has little to do with performance or learning ease and more to do with personality. Like in other endeavors, there are generalist who will learn dozens of languages superficially, there are specialists who choose to master a few, and then the rare animal that will do both.

ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: Double base palindromes

Wed Sep 22, 2021 4:08 pm

pidd wrote:
Wed Sep 22, 2021 3:36 pm
lurk101 wrote:
Wed Sep 22, 2021 2:27 pm
What I don't understand is why anyone would use Basic!
Any language can be compiled or interpreted. Any compiler can optimise so its down to the performance of the compiler rather than simply the language or programmer - although some languages have gaping holes in their functionality.
While the compiler undeniably makes a difference, so does the language design when it comes to optimisation. Though a bit of a blogvert, the article

https://www.nextplatform.com/2021/03/22 ... s-in-2021/

touches on some interesting performance considerations when designing the Julia programming language, the started goal of which was to replace the mishmash of Python, C and Fortran currently used in scientific computing with a language that combines the performance of a compiler with the convenience of an interpreter.

Julia has a read evaluate print loop with better history and interactive editing than Python and, in fact, any REPL I've seen for other languages. However, the language design itself allows for optimised just-in-time compilation and this, in my opinion, was more important in achieving the goal of having the performance of a compiler and convenience of an interpreter.

Then there is Go, which instead focused on having the convenience of a compiler with the performance of an interpreter and again largely succeeded. The fact is that not all music written for the tuba sounds good when played on a violin.

lurk101
Posts: 1066
Joined: Mon Jan 27, 2020 2:35 pm
Location: Cumming, GA (US)

Re: Double base palindromes

Wed Sep 22, 2021 4:18 pm

ejolson wrote:
Wed Sep 22, 2021 4:08 pm
pidd wrote:
Wed Sep 22, 2021 3:36 pm
lurk101 wrote:
Wed Sep 22, 2021 2:27 pm
What I don't understand is why anyone would use Basic!
Any language can be compiled or interpreted. Any compiler can optimise so its down to the performance of the compiler rather than simply the language or programmer - although some languages have gaping holes in their functionality.
While the compiler undeniably makes a difference, so does the language design when it comes to optimisation. Though a bit of a blogvert, the article

https://www.nextplatform.com/2021/03/22 ... s-in-2021/

touches on some interesting performance considerations when designing the Julia programming language, the started goal of which was to replace the mishmash of Python, C and Fortran currently used in scientific computing with a language that combines the performance of a compiler with the convenience of an interpreter.

Julia has a read evaluate print loop with better history and interactive editing than Python and, in fact, any REPL I've seen in other languages. However, the language design itself allows for optimised just-in-time compilation and this, in my opinion, was more important in achieving the goal of having the performance of a compiler and convenience of an interpreter.

Then there is Go, which instead focused on having the convenience of a compiler with the performance of an interpreter and again largely succeeded. The fact is that not all music written for the tuba sounds good when played on a violin.
I don't know Julia. I'll need to take a look. But it seems to me that any effort towards gains in one area has led to deficiencies elsewhere. Go's unpredictable memory management for example.

pidd
Posts: 2798
Joined: Fri May 29, 2020 8:29 pm
Location: Wirral, UK
Contact: Website

Re: Double base palindromes

Wed Sep 22, 2021 5:02 pm

There is too much choice already, any new language will be very unlikely to make it in the long term and languages that are a mash-up of other languages tend to get the ire from all sides.

I always thing of C as an argument based syntax and basic as a function/operator based syntax, both are fine in their own right but to me C has more detail instead of "natural language" eg the difference in argument handling of printf and scanf in C is generally an unnecessary detail compared to Basic's philosophy (I might be out of date on that).

I had my old Coral handbook out the other day, even back then that became a mash-up of algol, fortran and a few other languages.

I don't understand the popularity of Python, it doesn't appear to have anything uniquely positive in its repertoire. - my bias is against object orientated languages its another abstraction layer, take it or leave it, I choose to leave it and stick to procedural from my assembly roots.

Looking at most of the code in this thread, the C code and Basic code would be directly translatable, there is no real reason one executable should be faster or bigger than the other. In fact is there anything in a modern C which is unique compared to a modern basic, both have evolved to the same requirements.

Something I've never spotted (or simply not recognised) in any language is full recursive support, you always program the "recursity", it should be possible to develop a syntax to simplify recursion by specifying an equation and a test. While I quite like recursion I avoid it because its easy to make a mistake and (sometimes) difficult to debug - and there is always the problem of the length of the stack (or equivalent).

matherp
Posts: 101
Joined: Tue May 02, 2017 10:54 am

Re: Double base palindromes

Wed Sep 22, 2021 5:07 pm

I had my old Coral handbook out the other day,
Now there's a language I haven't heard referenced for a long time. Much of my early coding was in CORAL66 which was the mandated language for the nuclear power station monitoring and control systems we were implementing (UK)

hippy
Posts: 10955
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Double base palindromes

Wed Sep 22, 2021 5:29 pm

lurk101 wrote:
Wed Sep 22, 2021 2:27 pm
You don't understand why a good pissing match attracts a crowd?
I definitely do which is why I consider taking algorithmic challenges beyond finding an optimal algorithm to be be little more than sophisticated trolling with plausible deniability.
lurk101 wrote:
Wed Sep 22, 2021 2:27 pm
What I don't understand is why anyone would use Basic!
Plenty of reasons but I'm not stepping into that particular language war battle at this time.

User avatar
jahboater
Posts: 7551
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Double base palindromes

Wed Sep 22, 2021 6:11 pm

pidd wrote:
Wed Sep 22, 2021 5:02 pm
I don't understand the popularity of Python, it doesn't appear to have anything uniquely positive in its repertoire.
IMHO it is remarkably elegant, powerful, and has much basic stuff that's familiar to C programmers. There is direct support for doing almost anything imaginable. It is widely available. My choice for an interpreted language.

However many, if not most of these modern, powerful, languages rely on C for both their implementation and the heavy lifting in the support libraries.
Therefore C, is even more powerful, or perhaps "capable" is a better word.

Many high level languages have an easy mechanism for calling code written in C, but C has no such need to do the reverse.
You may call inline assembler from C (usually with zero overhead!), but its not often needed.

I suggest also "modern" Fortran as an interesting language. Interesting because it is one of the few not derived from Algol.
It is also a highly capable "multi-paradigm" language, with nearly 60 years of development behind it (the compiler is called "gfortran" on the Pi).
Last edited by jahboater on Wed Sep 22, 2021 6:23 pm, edited 1 time in total.

User avatar
RichardRussell
Posts: 1112
Joined: Thu Jun 21, 2012 10:48 am

Re: Double base palindromes

Wed Sep 22, 2021 6:22 pm

matherp wrote:
Wed Sep 22, 2021 8:33 am
I'd be interested in how this code runs in BBC Basic which I think is a lighter weight interpreter and should logically be faster.
You might think that, but there are a couple of reasons why it isn't the case. Firstly BBC BASIC doesn't have a Bin$() function, and secondly (my) BBC BASIC's integers are 64-bits which is overkill for this application. The fastest I've managed to achieve is 0.42 seconds using this conversion of your code:

Code: Select all

   10 TIME=0
   20 Sum=0
   30 Log2=LOG2
   40 FOR A%=100001 TO 900009 STEP 200002
   50   FOR B%=0 TO 90090 STEP 10010
   60     FOR X%=A%+B% TO A%+B%+9900 STEP 1100
   70       PROCbc(X%)
   80     NEXT
   90   NEXT
  100 NEXT
  110 FOR A%=10001 TO 90009 STEP 20002
  120   FOR B%=0 TO 9090 STEP 1010
  130     FOR X%=A%+B% TO A%+B%+900 STEP 100
  140       PROCbc(X%)
  150     NEXT
  160   NEXT
  170 NEXT
  180 FOR A%=1001 TO 9009 STEP 2002
  190   FOR B%=0 TO 990 STEP 110
  200     PROCbc(A%+B%)
  210   NEXT
  220 NEXT
  230 FOR A%=101 TO 909 STEP 202
  240   FOR B%=0 TO 90 STEP 10
  250     PROCbc(A%+B%)
  260   NEXT
  270 NEXT
  280 FOR X%=11 TO 99 STEP 22
  290   PROCbc(X%)
  300 NEXT
  310 FOR N%=1 TO 9 STEP 2
  320   Sum += N%
  330 NEXT
  340 PRINT "Sum of all double-base palindromes: "; Sum
  350 PRINT "Calculation time: "; TIME/100; " seconds"
  360 END
  370
  380 DEF PROCbc(N%)
  390 LOCAL P%,Q%
  400 P%=N%:Q%=2<<(LOGN%/Log2)
  410 WHILE P%
  420   Q%DIV=2
  430   IF (P%AND1) <> (N%DIVQ%AND1) ENDPROC
  440   P%DIV=2
  450 ENDWHILE
  460 Sum += N%
  470 ENDPROC
Last edited by RichardRussell on Wed Sep 22, 2021 6:38 pm, edited 1 time in total.

hippy
Posts: 10955
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Double base palindromes

Wed Sep 22, 2021 6:30 pm

pidd wrote:
Wed Sep 22, 2021 5:02 pm
I don't understand the popularity of Python, it doesn't appear to have anything uniquely positive in its repertoire.
But nothing majorly negative once one gets over the indentation and superfluous syntax fluff.

I would say most of the arguments for using it are the same as for using Basic. I strongly suspect it got used and favoured by people who did not have an actual Basic to hand and did not want to use C. That's pretty much how I got into Python when the Pi was released and I had to choose a cross-platform language other than Basic.

When one isn't going to choose C or a C-like language, and there isn't a credible Basic, there wasn't a lot of choice - Java, Perl, Pascal, Python, and things esoteric. For me Python looked the best option and I guess that was so for others, including Raspberry Pi. Today one could add Lua, JavaScript and Rust to the choices.

Had 'mono' fully supported VB6 I would probably have gone that route, or with Xojo (RealBASIC) if it had been free and worked on ARM. I did look at what Basic options there were but none appealed and Python still came out tops.

After a decade I still believe I made the right choice. And especially so with MicroPython and CircuitPython now available for the Pico.
pidd wrote:
Wed Sep 22, 2021 5:02 pm
Looking at most of the code in this thread, the C code and Basic code would be directly translatable, there is no real reason one executable should be faster or bigger than the other.
Indeed. The main difference comes from C being compiled while Basic is usually interpreted. The only real difference in size or speed should come down to implementation of the compiler and its libraries. GCC should have an advantage there because it has had far more people honing it over the years than most other languages have.

lurk101
Posts: 1066
Joined: Mon Jan 27, 2020 2:35 pm
Location: Cumming, GA (US)

Re: Double base palindromes

Wed Sep 22, 2021 6:57 pm

hippy wrote:
Wed Sep 22, 2021 5:29 pm
lurk101 wrote:
Wed Sep 22, 2021 2:27 pm
You don't understand why a good pissing match attracts a crowd?
I definitely do which is why I consider taking algorithmic challenges beyond finding an optimal algorithm to be be little more than sophisticated trolling with plausible deniability.
Hmm... It was in response to this from hippy (I think?)
I have never actually understood what the point of algorithmic challenges are when they get beyond determining optimal algorithm
Last edited by lurk101 on Wed Sep 22, 2021 8:23 pm, edited 2 times in total.

ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: Double base palindromes

Wed Sep 22, 2021 7:37 pm

lurk101 wrote:
Wed Sep 22, 2021 6:57 pm
hippy wrote:
Wed Sep 22, 2021 5:29 pm
lurk101 wrote:
Wed Sep 22, 2021 2:27 pm
You don't understand why a good pissing match attracts a crowd?
I definitely do which is why I consider taking algorithmic challenges beyond finding an optimal algorithm to be be little more than sophisticated trolling with plausible deniability.
Hmm... It was in response to this from ejolson
I have never actually understood what the point of algorithmic challenges are when they get beyond determining optimal algorithm
I don't see anything in your post which I wrote? Could you please check your quotes.

hippy
Posts: 10955
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Double base palindromes

Wed Sep 22, 2021 7:52 pm

[Deleted] - Issue resolved in later post
Last edited by hippy on Thu Sep 23, 2021 12:30 pm, edited 1 time in total.

ejolson
Posts: 8628
Joined: Tue Mar 18, 2014 11:47 am

Re: Double base palindromes

Wed Sep 22, 2021 8:11 pm

pidd wrote:
Wed Sep 22, 2021 5:02 pm
Looking at most of the code in this thread, the C code and Basic code would be directly translatable, there is no real reason one executable should be faster or bigger than the other. In fact is there anything in a modern C which is unique compared to a modern basic, both have evolved to the same requirements.
I think one reason most of the code in this thread is directly translatable is because it actually is a direct translation of the algorithm used by the first C code which showed good performance. Note that I provided one program

viewtopic.php?p=1915484#p1915484

that intersected the set of base 2 palindromes with the set of base 10 palindromes and used closures to do so. While the closures are a bit superficial in how they are used, it would be interesting to see whether the EVAL function along with dynamic scoping in BBC Basic could be used in a similar way.

As this thread is in the Pico section, one is somewhat constrained by which programming languages are available. At this point I understand that to be
  • C/C++ and Assembler
  • MicroPython and CircuitPython
  • BBC and MicroMite Basic
  • Inform
with partial support for
  • Free Pascal
  • Rust
Are there any language options for the Pico left out?

While those Basic interpreters add significantly to the variety, the Pico doesn't currently lend itself to exploring different programming languages in the same way the Raspberry Pi does. There is no Fortran, Go, Haskell, Java, JavaScript, Julia, Kotlin, Lisp, Ruby, Scratch or Scheme among others.

If following the popularity ratings on the TIOBE index

https://www.tiobe.com/tiobe-index/

the most urgent additions would be Java followed by C#. If looking for variety, I think Haskell and Scheme would round out the choices in a more interesting way. Given the current state of world affairs, maybe a military-strength implementation of Ada would be the most practical.

lurk101
Posts: 1066
Joined: Mon Jan 27, 2020 2:35 pm
Location: Cumming, GA (US)

Re: Double base palindromes

Wed Sep 22, 2021 8:24 pm

ejolson wrote:
Wed Sep 22, 2021 7:37 pm
I don't see anything in your post which I wrote? Could you please check your quotes.
My apology. Fixed the misquote, but too late. Damage done.

lurk101
Posts: 1066
Joined: Mon Jan 27, 2020 2:35 pm
Location: Cumming, GA (US)

Re: Double base palindromes

Wed Sep 22, 2021 8:43 pm

ejolson wrote:
Wed Sep 22, 2021 8:11 pm
Given the current state of world affairs, maybe a military-strength implementation of Ada would be the most practical.
pioasm has a module called ada_output.cpp for generating Ada includes.

https://github.com/JeremyGrosser/rp2040_hal
https://github.com/JeremyGrosser/pico_bsp
https://github.com/JeremyGrosser/pico_examples

Return to “General”