Yuvaraj Athur Raghuvir  wrote:
> find the nearest prime or prime power to a number.
...
> Can fpp be tacitly defined?

Personally, I do not understand what fpp is doing, so I
am not prepared to discuss how it might be implemented
tacitly, other than to mention that f^:t^:_ is somewhat
analogous to a while loop, where t is the "while test" and
f is a side-effect free function which represents the
while body.  (The argument should be a complete
representation of the state manipulated by the while
loop.)
 
That said, here's how I might implement "find the nearest
prime or prime power to a number", tacitly:
 
   npp=:(((i. <./)@:|@:- { ]) ,@(i.@>:&.(p:^:_1 <mailto:i.@>:&.(p:^:_1> ) ^/ 
[EMAIL PROTECTED]@#:))"0 <mailto:[EMAIL PROTECTED]@#:))"0> 
   npp i. 5 20
 1  1  2  3  4  5  5  7  8  9  9 11 11 13 13 13 16 17 17 19
19 19 23 23 25 25 27 27 27 29 29 31 32 32 32 37 37 37 37 37
41 41 41 43 43 43 47 47 49 49 49 49 53 53 53 53 53 59 59 59
59 61 61 61 64 64 67 67 67 67 71 71 71 73 73 73 73 79 79 79
81 81 81 83 83 83 83 89 89 89 89 89 89 89 97 97 97 97 97 97
 
Since that's a bit long, here's a breakout of some phrases:
  (i.@>:&.(p:^:_1 <mailto:i.@>:&.(p:^:_1> ) generates a list of relevant primes
  [EMAIL PROTECTED]@#:          generates a list of relevant powers
  ,@(x ^/ y)       generates a list from the power table
  (f g)y           is y f (g y) [see documentation on hook]
  x(i.<./)@:|@:- y finds the index of the y closest to x
  x(f{])y          expects x f y to be an index into y
 
Note, however, that for large numbers I might gain some
efficiencies by trimming the size of my intermediate
results.
 
Also note that I'm not doing stuff here that you are
doing in fpp.  There's a good chance that you are going
somewhere that my above example is not a good illustration
for.
 
-- 
Raul
 
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to