Yuvaraj Athur Raghuvir wrote:
> 1) Given : Number n
> 2) Consider only ODD numbers greater than n - say
> OddsGreaterThanOrEqual(n)
> 3) In this set, find the smallest number m such that:
> 3.1) either m is a prime number
> 3.2) or, m is a power of a prime number.
> (Full program attached after signature).
I must confess that I am still puzzled.
If I follow the above instructions literally, the result
would always be 3. There is always an odd number greater
than n which is a power of 3.
Alternatively, if I inspect your result:
;/ fpp"0 i.10 10
_______________________________
│3│11│23│31│41│53│61│71│ 3│ 97│
│3│11│23│31│41│53│61│71│ 3│ 97│
│3│13│23│37│43│53│67│73│83│ 97│
│3│13│23│37│43│53│67│73│83│ 97│
│5│17│ 5│37│47│59│67│79│89│ 97│
│5│17│ 5│37│47│59│67│79│89│ 97│
│7│17│ 3│37│47│59│67│79│89│ 97│
│7│17│ 3│37│47│59│67│79│89│ 97│
│3│19│29│41│ 7│59│71│79│89│101│
│3│19│29│41│ 7│59│71│79│89│101│
________________________________
I can construct a similar (but not identical)
result using:
clos=: ~.@((= <./)@:|@:- # ]) ,
near=: 3 >. _4 4&(p:"0)^:(0&p:)("0)@(2&>.)
pows=: >:@[EMAIL PROTECTED]@>.@(3&^.)
mpr=: <./@:([EMAIL PROTECTED]:)
mopr1=: ([EMAIL PROTECTED] near,([EMAIL PROTECTED] [EMAIL PROTECTED]) ^&x:
pows)"0@>:
mopr1 finds the prime or prime root of the prime power
which is nearest 1 + the argument.
Here's a display of the first 100 results from mopr1:
;/,"0 mopr1 i. 10 10
+-+--+--+--+--+--+--+--+--+---+
|3|11|19|31|41| 7|61|71| 3| 89|
|3|11|23|31|41|53|61|71| 3| 89|
|3|13|23|31|43|53|61|73|83| 89|
|3|13| 5|31|43|53|61|73|83| 97|
|5|13| 5|37|43|53|67|73|83| 97|
|5|17| 3|37|47|53|67|73|83| 97|
|7|17| 3|37|47|59|67|79|89| 97|
|3|17| 3|37| 7|59|67|79|89| 97|
|3|19|29|37| 7|59|67|79|89| 97|
|3|19|29|41| 7|59|71| 3|89|101|
+-+--+--+--+--+--+--+--+--+---+
As you can see, I am not quite getting the same
result as you.
Since each of these functions has changed from my
original proposal, here's a description of each:
clos returns a list of the unique atoms in y
which are closest to x. This will be a list of
one or two numbers in this context.
near returns the odd primes which bracket y.
More specifically, the largest odd prime which
does not exceed y (if it exists) and the
smallest odd prime which is not smaller than
y. (It first finds the primes which bracket y,
and then replaces any values smaller than 3 with 3.)
pows returns somewhat plausible powers, relevant
to this problem.
mpr returns the minimum prime root from its
argument
mopr1 returns the minimum prime or prime root
for the number nearest 1 + y
As for reading the code, let's take apart clos
to illustrate how you might do this:
First, an example of how it can be used:
15 clos p:i.3 3
13 17
And, its definition:
clos
~.@((= <./)@:|@:- # ]) ,
The first thing I like to do when inspecting
a tacit function is set up an example that
I can edit:
15 (~.@((= <./)@:|@:- # ]) ,) p:i.3 3
13 17
Also, when I want to see the arguments to
a dyad, I can replace that dyad with one
which shows its arguments literally:
15 (; ,) p:i.3 3
+--+----------------------+
|15|2 3 5 7 11 13 17 19 23|
+--+----------------------+
Here, I can ignore the outer parenthesis:
15 ~.@((= <./)@:|@:- # ]) ,p:i.3 3
13 17
I can also extract interesting intermediate
verbs and see how they work:
15 |@:- ,p:i.3 3
13 12 10 8 4 2 2 4 8
15 (= <./)@:|@:- ,p:i.3 3
0 0 0 0 0 1 1 0 0
In other words, what clos does is:
ravel its right argument
find the absolute value of the difference
between its left arg and this ravel
select values from the ravel which correspond
to the minimum absolute value
find the unique set of values from this selection
To illustrate this latter point:
3 clos 1 2 4 6 4 2 1
2 4
It might also be informative to inspect what mopr1
passes to clos:
(; (near , ([EMAIL PROTECTED] [EMAIL PROTECTED]) ^&x: pows))"0@>: 9
+--+-----+
|10| 7 11|
| | 9 25|
| |27 27|
+--+-----+
Here, pows >: 9 produces 2 3
Also, clos will return 11 9 in this case:
(clos (near , ([EMAIL PROTECTED] [EMAIL PROTECTED]) ^&x: pows))"0@>: 9
11 9
This message is getting a bit long, so I'll stop
here, but if anything is not obvious to you, just
ask.
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm