props for using { catalogue and managing a &. application in your step function.


a design principle I was aiming for is a big strength of J.  What I call a 
"perfect function" defined as iterative form where output is of consistent 
shape/meaning as input, and where ^: can be used "separately" to get the 
iterations.  It means debug free explorative programing.  Use adverbs to 
integrate extra/domain functionality above the "perfect function"

Your "step" function is actually more pure/more perfect than my ltrunc because 
it just returns the raw result of the next iteration.

(1 + i.9) step < 3 7

13 17 23 37 43 47 53 67 73 83 97

to use your step function to get the "exploratory structure" I want/ed just 
with adverbs

 H =: 2 : 'u v'

 > {: (1 + i.9) step H {: ((<@) ,~ ])^:(2) < 3 7 

113 137 167 173 197 223 283 313 317 337 347 353 367 373 383 397 443 467 523 547 
613 617 643 647 653 673 683 743 773 797 823 853 883 937 947 953 967 983 997

The function and adverbs will still work with data that is truncated to the 
last iteration, perhaps if exploration results in memory constraints.  Compared 
to ^:a: or ^:(i.n), there are no shape/fill cleanups to obtain the count of 
each iteration result.

for completeness, a right truncated primes generation function.

original, just edit of my less than fully perfect ltrunc

rtrunc1 =: (] , ((1 3 7 9) (10 #. ,~)"0 1 (10&#.inv))"0(,@:)(selPrime@:) 
each@:{:)

Perfect separation of a core perfect function (optimized for  wrapped with 
adverbs matching my exploration objectives

 rtruncCore =: (+ 10 * ])(selPrime@:)"0 1((,@:) -. 0:)

1 3 7 9 rtruncCore 2 3 5 7

31 71 23 53 73 37 29 59 79

rtrunc =: rtruncCore &>/@; H {: ((<@) ,~ ])

> {: 1 3 7 9 rtrunc^:2 < 2 3 5 7

311 313 233 733 373 293 593 317 797 719 239 739 379 599

The &>/@; portion could be part of "core function" and makes it compatible with 
either boxed or unboxed y argument.  Worth noting that the adverb section is 
identical to both "structural applications of the core perfect function".






On Monday, November 21, 2022 at 09:47:27 a.m. EST, Raul Miller 
<rauldmil...@gmail.com> wrote: 





For this, I think I would have gone with:

  selPrime=: #~ 1&p:
  seed=: selPrime digits=: 1+i.9
  step=: selPrime@,@:(,&.(10x&#.inv)/&>)@{@;
  digits&step&.>^:a: ,<seed

Which, granted, is not particularly fast. (That last line takes about
15 seconds on my current laptop.)

Not sure if this helps, though...

-- 
Raul

On Sun, Nov 20, 2022 at 8:37 PM 'Pascal Jasmin' via Programming
<programm...@jsoftware.com> wrote:
>
> based on Raul's rosettacode link, but building an expaning list of left 
> truncable primes, such that further search is possible on future "iterations"
>
>
>  selPrime=: #~ 1&p:
>
> ltrunc =: (] , ((1+i.9) (10 #. ,)"0 1 (10&#.inv))"0(,@:)(selPrime@:) each@:{:)
>
> # &> sofar =: ltrunc^:6 < 3 7x
>
> 2 11 39 99 192 326 429
>
> # &> sofar =: ltrunc^:6 sofar
>
> 2 11 39 99 192 326 429 521 545 517 448 354 276
>
> the number of truncable primes decreases substantially as the digits 
> increase. (last answer is for 1 to 13 digits, with single digit primes 
> wrongly listed (4 is right number of single digit primes)
>
> the full list eventually goes to 0
>
> # &> sofar =: ltrunc^:1 sofar (NB. iterations skipped)
>
> 2 11 39 99 192 326 429 521 545 517 448 354 276 212 117 72 42 24 13 6 5 4 3 1 0
>
> which I guess we knew from example number
>
> > _2 { sofar
>
> 357686312646216567629137
>
>
> On Saturday, November 19, 2022 at 07:04:25 p.m. EST, Raul Miller 
> <rauldmil...@gmail.com> wrote:
>
>
>
>
>
> On Sat, Nov 19, 2022 at 6:47 PM 'Skip Cave' via Programming
> <programm...@jsoftware.com> wrote:
> > Now what is the J verb that will find an n-digit integer that is still
> > prime when each of the digits are removed?
>
> I'd probably go with https://rosettacode.org/wiki/Truncatable_primes#J for 
> that.
>
> Thanks,
>
> --
> Raul
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to