help needed with bases

69 views
Skip to first unread message

Richard Donovan

unread,
Sep 2, 2024, 10:58:01 AMSep 2
to fo...@jsoftware.com

Hi

I am doing some work on palindromic primes in any base and I am using

    5 #.^:_1&.> i.100000 

To convert in this case the first 100000 digits to base 5 which I later test to select palindromes.

It gives the right result but takes forever and uses heaps of space:-

ts'10 #.^:_1&.> i.100'
┌─────────┬──────┐
│0.0008306│476224│
└─────────┴──────┘
  ts'10 #.^:_1&.> i.10000'
┌────────┬────────┐
│0.111163│46990272│
└────────┴────────┘
  ts'10 #.^:_1&.> i.100000'
┌────────┬─────────┐
│0.965501│477693888│
└────────┴─────────┘
  ts'5 #.^:_1&.> i.100000'
┌────────┬─────────┐
│0.945648│469305216│
└────────┴─────────┘

Is there anything I can do to improve performance and reduce storage use?

Thanks in advance

Richard Donovan

Henry Rich

unread,
Sep 2, 2024, 11:15:07 AMSep 2
to fo...@jsoftware.com
   (6!:2 , 7!:2)'(#: i.@(*/)) 8$5'
0.0136049 3.35563e7

Not an exact match as it has high-order 0s, but faster.

Henry Rich
To unsubscribe from this group and stop receiving emails from it, send an email to forum+un...@jsoftware.com.

More Rice

unread,
Sep 2, 2024, 2:45:30 PMSep 2
to fo...@jsoftware.com
maybe get rid of the boxes and do palindrome filtering in string?

On my poor machine ...

   timespacex '5 #.^:_1&.> i.100000'  NB. Yours

0.694979 4.69305e8


Here, I tried to do the same + filtering; it still costs less:


   pf =: (#~ (-:|.)"1) NB. guessing this is what you want to do.

   timespacex 'pf ,"2 ":"(0) 5 #.inv i.100000'

0.11806 9.44435e6



Maurice


To unsubscribe from this group and stop receiving emails from it, send an email to forum+un...@jsoftware.com.

Raul Miller

unread,
Sep 2, 2024, 4:28:09 PMSep 2
to fo...@jsoftware.com
It might be that the suggestions you've received are good enough for
your purposes.

That said, I think the purpose of the boxing here is to ignore leading zeros.

So, it might make sense to generate the sequence of numbers
corresponding to some number of digits in the given base.

NB. x base, y digit count
subseq=: {{ x #.inv (x^y-1)+i.x^y-1 }}

Or, better yet, introduce palindrome filtering into the generating function:

palseq=: {{ x #. (#~ ] -:"1 |."1) x #.inv (x^y-1)+i.x^y-1 }}

This generates some extra numbers, but hopefully discarding them
wouldn't be too problematic.

#0,; 5 palseq&.> 1+i.>.5^.1e5
313
#(#~ 1e5>]) 0,; 5 palseq&.> 1+i.>.5^.1e5
223

FYI,

--
Raul

On Mon, Sep 2, 2024 at 10:58 AM Richard Donovan <rsdo...@hotmail.com> wrote:
>
>

Mike Day

unread,
Sep 3, 2024, 1:10:55 PMSep 3
to fo...@jsoftware.com
This constructive approach might be of some use for
producing Richard's pally primes:

ps =: {{
b =. x             NB. base
d =. y             NB. width
hd=. >.-: d                  NB. size of rhs (incl middle if d odd)
i =. b #.inv i. b ^ hd       NB. generate too many rhs,  ie incl 0 in units column
i =. b (]#~(* @ | {:"1)) i   NB. prune out 0s in units col 
10 #. i,. ~ (d-hd){."1|."1 i NB. pin on the lhs, expressed in base 10
}} 
 
eg 
   3 6$3 ps 5
10001 20002 11011 21012 12021 22022
10101 20102 11111 21112 12121 22122
10201 20202 11211 21212 12221 22222

I've returned them as base 10 integers,  but could have kept the result as 
a table of digits,  or indeed a character array.  

Boxing isn't needed, as it ensures that the leading digit is non-zero.

Its performance seems adequate:
   #7 ps 10
14406
   timespacex'7 ps 10'
0.006473 4.19664e6
   
... depending of course on how large the required primes will be!

Mike

Sent from my iPad

On 2 Sep 2024, at 21:28, Raul Miller <rauld...@gmail.com> wrote:

It might be that the suggestions you've received are good enough for

Michael Day

unread,
Sep 3, 2024, 2:03:34 PMSep 3
to 'Mike Day' via forum
Actually it's not much more complicated when avoiding any pruning of
the rhs of the candidate palindromes:

cf my earlier email - below this one -

psa =: {{

b =. x             NB. base
d =. y             NB. width
hd=. >.-: d                                   NB. size of rhs (incl middle if d odd)
bb=. (1,~<: hd) # b - 0 1                     NB. using b-1 in units column
i =. (1 {.~ - hd) +"1 bb #: i. */bb           NB. generate rhs, incrementing units column to avoid zeros
10 #. i,. ~ (d-hd){."1|."1 i NB. pin on the lhs; result is expressed in base 10
}}

    timespacex'5 ps 8'    NB. on my laptop this time

0.000277 67296


    timespacex'5 psa 8'  NB. space much the same!

0.0001156 67424



      (6!:2 , 7!:2)'(#: i.@(*/)) 8$5'    NB. Henry's scoping snapshot on my laptop
0.0186664 3.35563e7

Virus-free.www.avast.com

More Rice

unread,
Sep 4, 2024, 1:29:01 AMSep 4
to fo...@jsoftware.com

(The theme here continued to be boxless.)


more seriously,


   bn =: <.@(1: + (^. >./))

   bdl =: ] #:~ bn # [


   NB. the table of 0-padded digits in base 5; similar to Henry's.

   5 bdl i.100000


   NB. leading zero aware palindrome check and tell you which one. Did I get it right?

   dr =: 1 i."1~ 0&<

   bpldm =: 4 : '(#~ ([: ((-:|.)"1 (|."(0 1)~ dr)) x&bdl)) y'


   NB. looks ok, no?
   4 bpldm 25 0 100 5 514 46 706
25 0 5 514 46

   NB. the finale - number of primes < 100000 that are also palindrome in base 4.
   # 4 bpldm p:i.9591
65

trx2358...@yahoo.com

unread,
Sep 4, 2024, 1:40:27 AMSep 4
to fo...@jsoftware.com
Following Raul’s suggestion to use anti-base to a specified number of digits you could simply remove leading 0’s and let the fill shift them to the end which will line them up when reversed for testing.

   strp=.[ #~ >./\&:*
   ([;|.-:/@,:strp)"1(3#5) #:i.20
┌─────┬─┐
│0 0 0│1│
├─────┼─┤
│0 0 1│1│
├─────┼─┤
│0 0 2│1│
├─────┼─┤
│0 0 3│1│
├─────┼─┤
│0 0 4│1│
├─────┼─┤
│0 1 0│0│
├─────┼─┤
│0 1 1│1│
├─────┼─┤
│0 1 2│0│
├─────┼─┤
│0 1 3│0│
├─────┼─┤
│0 1 4│0│
├─────┼─┤
│0 2 0│0│
├─────┼─┤
│0 2 1│0│
├─────┼─┤
│0 2 2│1│
├─────┼─┤
│0 2 3│0│
├─────┼─┤
│0 2 4│0│
├─────┼─┤
│0 3 0│0│
├─────┼─┤
│0 3 1│0│
├─────┼─┤
│0 3 2│0│
├─────┼─┤
│0 3 3│1│
├─────┼─┤
│0 3 4│0│
└─────┴─┘

P.S. I only subscribe to the digest.  Might it be useful to insert Reply-to links for the individual threads?


On Wednesday, September 4, 2024, 11:39 AM, fo...@jsoftware.com wrote:

Mike Day <mike_l...@tiscali.co.uk>: Sep 03 06:10PM +0100
Michael Day <mike_l...@tiscali.co.uk>: Sep 03 07:06PM +0100

Actually it's not much more complicated when avoiding any pruning of
the rhs of the candidate palindromes:
 
cf my earlier email - below this one -
 
psa =: {{
b =. x             NB. base
d =. y             NB. width
hd=. >.-: d                                   NB. size of rhs (incl
middle if d odd)
bb=. (1,~<: hd) # b - 0 1                     NB. using b-1 in units column
i =. (1 {.~ - hd) +"1 bb #: i. */bb           NB. generate rhs,
incrementing units column to avoid zeros
10 #. i,. ~ (d-hd){."1|."1 i NB. pin on the lhs; result is expressed in
base 10
}}
 
    timespacex'5 ps 8'    NB. on my laptop this time
 
0.000277 67296
 
 
timespacex'5 psa 8'  NB. space much the same!
 
0.0001156 67424
 
 
 
      (6!:2 , 7!:2)'(#: i.@(*/)) 8$5'    NB. Henry's scoping snapshot
on my laptop
0.0186664 3.35563e7
 
 
 
On 03/09/2024 18:10, 'Mike Day' via forum wrote:
 
--
This email has been checked for viruses by Avast antivirus software.
www.avast.com
bill lam <bbil...@gmail.com>: Sep 04 12:21AM +0800

That should be a bug in J9.6 beta 3 and later.
It should be already fixed in beta 17.
 
 
On Sat, Aug 10, 2024, 11:28 PM Jan-Pieter Jacobs <janpiete...@gmail.com>
wrote:
 
Jan-Pieter Jacobs <janpiete...@gmail.com>: Sep 03 07:37PM +0200

Thank you Bill,
I tried and confirm installing from Github now works.
 
Jan-Pieter
 
Marcin Żołek <marcin...@students.mimuw.edu.pl>: Sep 03 05:44PM +0200

Thanks! I find your approach (get the indices of elements for each diagonal and then work with flattened array) very useful and universal. I came up with a less universal approach: find pairs of local indices for each diagonal and then map them to the correct indices. Your solution seems more universal to me, because finding pairs is independent of the fact that the task involved diagonals in your case, while in my case it is dependent.
 
a =: 4 4 $ 1 0 1 1 2 2 1 1 1 2 0 1 2 0 2 2
((I.@:(=&1) <@:(,/)@:(,."0 1) I.@:(=&2))/. ;@:({&.>) </.@:i.@:$)"2 a
2 5
8 5
3 9
3 12
6 9
6 12
11 14
 
Marcin
 
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to forum+un...@jsoftware.com.
Reply all
Reply to author
Forward
0 new messages