IMO, being new to J, you should avoid explicit loops (for and while) as much
as possible.
And, even more important, think array-wise.


Here is my second attempt to determine the next odd prime power (including
first power) greater or equal than (the elements of) the input y.

The algorithm is rather simple:
A. determine the odd prime powers between <./y and _4 p: y 
B. compare y with these prime powers

   [Y=:20+20?.100               NB. testinput
66 119 43 82 62 64 32 25 88 83 96 104 73 103 89 117 94 102 30 99

A1.
        onr=: <./ ((2: | >:@[) + [ + +:@i.@<[EMAIL PROTECTED]:@-~) 4: p: >./
                NB. odd numbers between <./ and 4: p: >./
   3 17 $ onr Y
25 27 29 31  33  35  37  39  41  43  45  47  49  51  53  55  57
59 61 63 65  67  69  71  73  75  77  79  81  83  85  87  89  91
93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125

A2.
        pwpr=:  #~ 1: = [EMAIL PROTECTED]@q:            NB. powers of primes
   pwpr onr Y
25 27 29 31 37 41 43 47 49 53 59 61 67 71 73 79 81 83 89 97 101 103 107 109
113 121 125

B.
        nle=: [ ((i.~ {.) { 1: { ]) ] (] ,: |[EMAIL PROTECTED] {~ 
<:@(+/\)&.|[EMAIL PROTECTED])
/:[EMAIL PROTECTED]@,
                NB. next_larger_or_equal of x in y
   (] nle [EMAIL PROTECTED]) Y
67 121 43 83 67 67 37 25 89 83 97 107 73 103 89 121 97 103 31 101

or, to check the result

   (] ([,:nle) [EMAIL PROTECTED]) Y
66 119 43 82 62 64 32 25 88 83 96 104 73 103 89 117 94 102 30  99
67 121 43 83 67 67 37 25 89 83 97 107 73 103 89 121 97 103 31 101

NB. code is not optimized (yet).

R.E. Boss



-----Oorspronkelijk bericht-----
Van: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Namens Yuvaraj Athur Raghuvir
Verzonden: donderdag 21 september 2006 6:35
Aan: Programming forum
Onderwerp: Re: [Jprogramming] Explicit to Tacit - newbie question

Hello,

Putting together the previous mails, I have written my first J program to
find the nearest prime or prime power to a number.
Here I am excluding powers of 2.

Hurrah!!
I could move quite far until I had to loop that incremented and applied the
logic.

Program follows.

Can fpp be tacitly defined?

Regards,
Yuva

J Program:
NB. find the nearest prime or prime power for a number
NB. odd means remainder is 1
o =: 2&|
NB. makeodd: - use tie + agenda (for implementing if condition!)
mo =: (>: ` ]) @. o
NB. whole: compare with Floor of number
w =: ]=<.
NB. pl: list of primes less that given number
pl =: i. &. (p:^:_1) NB. use of dual operator &. (Henry Rich)
NB. however, I need primes <= number
ple=: pl@>: NB. add one more and get the list! (Roger Hui)
NB. check if the we have found a suitable base prime
ckl =. w @: (ple ^. ]) NB. ckl: Check the log wrt primes list
d =:  +/ @: ckl NB.  done: stopping condition
NB. repeat the discovery by increasing the value
fpp =: 3 : 0
k =. mo y
while. (0 = d k) do.
k =. 2&+k
end.
j =. I. ckl k
t =. p:j
y,k, j , t , t ^. k
)



On 9/21/06, Roger Hui <[EMAIL PROTECTED]> wrote:
>
>   ple=: i.&.(p:^:_1)@>:
>   ple 30
> 2 3 5 7 11 13 17 19 23 29
>   ple 31
> 2 3 5 7 11 13 17 19 23 29 31
>   ple 32
> 2 3 5 7 11 13 17 19 23 29 31
>
> Or, alternatively,
>
>   ple=: pl@>:
>
> where pl is what you have already defined.
>
>
>
> ----- Original Message -----
> From: Yuvaraj Athur Raghuvir <[EMAIL PROTECTED]>
> Date: Wednesday, September 20, 2006 8:30 pm
> Subject: Re: [Jprogramming] Explicit to Tacit - newbie question
>
> > Thanks!
> >
> > Interesting  use of the dual/under operator &. Although I had
> > studied the
> > form, this usage didnot occur to me!
> >
> > As an extension question:
> >
> > p1 =: i. &. (p:^:_1)     NB. primes strictly less than number
> >
> > so,
> > p1 37
> > 2 3 5 7 11 13 17 19 23 29 31
> >
> > In the generated list, I want the number included if it is prime.
> > What would
> > that extension be using &. ?
> > Or, does that need compelete verb definition?
>
>
> ----------------------------------------------------------------------
> 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