Mini-Challenge: Rudin-Shapiro Sequence
|
12-09-2022, 10:46 PM
Post: #1
|
|||
|
|||
Mini-Challenge: Rudin-Shapiro Sequence
A simple programming challenge to stretch your brains before Valentin's next round: Just write the most efficient program you can for your favorite HP calculator to return at least 1000 terms of the Rudin-Shapiro sequence as an array or list. To be clear, I am referring to what the Wikipedia article calls r(n) but you may also submit a program for u(n) if you like for "extra credit"
The winner will be determined by the lowest product of (program size in bytes) * (execution time in seconds). For the RPL series, speed is to be timed on an HP 50g in approximate mode. For other calculators including the HP-71B and the Prime, execution time can only be compared with other programs on the same model of calculator. Older calculators can play along too, just compute as many terms as memory allows and scale the execution time accordingly. |
|||
12-10-2022, 05:54 AM
(This post was last modified: 12-11-2022 03:45 AM by Gerald H.)
Post: #2
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
Edit 2022-12-11: Supplied missing "→". CKSUM & SIZE remain correct.
For 48G, 49G & 50g. Code: CKSUM # 9B57h |
|||
12-10-2022, 07:50 AM
(This post was last modified: 12-10-2022 09:00 AM by C.Ret.)
Post: #3
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
For HP-28S,one algorithm, three submissions.
Version with extra \(u_k\) computations: Uk: « 0 SWAP BIN R→B →STR OVER WHILE 1 + 999 SUB DUP "11" POS DUP REPEAT ROT 1 + ROT ROT END DROP2 » Usage:375775 Uk returns 10. Rk: « -1 SWAP Uk ^ » LST1000: « 0 999 FOR k k Rk NEXT 1E3 →LIST 960 .1 BEEP » The list of the first 1000 terms of the Rudin-Shapiro sequence obtained in about 2'46". Version without extra \(u_k\): Rk: « 1 SWAP BIN R→B →STR 2 WHILE 1 + 999 SUB DUP "11" POS DUP REPEAT ROT NEG ROT ROT END DROP2 » Usage:375775 Rk returns 1. LST1000: « 0 999 FOR k k Rk NEXT 1E3 →LIST 960 .1 BEEP » The list of the first 1000 terms of the Rudin-Shapiro sequence obtained in about 2'26". Stand alone version: LST1000: « 0 999 FOR k 1 k BIN R→B →STR 2 WHILE 1 + 999 SUB DUP "11" POS DUP REPEAT ROT NEG ROT ROT END DROP2 NEXT 1E3 →LIST 960 .1 BEEP » The list of the first 1000 terms of the Rudin-Shapiro sequence obtained in about 2'18". Next challenge, trying to improuve speed efficiency of the list build-up by using the recursive algorithm. P.S.: All system settings expected to be default values, especially binary words size at 64 bits. |
|||
12-10-2022, 10:56 PM
(This post was last modified: 12-10-2022 10:58 PM by John Keith.)
Post: #4
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
(12-10-2022 05:54 AM)Gerald H Wrote: « There seems to be a missing right arrow before the second from last ←P as indicated above. It now runs and returns u(n). Neat program, I'm still trying to wrap my head around compiled local variables. |
|||
12-11-2022, 03:41 AM
(This post was last modified: 12-11-2022 03:49 AM by Gerald H.)
Post: #5
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
Correct, will re-edit.
Yes, compiled local variables are useful & the programme is meant as a demonstration of their utility, sadly I can't think of a previous example in programmes available on the forum. |
|||
12-11-2022, 05:20 AM
Post: #6
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
I've found a previous reference to "compiled local variables".
https://www.hpmuseum.org/forum/thread-14...#pid125758 |
|||
12-11-2022, 02:02 PM
Post: #7
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
Not quite to 1000, but I found a LCG with multiplier 14, increment 2 and prime 3449 that to my surprise will generate the first 33 bit entries of the Zero-one version of Golay-Rudin-Shapiro sequence in only 44 bytes, including the "RSS" label.
Code:
Yields: Code:
17bii | 32s | 32sii | 41c | 41cv | 41cx | 42s | 48g | 48g+ | 48gx | 50g | 30b |
|||
12-11-2022, 03:34 PM
(This post was last modified: 12-11-2022 03:46 PM by Allen.)
Post: #8
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
Output with the LCG internal state and the bit mask for a_n = bit7 .. except for a few entries, it's correct up through a_50
Code:
17bii | 32s | 32sii | 41c | 41cv | 41cx | 42s | 48g | 48g+ | 48gx | 50g | 30b |
|||
12-11-2022, 07:05 PM
Post: #9
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
Very cool program, Allen! I like those off-the-wall solutions.
|
|||
12-15-2022, 08:44 PM
(This post was last modified: 12-15-2022 08:58 PM by John Keith.)
Post: #10
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
Since interest in this challenge seems to have waned, I will post my solutions tomorrow afternoon. I have a 97 byte program which runs on any RPL calculator, and a faster 55 byte program which requires the 48G or newer. Let's see if anyone can do better, you have 24 hours!
|
|||
12-16-2022, 10:41 PM
(This post was last modified: 12-16-2022 10:42 PM by John Keith.)
Post: #11
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
As promised, here are my solutions to this challenge.
First, a 97 byte program for any RPL calculator. This is a simple implementation of the recurrence from the Wikipedia article. Given a number n on the stack, returns 2n+1 terms. An input of 500 returns 1001 terms in 3.25 seconds on my 50g. Code:
Next, a larger but faster version which avoids the 2 MOD test but requires extra code to avoid problems with odd number inputs. 121 bytes. An input of 500 returns 1000 terms in 1.94 seconds on my 50g. Code:
Next, a completely different sort of program which requires the 48G or newer because it uses the listability of functions (called "automatic list processing" in the manuals). Given a number n on the stack, returns 2^n terms. For example an input of 10 will return 1024 terms in 1.22 seconds on my 50g. 55 bytes. Code:
Finally, a similar program for u(n) aka A014081. Also returns 2^n terms, 55.5 bytes. An input of 10 returns 1024 terms in 1.19 seconds. Code:
|
|||
12-17-2022, 02:30 AM
Post: #12
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
The Thue-Morse sequence isn't that hard (it's very fast on the Hp50g as one can compute en masse with X = {0 1} and follow up with X = X + (1 - X). The Nth element is the parity of the number of bits set in the binary representation of N. The anti-Thue sequence (I don't know an accepted name) is the sequence that counts the number of 0's in the number N.
|
|||
12-17-2022, 04:03 PM
(This post was last modified: 12-17-2022 06:25 PM by Allen.)
Post: #13
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
@ John,
Thank you for an interesting problem!! These challenges are always a good cause to learn more about other areas of math I knew little about. While I was trying to squeeze more juice out of this, I learned some interesting things about the Shapiro Polynomials and some other items. One of the potential avenues I explored was using run length encoding for A020987. There are some really interesting properties of this function and these polynomials they represent!! Regarding the run length, for n up to 2**20 there are no more than 4 0's or 1' in a row. I think it's possible to prove that there are never more than 4 in a row, and Brillhard and Morton have made a really heavy dent in proving some related theorems- generating 16 pages of math, but without a grand unified proof. (humorous aside: the authors of this paper say on p.3 "We find ourselves being distracted from the original question by our conjecture, which is interesting in it's own right. Let's indulge ourselves and pursue this pattern question, ignoring the original question for the time being." The irony of this statement, both in the context of this series, this original forum post, and their paper seems appropriately recursive! In fact, this may be one of the best quotes I've ever seen with respect to applied math research. Perhaps the second-best math quote I've seen is on p. 15: "What strange numbers!" - Seems like the kind of thing Ramanujan or Erdős, or Hawking might put on their gravestones.) For the first 1024*1024, here are the counts of the run lengths: Code:
This seems a beautiful coincidence! In the original sequence count for 2**20 is not quite as nice, but close: Code:
For various reasons, I encoded a run length of 1 as 0 and 2 as 1 and so on, so the first 20 entries of A020987 would be run-length encoded as: Code:
when packed as binary encoded, the sequence for the first 2*20 A020978 entries fed into zlib.compress yields only 2791 bytes. Due to the semi-cyclical nature of even the 1X compressed data, feeding those bytes back into zlib again returns only 274 bytes!! A 500-fold reduction in size (when 2X compressed) can sometimes be a bellwether of deeper mathematical mysteries- or at least some very long repeated sub-sequences. Behold, 2**20 entries of A020987 are here: Code:
Or I could examine 1 line of python and stop procrastinating my Saturday morning house cleaning: Code:
17bii | 32s | 32sii | 41c | 41cv | 41cx | 42s | 48g | 48g+ | 48gx | 50g | 30b |
|||
12-17-2022, 06:30 PM
Post: #14
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
(12-17-2022 02:30 AM)ttw Wrote: The Thue-Morse sequence isn't that hard (it's very fast on the Hp50g as one can compute en masse with X = {0 1} and follow up with X = X + (1 - X). The Nth element is the parity of the number of bits set in the binary representation of N. The anti-Thue sequence (I don't know an accepted name) is the sequence that counts the number of 0's in the number N. The sequence that counts the number of 0's in binary n is A023416. The parity of the number of 0's is A059448. I don't know if there are special names for either sequence. Both can be computed by similar small, fast programs. This was actually the reason behind my posting of this challenge. I have observed that nearly all sequences describing properties of binary numbers have a fractal pattern, that is they are self-similar at different scales. This self-similarity leads to fast and simple algorithms as you noted for the Thue-Morse sequence. |
|||
12-17-2022, 06:52 PM
(This post was last modified: 12-17-2022 06:52 PM by John Keith.)
Post: #15
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
(12-17-2022 04:03 PM)Allen Wrote: @ John, Thanks for your interesting observations! I don't think that the patterns you found in the run length are coincidences at all, see my post above in response to ttw. Of course, I don't have a grand unified theory to explain the patterns either. As it happens, the way I discovered the pattern that led to my HP48G program is by looking at A020987. The Rudin-Shapiro sequence (A020985) is a confusing wall of ones and minus signs but seeing it as a sequence of ones and zeros made the pattern much clearer visually. One additional note on the subject of run-length encoding: the ListExt commands LRPCT AND LRPC→ perform run-length encoding and decoding respectively. They are very fast. |
|||
12-18-2022, 07:53 PM
(This post was last modified: 12-18-2022 07:56 PM by Allen.)
Post: #16
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
A 42S program to print (n...1) entries: ( for n up to 1023)
Code:
The bit twiddling was adapted from here. Code:
17bii | 32s | 32sii | 41c | 41cv | 41cx | 42s | 48g | 48g+ | 48gx | 50g | 30b |
|||
12-18-2022, 08:30 PM
Post: #17
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
(12-16-2022 10:41 PM)John Keith Wrote: Some nice programming! I just noticed you can save 10% (5 bytes) of the program size by using the NEG function rather than "0 SWAP -" (50 bytes) Code:
17bii | 32s | 32sii | 41c | 41cv | 41cx | 42s | 48g | 48g+ | 48gx | 50g | 30b |
|||
12-18-2022, 09:26 PM
(This post was last modified: 12-18-2022 09:31 PM by John Keith.)
Post: #18
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
(12-18-2022 08:30 PM)Allen Wrote: I just noticed you can save 10% (5 bytes) of the program size by using the NEG function rather than "0 SWAP -" As strange as it may seem, 0 SWAP - is faster than NEG for lists of numbers. I considered the speed gain to be worth the extra 5 bytes since the operation is inside the loop. Also, on the 49/50 you can replace ROT ROT with UNROT and save another 2.5 bytes. |
|||
07-14-2023, 09:44 PM
(This post was last modified: 07-15-2023 06:36 PM by Gilles.)
Post: #19
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
newRPL HP50g:
Code: « Nota : + and ADD are switched in NewRPL, wich is more logical but loose compatibility Size: 68 bytes 1024 items, Time : 0.022 sec (usb connected) 8192 items, Time : 0.161 sec PC newRPL : 65536 items, Time : 0 sec (!!) |
|||
07-14-2023, 11:47 PM
Post: #20
|
|||
|
|||
RE: Mini-Challenge: Rudin-Shapiro Sequence
Dang that's fast!
|
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)