Pythagorean Triple Sequence
Jelly, 19 bytes
o3ṄÆF*/€ŒPP€²+Ṛ$HṂß
Saved a byte thanks to @Dennis by refactoring to an infinite sequence.
Takes no input and arguments, then outputs the sequence infinitely by printing each term as it computes them. This method slows down as the numbers get larger since it depends on prime factorization.
Try it online!
This calculates the next term by computing the prime power factorization of the current term. For 12325, this is {52, 17, 29}. There is a variant of Euclid's formula for calculating Pythagorean triples {a, b, c},
where m > n and the triple is primitive iff m and n are coprime.
To calculate the next primitive root from 12325, find m and n such that mn = 12325 and choose m,n so that gcd(m, n) = 1. Then generate all pairs of m,n by creating all subsets of {52, 17, 29} and finding the product of each of those subsets which are {1, 25, 17, 29, 425, 725, 493, 12325}. Then divide 12325 by each value and pair so that each pair is m,n. Compute the formula for c using each pair and take the minimum which is 90733.
- The previous method failed for determining the next term after 228034970321525477033478437478475683098735674620405573717049066152557390539189785244849203205. The previous method chose the last value as a factor when the correct choice was the 3rd and last primes. The new method is slower but will always work since it tests all pairs of coprimes to find the minimal hypotenuse.
Explanation
o3ṄÆF*/€ŒPP€²+Ṛ$HṂß Main link. Input: 0 if none, else an integer P
o3 Logical OR with 3, returns P if non-zero else 3
Ṅ Println and pass the value
ÆF Factor into [prime, exponent] pairs
*/€ Reduce each pair using exponentation to get the prime powers
ŒP Powerset of those
P€ Product of each
² Square each
$ Monadic chain
+ Add vectorized with
Ṛ the reverse
H Halve
Ṃ Minimum
ß Call recursively on this value
Brachylog, 36 bytes
3{@wB:?>:^a+~^=C:B:?:{$pd}ac#d,C:1&}
Try it online!
You have to wait for the program to time out (1 minute) before TIO flushes the output. In SWI-Prolog's REPL this prints as soon as it finds the value.
This will print the sequence forever.
After a few minutes on the offline SWI-Prolog's interpreter, I obtained 90733
after 12325
. I stopped it after this point.
This isn't complete bruteforce as it uses constraints to find pythagorean triples, though it is obviously not optimised for speed.
Explanation
3{ } Call this predicate with 3 as Input
@w Write the Input followed by a line break
B:?> B > Input
+ The sum...
:^a ...of Input^2 with B^2...
~^ ...must equal a number which is itself a square
=C Assign a fitting value to that number and call it C
C:B:?:{$pd}a Get the lists of prime factors of C, B and Input
without duplicates
c#d, Concatenate into a single list; all values must be
different
C:1& Call recursively with C as Input
Perl, 73 bytes
for($_=3;$_<1e9;$_=$a**2+$b**2){$a++until($b=($_+$a**2)**.5)==($b|0);say}
All Pythagorean Triples a²+b²=c²
satisfy a=r(m²-n²), b=2rmn, c=r(m²+n²)
for some integers r,m,n
. When r=1
and m,n
are coprime with exactly one being divisible by 2, then a,b,c
is a primitive triple, where a,b,c
are all pairwise coprime.
With this in mind, given some a
, I use a brute-force algorithm to calculate the smallest n
such that a²-n²
is a square, namely m²
. Then, c
is equal to n²+m²
.