| Bytes | Lang | Time | Link |
|---|---|---|---|
| 149 | SAKO | 250326T173924Z | Acrimori |
| 016 | Husk | 240914T172812Z | Glory2Uk |
| 012 | Nekomata | 230302T121000Z | alephalp |
| 078 | jq | 230228T042136Z | GammaFun |
| 1137 | C gcc | 230301T195034Z | evanstar |
| 063 | JavaScript V8 | 230227T134243Z | Arnauld |
| 013 | Brachylog | 230301T134310Z | Fatalize |
| 098 | Python | 230301T013153Z | robertla |
| 056 | Haskell | 230228T213927Z | Benji |
| 081 | JavaScript | 230227T135605Z | EzioMerc |
| 072 | JavaScript | 230228T140901Z | Falco |
| 104 | Python 3 | 230227T125943Z | Rhaixer |
| 125 | Befunge93 PyFunge | 230228T015749Z | yeah ok |
| 062 | Noether | 230228T011257Z | Beta Dec |
| 008 | Vyxal | 230227T115448Z | lyxal |
| 014 | Pyth | 230227T191259Z | CursorCo |
| 071 | Scala | 230227T191112Z | waf9000 |
| 088 | Python | 230227T155122Z | The Thon |
| 021 | MathGolf | 230227T163157Z | Kevin Cr |
| 050 | Arturo | 230227T153336Z | chunes |
| 057 | Factor + math.combinatorics | 230227T145738Z | chunes |
| nan | 230227T144040Z | The Thon | |
| 111 | ><> Fish | 230227T131419Z | mousetai |
| 009 | 05AB1E | 230227T132103Z | Kevin Cr |
| 026 | Charcoal | 230227T123856Z | Neil |
| 014 | Japt | 230227T114504Z | Shaggy |
SAKO, 149 bytes
PODPROGRAM:F(N)
**)C=A*2+B*2
GDYC>N*2:2,INACZEJ1
1)C=PWK(C)
GDYC=ENT(C):3,INACZEJ4
3)DRUKUJ(9,0):A,B,C
LINIA
4)POWTORZ:A=3(1)B
2)POWTORZ:B=4(1)N
WROC
Full programme version, 150 bytes
CZYTAJ:N
**)C=A*2+B*2
GDYC>N*2:2,INACZEJ1
1)C=PWK(C)
GDYC=ENT(C):3,INACZEJ4
3)DRUKUJ(9,0):A,B,C
LINIA
4)POWTORZ:A=3(1)B
2)POWTORZ:B=4(1)N
STOP2
KONIEC
Husk, 16 bytes
umOf(§=o½Σ▲†□)π3
This takes an integer as an input and outputs all Pythagorean triples of values not greater than the input number.
In my solution, I have tried to avoid brackets (2 bytes), or, at least, to replace them with a combinator, but, sadly, I couldn't. Without any braces the code will generate the squared triples. That means that the 16 bytes code below is valid too:
†√umOf§=o½Σ▲†□π3.
I prefer however the version with the brackets because it would look kinda wrong first to square and then to take the square root of the result.
Explanation:
π3 # 3 x Cartesian product from 1 to the input value
f( ) # Filter those triads that fullfill the condition inside the brackets:
†□ # After each number got squared,
▲ # the triad's maximum
o½Σ # and the half sum
§= # are equal.
mO # Sort each triad
u # Remove duplicates
Nekomata, 12 bytes
RS3Lᵖ{:*Ɔ$∑=
A port of @Fatalize's Brachylog answer.
RS3Lᵖ{:*Ɔ$∑=
R Range from 1 to the input
S Subset
3L Length should be 3
ᵖ{ Start a block; treat the block as a predicate,
and return the original value if the block does not fail
:* Multiply by itself
Ɔ$∑= The last element should equals to the sum of the other elements
By default, the Nekomata interpreter will print all possible results.
jq, 85 78 bytes
[range(3;.)]|combinations(3)|select(map(pow(.;2))|.[0]==.[1]+.[2]and.[1]>.[2])
Generates all triples, then filters.
C (gcc), 113 + 7 bytes
Compiled with -lm -DM=<input>
r,s,t;main(){for(;s<M;)for(t=++s;(r=sqrt(2*s*++t))<M;)r-sqrt(2*s*t)||(r+=s+t)>M||printf("%d %d %d\n",r-t,r-s,r);}
JavaScript (V8), 63 bytes
n=>{for(;p=n;n--)while(--p>(q=(n*n-p*p)**.5))q%1||print(q,p,n)}
Commented
n => { // n = input
for( // outer loop:
; //
p = n; // before each iteration, initialize p to n
// stop when n = 0
n-- // decrement n after each iteration
) //
while( // inner loop:
--p > ( // decrement p and test whether it's greater
q = // than q defined as:
(n * n - p * p) // the square root of the difference
** .5 // between n² and p²
) // stop as soon as the test fails
) //
q % 1 || // if q is an integer:
print(q, p, n) // print the triplet (q, p, n)
} //
Brachylog, 13 bytes
⟦₁⊇Ṫ.^₂ᵐṅᵗ+0∧
Explanation
This is a generator, which will unify with each triplet.
⟦₁ Range [1, …, N]
⊇Ṫ. Subset of 3 elements
.^₂ᵐ Square each element
ṅᵗ Negate the last one (which is the biggest)
+0∧ The sum must be 0
Much faster, 19 bytes
≥~hṪ≥₁ℕ₁ᵐ^₂ᵐṅʰ+0∧Ṫ≜
Takes about 1.5s for N = 200 on TIO. This uses integer constraint programming mechanisms which is way more efficient than brute forcing combinations in a range, but is longer to express in this case.
≥~hṪ A triplet of elements whose head is smaller than N
≥₁ The triplet is non-increasing
ℕ₁ᵐ Each element is in [1, +inf)
^₂ᵐ Map square
ṅʰ Negate the first element
+0∧ The sum must be 0
Ṫ≜ Assign values to satisfy these constraints
Python, 98 bytes
Two solutions already exist for Python, but this one uses a slightly different approach that I thought might be informative:
from itertools import *
lambda n:[(k,j,i)for(k,j,i)in combinations(range(0,n+1),3)if i*i==j*j+k*k]
JavaScript, 81 bytes
n=>{for(a=0;++a<n;)for(b=a;b<n;++b)(c=(a*a+b*b)**.5)%1||c<=n&&console.log(a,b,c)}
Try it:
f=n=>{for(a=0;++a<n;)for(b=a;b<n;++b)(c=(a*a+b*b)**.5)%1||c<=n&&console.log(a,b,c)}
;[
1,
3,
5,
7,
10,
17,
20
].forEach(n=>{console.log(n+':');f(n)})
JavaScript 72 bytes
c=>{for(a=b=c;c;--a||(a=--b)||(a=b=--c))c*c-b*b-a*a||console.log(c,b,a)}
Formatted:
c => {
for( a=b=c; c; /* until c is 0 */
/* count down a, when a==0 reduce b and reset a=b, the same with c */
--a || (a = --b) || (a = b = --c)
)
/* if c²-b²-a² == 0 log the tuple */
c*c - b*b - a*a || console.log(c,b,a)
}
Python 3, 112 104 bytes
lambda n,r=range:{(*sorted((i,j,k)),)for i in r(1,n+1)for j in r(1,n+1)for k in r(1,n+1)if i*i+j*j==k*k}
Literally the same thing as the other answers.
Befunge-93 (PyFunge), 125 bytes
v
&
>35g:*25g:*+15g:*-|
,*52.g51.g52.g53<v
^p53+1_v#`\g52:g53<<
^111 >$135p25g:1 5g\`!#v_1+25p
^ p51+1_@#`g51\g51 :p521$<
Note: The three "1"s shown here on line 6 ((1,5),(2,5) and (3,5) in Funge-Space) are actually be U+0001 in the source code. This is reflected correctly in TIO.
I may be able to save some bytes by moving those storage characters to line 1 or 2, and send line 6 left and wrap it, and if I can find a way to reduce the number of "g"s required to get the incremented values.
I'll do a nice writeup for this when I have time.
Noether, 62 bytes
!aI~n(!a~bn1+({a2^!b2^+0.5^~cn<cn=|cc_=&}{aP","PbP","PcP?}b)a)
Surprisingly this is the first time I've realised the severe lack of \$\leq\$ and \$\geq\$ operators in the language.
Explanation:
!a # Initialiase variable a to 1
I~n # Store user input in n: outer loop exit condition
( ) # Outer loop
!a~b # Increment a and store value in b
n1+ # Add 1 to n: inner loop exit condition
( ) # Inner loop
{ }{ } # If ... Else
a2^ # Square a
!b2^ # Increment value of b and square
+0.5^ # Sum a and b and square root
~cn< # Store value in c and check if less than n
cn= # c == n
| # Bitwise OR
cc_= # c == floor(c)
& # Bitwise AND
aP","PbP","PcP? # If true, output variable values, comma separated
b # Push b to stack (inner loop exit condition)
a # Push a to stack (outer loop exit condition)
Vyxal, 9 8 bytes
ɾ3ḋ'²÷ε=
I'm surprised I didn't manage to make this horribly inefficient.
-1 thanks to Kevin!
Explained
ɾ3ḋ'²÷ε=
ɾ # Range [1, input]
3ḋ # Combinations of length 3
' # filtered by:
² # squaring everything
÷ # dumping the triplet onto the stack in reverse order
ε # absolute difference of b**2 and c**2
= # equals a**2
Pyth, 14 bytes
f!-F^R2_T.CSQ3
Explanation
# implicitly assign Q = eval(input())
.C 3 # all sorted lists of three elements from
SQ # range(1,Q+1)
f # filter these lists on lambda T
_T # reverse T
^R2 # map elements to their squares
-F # fold on subtraction (subtract all elements from the first)
! # not (only true for zero)
Scala, 71 bytes
(n:Int)=>for{c<-5 to n;b<-4 to c;a<-3 to b if a*a+b*b==c*c}yield(a,b,c)
or styled differently:
(n:Int)=>for{c<-5 to n
b<-4 to c
a<-3 to b
if a*a+b*b==c*c}yield(a,b,c)
It's not often that Scala gets a chance to do well in golfing, but the .to() method is perfect for inclusive ranges.
Try it online or on Scastie!
Python, 91 88 bytes
lambda n,r=range:[(k,j,i)for i in r(1,n+1)for j in r(1,i)for k in r(1,j)if i*i==j*j+k*k]
MathGolf, 21 bytes
╒■■mÅ─╡gæ_▀s=gƲxε-┬▀
Explanation:
╒ # Push a list in the range [1, (implicit) input-integer]
■ # Get the cartesian product with itself to create pairs
■ # Get the cartesian product with itself to create pairs of pairs
m # Map over each pair of pairs,
Å # using 2 characters as inner code-block:
─ # Flatten the pair to a quadruplet
╡ # Discard the last item to make it a triplet
g # Filter this list of triplets by,
æ # using 4 characters as inner code-block:
_ # Duplicate the current list
▀ # Uniquify the values in the copy
s # Sort it from lowest to highest
= # Check if the two lists are still the same
g # Filter this list further by,
Æ # using 5 characters as inner code-block:
² # Square each integer in the triplet
x # Reverse it
ε # Reduce the triplet by:
- # Subtracting
┬ # Check if the result of this c²-b²-a² equals 0
▀ # After the filter: uniquify the remaining list of triplets
# (after which the entire stack is output implicitly as result)
Arturo, 50 bytes
$=>[select combine.by:3@1..&'x[=+x\0^2x\1^2x\2^2]]
$=>[ ; an anonymous function
select ; select elements from a block
combine.by:3 @1..& ; combinations of 3 from the range 1 to n
'x ; assign current triplet to x in the select
[ ; begin select
=+x\0^2x\1^2x\2^2 ; is a^2+b^2 equal to c^2?
] ; end select
] ; end function
Factor + math.combinatorics, 57 bytes
[ [1,b] 3 [ 2 v^n first3 -rot + = ] filter-combinations ]
[1,b] 3 [ ... ] filter-combinationsselect combinations of three from \$[1..n]\$ where...2 v^nsquare the elements of the tripletfirst3place each element onto the stack-rotmove \$c^2\$ from the top of the stack to the bottom+add \$a^2\$ and \$b^2\$=equal?
Thunno, \$13\log_{256}(96)\approx\$ 10.70 bytes
R1+3zQg2^Au_=
Port of Kevin Cruijssen's 05AB1E answer.
Explanation
R1+3zQg2^Au_= # Implicit input
R1+ # Push range(1, input+1)
3zQ # Combinations with length 3
g # Filter by: STACK: [a, b, c]
2^ # Square each STACK: [a**2, b**2, c**2]
Au # Dump onto stack STACK: a**2, b**2, c**2
_ # Subtract top two STACK: a**2, c**2 - b**2
= # Are they equal? STACK: a**2 == c**2 - b**2
# Implicit output
><> (Fish), 111 bytes
i:*&43::*{:}:*+\
/*::<5[1;?)&:&:/
\:{:}(?!v~1+31.
/]v?)}:{/|.!051+1~\
n \]~~>1+$:@$:@= ?^50.
\' 'o}:n' 'o}:nao$~$64.
Explanation
><> lacks a square root function so this simply tries all numbers until it finds one greater than the square of the inputs.
First, we square the input. We start searching at a,b=4,3. We check if \$a^2\$, (::*) + \$b^2\$ is more than the input squared, if so we exit.
Then we push 5, the starting third value. Square it, if it is more than a^2+b^2, go to the next iteration. Otherwise, add 1 to C.
If A^2+B^2=C^2, print A, B, and C, then go to next.
When we go to the next iteration, add 1 to B. If B>A, add 1 to A instead and reset B to 1. This is 1+$:@$:@=?~1+1. Then we jump back to the start to try another iteration.
05AB1E, 9 bytes
L3.Æʒn`αQ
Try it online or verify all test cases.
The last three bytes could alternatively be RÆ_ for the same byte-count:
Try it online or verify all test cases.
Explanation:
L # Push a list in the range [1, (implicit) input-integer]
3.Æ # Create all triplet-combinations of this list
ʒ # Filter this list of [a,b,c]-triplets by:
n # Square each inner integer in the triplet
` # Push all three values separated to the stack
α # Get the absolute difference of the top two: |b²-c²|
Q # Check if it's equal to the third one: a² == |b²-c²|
# (after which the filtered list of triplets is output implicitly as result)
R # Reverse the triplet to [c²,b²,a²]
Æ # Reduce it by subtracting: c²-b²-a²
_ # Check if this is 0
Charcoal, 26 bytes
IΣΣE⊕NEιEΦλ⁼×ιι⁺×λλ×νν⟦νλι
Attempt This Online! Link is to verbose version of code. Explanation:
N Input integer
⊕ Incremented
E Map over implicit range
ι Current value
E Map over implicit range
λ Current value
Φ Filter over implicit range
×ιι Square of outer value
⁼ Equals
×λλ Square of inner value
⁺ Plus
×νν Square of innermost value
E Map over matches
⟦ List of
ν Innermost value
λ Inner value
ι Outer value
Σ Flatten
Σ Flatten
I Cast to string
Implicitly print
Japt, 14 bytes
õ à3 fÈ̲ѶXx²
õ à3 fÈ̲ѶXx² :Implicit input of integer U
õ :Range [1,U]
à3 :Combinations of length 3
f :Filter by
È :Passing each X through the following function
Ì : Last element
² : Square
Ñ : Multiply by 2
¶ : Is equal to
Xx : X reduced by addition
² : After squaring each
