Generator/Exponential: Difference between revisions

m
→‎{{header|J}}: add whitespace
m (→‎{{header|J}}: remove leftover line)
m (→‎{{header|J}}: add whitespace)
Line 41:
 
<lang j>coclass 'mthPower'
N=: 0
create=: 3 :0
M=: y
)
next=: 3 :0
n=. N
N=: N+1
n^M
)</lang>
Line 53:
And, here are corresponding square and cube generators
 
<lang j>stateySquare=: 2 conew 'mthPower'
stateyCube=: 3 conew 'mthPower'</lang>
 
Here is a generator for squares which are not cubes:
 
<lang j>coclass 'uncubicalSquares'
N=: 0
next=: 3 :0"0
while. (-: <.) 3 %: *: n=. N do. N=: N+1 end. N=: N+1
*: n
)</lang>
 
And here is an example of its use:
 
<lang j> next__g i.10 [ next__g i.20 [ g=: conew 'uncubicalSquares'
529 576 625 676 784 841 900 961 1024 1089</lang>
 
That said, here is a more natural approach, for J.
 
<lang j>mthPower=: 1 :'^&m@i.'
squares=: 2 mthPower
cubes=: 3 mthPower
uncubicalSquares=: squares -. cubes</lang>
 
The downside of this approach is that it is computing independent sequences. And for the "uncubicalSquares" verb, it is removing some elements from that sequence. So you must estimate how many values to generate. However, this can be made transparent to the user with a simplistic estimator:
 
<lang j>uncubicalSquares =: {. squares@<.@p.~&3 1.1 -. cubes</lang>
 
Example use:
892

edits