Pythagorean triples: Difference between revisions

Line 265:
 
The last line took about 16 seconds.
 
That said, we do not actually have to generate all the triples, we just need to count them. Thus:
 
<lang j>trc=:3 :0
'm n'=. |:(#~ 1 = 2 | +/"1)(#~ >/"1) ,/ ,"0/~ }. i. <. %: y
<.y%+/"1 (#~ 1 = 2 +./@{. |:) (#~ y >: +/"1)m (-&*: ,. +:@* ,. +&*:) n
)</lang>
 
The result is a list of positive integers, one number for each primitive triple which fits within the limit, giving the number of triples which are multiples of that primitive triple whose perimeter is no greater than the limiting perimeter.
 
<lang> (#,+/)trc 1e8
7023027 113236940</lang>
 
But note that J's memory footprint reached 6.7GB during the computation, so to compute larger values the computation would have to be broken up into reasonable sized blocks.
 
=={{header|Java}}==
6,962

edits