Jump to content

Pythagorean triples: Difference between revisions

(J: the easy implementation)
Line 112:
 
pytr 10000 takes 4 seconds on this laptop, and time to complete grows with square of perimeter, so pytr 1e6 should take a something like 11 hours using this algorithm on this machine.
 
A slightly smarter approach:
 
<lang j>trips=:3 :0
'm n'=. |:(#~ 1 = 2 | +/"1)(#~ >/"1),/,"0/~}.i.<.%:y
prim=. (#~ 1 = 2 +./@{. |:) (#~ y>:+/"1)m (-&*:,. +:@*,. +&*:) n
(,.~ e.&prim) ~./:~;<@(*/~ 1 + y i.@<.@% +/)"1 prim
)</lang>
 
usage for trips is the same as for pytr. Thus:
 
<lang j> (# , 1 {. +/) trips 10
0 0
(# , 1 {. +/) trips 100
17 7
(# , 1 {. +/) trips 1000
325 70
(# , 1 {. +/) trips 10000
4858 703
(# , 1 {. +/) trips 100000
64741 7026
(# , 1 {. +/) trips 1000000
808950 70229
(# , 1 {. +/) trips 10000000
9706567 702309</lang>
 
The last line took about 16 seconds.
 
=={{header|Java}}==
6,962

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.