Yes, agreed - no points for performance for even quite small
values of y; I was only suggesting some small changes to
Tracy's code.
Incidentally, she says she prefers an earlier transpose to my
deferred rotation, mentioning the z y x ordering of the outer
products; however, the phrase (=/~ +/~) avoids the need
for either device, and
pytrplb =: 3 : '>: 4$. $. (=/~ </~* +/~) *: }.i.>:y'
is quite nice - still hopeless for anything of any size though.
I agree with her that the row-sort is unnecessary since the
sparse index is already ordered. It was necessary with the
($#:I.@,) idiom however.
IF only the shorter sides are required, this is quite handy,
saving one outer product:
3 : '>: 4$. $. (e.~</~* +/~) *: }.i.>:y'
Pasting on the hypotenuse
3 : '(,.+/&.:*:"1) >: 4$. $. (e.~</~* +/~) *: }.i.>:y'
doubles the time, though it's still competitive with P2
in time & space. They both run out of memory (on my
machine) for arguments over 3 or 4 thousand.
Mike
R.E. Boss wrote:
Different approach (haven't looked at the lab yet):
P2=: 3 : '%:(+/(,~#"1~(e.{:)) ])&.|:;<@({.,.}.)\.*:}.>:i.y'
ts'pytrpla 500'
0.69549456 1.352768e8
ts'P2 500'
0.028264955 6240320
rnk 5 ts&> 'P2 500'; 'pytrpla 500'
0 1 1
1 46 22
(pytrpla-:P2) 500
1
R.E. Boss
-----Oorspronkelijk bericht-----
Van: [EMAIL PROTECTED] [mailto:programming-
[EMAIL PROTECTED] Namens Mike Day
Verzonden: donderdag 17 januari 2008 14:13
Aan: Programming forum
CC: [EMAIL PROTECTED]
Onderwerp: Re: [Jprogramming] Pythagorean triples
This comes in a bit simpler:
pytrpla =: 3 : '/:~ |."1 >: 4$. $. (=/ >/~* +/~) *: }.i.>:y'
The >/~ phrase ensures you only get decreasing triples.
I was going to point out that I use something along the lines of
($#:I.@,) rather than 4$. $.
to locate ones in a boolean array, but on examination your
scant array idiom is much better in this case, and seems
generally good, so might be worth adding to the phrases list -
I couldn't see it there.
In any case, this exhaustive search approach eventually
blows up for y somewhere in 1000-2000, and as Raul
says, it's worth looking at the pythagorean triples lab.
There are several problems on these triples in "Project
Euler" which you might enjoy.
Mike
Tracy Harms wrote:
I have been looking at the Rosetta Code task entitled "List
Comprehension", here:
http://www.rosettacode.org/rosettacode/w/index.php?title=List_Comprehens
ion
<http://www.rosettacode.org/rosettacode/w/index.php?title=List_Comprehen
sion>
J does not have any special syntax, so it cannot (I think) satisfy this
syntax-specific requirement. I can, however, build the list of
Pythagorean triples. Here's my solution:
Across =: 1 : '*./@(}: u }.)"_1'
pytrpl =: 3 : '(#~ < Across) 2+ 4$. $. |: (=/ +/~) *: 2+i. 1-~ y'
pytrpl 22
3 4 5
5 12 13
6 8 10
8 15 17
9 12 15
12 16 20
I'd be interested in knowing a tacit way to define the adverb, Across.
I notice that the phrase (#~ < Across) has an ambiguity Bruno Daniel
criticized: whether this is a fork or a hook depends on whether Across
is verb or adverb. I used capitalization of initial letter to indicate
that it is an adverb, but some other naming convention might be better.
As for alternatives to pytrpl, what I'd find interesting are programs
that bring out the Pythagorean relationship. The heart of the program
listed here-- (=/ +/~) *: --seems rather buried.
The Haskell list comprehension syntax is attractive. It is notational,
and much like familiar set notation. If an analogous presentation can be
phrased in J, it's beyond my creative powers.
--
Tracy
----------------------------------------------------------------------
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