Heronian triangles: Difference between revisions

Content added Content deleted
No edit summary
m (syntax highlighting fixup automation)
Line 39: Line 39:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F gcd(=u, =v)
<syntaxhighlight lang="11l">F gcd(=u, =v)
L v != 0
L v != 0
(u, v) = (v, u % v)
(u, v) = (v, u % v)
Line 70: Line 70:
print(h[0.<10].map3((x, y, z) -> ‘ #14 perim: #3 area: #.’.format(String((x, y, z)), x + y + z, hero(x, y, z))).join("\n"))
print(h[0.<10].map3((x, y, z) -> ‘ #14 perim: #3 area: #.’.format(String((x, y, z)), x + y + z, hero(x, y, z))).join("\n"))
print("\nAll with area 210 subject to the previous ordering:")
print("\nAll with area 210 subject to the previous ordering:")
print(h.filter3((x, y, z) -> hero(x, y, z) == 210).map3((x, y, z) -> ‘ #14 perim: #3 area: #.’.format(String((x, y, z)), x + y + z, hero(x, y, z))).join("\n"))</lang>
print(h.filter3((x, y, z) -> hero(x, y, z) == 210).map3((x, y, z) -> ‘ #14 perim: #3 area: #.’.format(String((x, y, z)), x + y + z, hero(x, y, z))).join("\n"))</syntaxhighlight>


{{out}}
{{out}}
Line 98: Line 98:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Containers.Indefinite_Ordered_Sets;
<syntaxhighlight lang="ada">with Ada.Containers.Indefinite_Ordered_Sets;
with Ada.Finalization;
with Ada.Finalization;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
Line 228: Line 228:
end loop;
end loop;
end;
end;
end Heronian;</lang>
end Heronian;</syntaxhighlight>
{{out}}
{{out}}
<pre> 517 heronian triangles found :
<pre> 517 heronian triangles found :
Line 257: Line 257:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{Trans|Lua}}
{{Trans|Lua}}
<lang algol68># mode to hold details of a Heronian triangle #
<syntaxhighlight lang="algol68"># mode to hold details of a Heronian triangle #
MODE HERONIAN = STRUCT( INT a, b, c, area, perimeter );
MODE HERONIAN = STRUCT( INT a, b, c, area, perimeter );
# returns the details of the Heronian Triangle with sides a, b, c or nil if it isn't one #
# returns the details of the Heronian Triangle with sides a, b, c or nil if it isn't one #
Line 339: Line 339:
OD
OD
END
END
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 369: Line 369:
=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
{{Trans|Lua}}
{{Trans|Lua}}
<lang algolw>begin
<syntaxhighlight lang="algolw">begin
% record to hold details of a Heronian triangle %
% record to hold details of a Heronian triangle %
record Heronian ( integer a, b, c, area, perimeter );
record Heronian ( integer a, b, c, area, perimeter );
Line 465: Line 465:
end
end
end
end
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 498: Line 498:


{{Trans|JavaScript}}
{{Trans|JavaScript}}
<lang AppleScript>use framework "Foundation"
<syntaxhighlight lang="applescript">use framework "Foundation"


-- HERONIAN TRIANGLES --------------------------------------------------------
-- HERONIAN TRIANGLES --------------------------------------------------------
Line 774: Line 774:
((ca's NSString's stringWithString:(str))'s ¬
((ca's NSString's stringWithString:(str))'s ¬
capitalizedStringWithLocale:(ca's NSLocale's currentLocale())) as text
capitalizedStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toTitle</lang>
end toTitle</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Number of triangles found (with sides <= 200): 517
<pre>Number of triangles found (with sides <= 200): 517
Line 803: Line 803:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>Primitive_Heronian_triangles(MaxSide){
<syntaxhighlight lang="autohotkey">Primitive_Heronian_triangles(MaxSide){
obj :=[]
obj :=[]
loop, % MaxSide {
loop, % MaxSide {
Line 821: Line 821:
x := StrSplit(x, "`t"), y := StrSplit(y, "`t")
x := StrSplit(x, "`t"), y := StrSplit(y, "`t")
return x.1 > y.1 ? 1 : x.1 < y.1 ? -1 : x.2 > y.2 ? 1 : x.2 < y.2 ? -1 : 0
return x.1 > y.1 ? 1 : x.1 < y.1 ? -1 : x.2 > y.2 ? 1 : x.2 < y.2 ? -1 : 0
}</lang>
}</syntaxhighlight>
Examples:<lang AutoHotkey>res := Primitive_Heronian_triangles(200)
Examples:<syntaxhighlight lang="autohotkey">res := Primitive_Heronian_triangles(200)
loop, parse, res, `n, `r
loop, parse, res, `n, `r
{
{
Line 837: Line 837:
. "`nResults for Area = 210:"
. "`nResults for Area = 210:"
. "`n" "Area`tPerimeter`tSides`n" res3
. "`n" "Area`tPerimeter`tSides`n" res3
return</lang>
return</syntaxhighlight>
Outputs:<pre>517 results found
Outputs:<pre>517 results found


Line 867: Line 867:
----
----
'''IMPORTANT''': This is a C99 compatible implementation. May result in errors on earlier compilers.
'''IMPORTANT''': This is a C99 compatible implementation. May result in errors on earlier compilers.
<syntaxhighlight lang="c">
<lang C>
#include<stdlib.h>
#include<stdlib.h>
#include<stdio.h>
#include<stdio.h>
Line 988: Line 988:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
Invocation and output :
Invocation and output :
<pre>
<pre>
Line 1,018: Line 1,018:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang Csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;


Line 1,075: Line 1,075:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Number of primitive Heronian triangles with sides up to 200: 517
<pre>Number of primitive Heronian triangles with sides up to 200: 517
Line 1,103: Line 1,103:
=={{header|C++}}==
=={{header|C++}}==
{{Works with|C++11}}
{{Works with|C++11}}
<lang cpp>#include <algorithm>
<syntaxhighlight lang="cpp">#include <algorithm>
#include <cmath>
#include <cmath>
#include <iostream>
#include <iostream>
Line 1,188: Line 1,188:
std::cout << area(*it) << '\t' << perimeter(*it) << "\t\t" <<
std::cout << area(*it) << '\t' << perimeter(*it) << "\t\t" <<
it->a << 'x' << it->b << 'x' << it->c << '\n';
it->a << 'x' << it->b << 'x' << it->c << '\n';
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>There are 517 primitive Heronian triangles with sides up to 200
<pre>There are 517 primitive Heronian triangles with sides up to 200
Line 1,216: Line 1,216:
=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==
{{trans|JavaScript}}
{{trans|JavaScript}}
<lang coffeescript>heronArea = (a, b, c) ->
<syntaxhighlight lang="coffeescript">heronArea = (a, b, c) ->
s = (a + b + c) / 2
s = (a + b + c) / 2
Math.sqrt s * (s - a) * (s - b) * (s - c)
Math.sqrt s * (s - a) * (s - b) * (s - c)
Line 1,262: Line 1,262:
for i in list
for i in list
if i[4] == 210
if i[4] == 210
console.log i[0..2].join(' x ') + ', p = ' + i[3]</lang>
console.log i[0..2].join(' x ') + ', p = ' + i[3]</syntaxhighlight>
{{out}}
{{out}}
<pre>primitive Heronian triangles with sides up to 200: 517
<pre>primitive Heronian triangles with sides up to 200: 517
Line 1,287: Line 1,287:
=={{header|D}}==
=={{header|D}}==
{{trans|Python}}
{{trans|Python}}
<lang d>import std.stdio, std.math, std.range, std.algorithm, std.numeric, std.traits, std.typecons;
<syntaxhighlight lang="d">import std.stdio, std.math, std.range, std.algorithm, std.numeric, std.traits, std.typecons;


double hero(in uint a, in uint b, in uint c) pure nothrow @safe @nogc {
double hero(in uint a, in uint b, in uint c) pure nothrow @safe @nogc {
Line 1,331: Line 1,331:
"\nAll with area 210 subject to the previous ordering:".writeln;
"\nAll with area 210 subject to the previous ordering:".writeln;
showTriangles(h.filter!(t => t[].hero == 210));
showTriangles(h.filter!(t => t[].hero == 210));
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517
<pre>Primitive Heronian triangles with sides up to 200: 517
Line 1,360: Line 1,360:


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<lang scheme>
<syntaxhighlight lang="scheme">
;; returns quintuple (A s a b c)
;; returns quintuple (A s a b c)
;; or #f if not hero
;; or #f if not hero
Line 1,386: Line 1,386:
(define (print-laurels H)
(define (print-laurels H)
(writeln '🌿🌿 (length H) 'heroes '🌿🌿))
(writeln '🌿🌿 (length H) 'heroes '🌿🌿))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>(define H (heroes))
<pre>(define H (heroes))
Line 1,416: Line 1,416:


=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>defmodule Heronian do
<syntaxhighlight lang="elixir">defmodule Heronian do
def triangle?(a,b,c) when a+b <= c, do: false
def triangle?(a,b,c) when a+b <= c, do: false
def triangle?(a,b,c) do
def triangle?(a,b,c) do
Line 1,451: Line 1,451:
|> Enum.each(fn {a, b, c} ->
|> Enum.each(fn {a, b, c} ->
IO.puts "#{a}\t#{b}\t#{c}\t#{a+b+c}\t#{area_size}"
IO.puts "#{a}\t#{b}\t#{c}\t#{a+b+c}\t#{area_size}"
end)</lang>
end)</syntaxhighlight>


{{out}}
{{out}}
Line 1,476: Line 1,476:


=={{header|ERRE}}==
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROGRAM HERON
PROGRAM HERON


Line 1,544: Line 1,544:
END FOR
END FOR
END PROGRAM
END PROGRAM
</syntaxhighlight>
</lang>
<pre>Number of triangles: 517
<pre>Number of triangles: 517
3 4 5 12 6
3 4 5 12 6
Line 1,565: Line 1,565:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: accessors assocs backtrack combinators.extras
<syntaxhighlight lang="factor">USING: accessors assocs backtrack combinators.extras
combinators.short-circuit formatting io kernel locals math
combinators.short-circuit formatting io kernel locals math
math.functions math.order math.parser math.ranges mirrors qw
math.functions math.order math.parser math.ranges mirrors qw
Line 1,626: Line 1,626:
[ first10 nl ] [ area210= ] bi ;
[ first10 nl ] [ area210= ] bi ;
MAIN: main</lang>
MAIN: main</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,658: Line 1,658:
=={{header|Fortran}}==
=={{header|Fortran}}==
Earlier Fortran doesn't offer special functions such as SUM, PRODUCT and MAXVAL of arrays, nor the ability to create compound data aggregates such as STASH to store a triangle's details. Simple code would have to be used in the absence of such conveniences, and multiple ordinary arrays rather than an array of a compound data entity. Rather than attempt to create the candidate triangles in the desired order, the simple approach is to sort a list of triangles, and using an XNDX array evades tossing compound items about. Rather than create a procedure to do the sorting, a comb sort is not too much trouble to place in-line once. Further, since the ordering is based on a compound key, having only one comparison to code is a boon. The three-way-if statement is central to the expedient evaluation of a compound sort key, but this facility is deprecated by the modernists, with no alternative offered that avoids re-comparison of parts.
Earlier Fortran doesn't offer special functions such as SUM, PRODUCT and MAXVAL of arrays, nor the ability to create compound data aggregates such as STASH to store a triangle's details. Simple code would have to be used in the absence of such conveniences, and multiple ordinary arrays rather than an array of a compound data entity. Rather than attempt to create the candidate triangles in the desired order, the simple approach is to sort a list of triangles, and using an XNDX array evades tossing compound items about. Rather than create a procedure to do the sorting, a comb sort is not too much trouble to place in-line once. Further, since the ordering is based on a compound key, having only one comparison to code is a boon. The three-way-if statement is central to the expedient evaluation of a compound sort key, but this facility is deprecated by the modernists, with no alternative offered that avoids re-comparison of parts.
<syntaxhighlight lang="fortran">
<lang Fortran>
MODULE GREEK MATHEMATICIANS !Two millenia back and more.
MODULE GREEK MATHEMATICIANS !Two millenia back and more.
CONTAINS
CONTAINS
Line 1,766: Line 1,766:
END DO !Just thump through the lot.
END DO !Just thump through the lot.
END
END
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,791: Line 1,791:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' version 02-05-2016
<syntaxhighlight lang="freebasic">' version 02-05-2016
' compile with: fbc -s console
' compile with: fbc -s console


Line 1,937: Line 1,937:
Print : Print "hit any key to end program"
Print : Print "hit any key to end program"
Sleep
Sleep
End</lang>
End</syntaxhighlight>
{{out}}
{{out}}
<pre>There are 517 Heronian triangles with sides <= 200
<pre>There are 517 Heronian triangles with sides <= 200
Line 1,969: Line 1,969:


=={{header|FutureBasic}}==
=={{header|FutureBasic}}==
<lang futurebasic>
<syntaxhighlight lang="futurebasic">
include "ConsoleWindow"
include "ConsoleWindow"


Line 2,056: Line 2,056:
end if
end if
next
next
</syntaxhighlight>
</lang>


Output:
Output:
Line 2,087: Line 2,087:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 2,156: Line 2,156:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Number of primitive Heronian triangles with sides up to 200: 517
<pre>Number of primitive Heronian triangles with sides up to 200: 517
Line 2,183: Line 2,183:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang Haskell>import qualified Data.List as L
<syntaxhighlight lang="haskell">import qualified Data.List as L
import Data.Maybe
import Data.Maybe
import Data.Ord
import Data.Ord
Line 2,245: Line 2,245:
mapM_ printTri $ take 10 tris
mapM_ printTri $ take 10 tris
putStrLn ""
putStrLn ""
mapM_ printTri $ filter ((== 210) . fifth) tris</lang>
mapM_ printTri $ filter ((== 210) . fifth) tris</syntaxhighlight>
{{out}}
{{out}}
<pre>Heronian triangles found: 517
<pre>Heronian triangles found: 517
Line 2,272: Line 2,272:
'''Hero's formula Implementation'''
'''Hero's formula Implementation'''


<lang J>a=: 0&{"1
<syntaxhighlight lang="j">a=: 0&{"1
b=: 1&{"1
b=: 1&{"1
c=: 2&{"1
c=: 2&{"1
Line 2,278: Line 2,278:
area=: 2 %: s*(s-a)*(s-b)*(s-c) NB. Hero's formula
area=: 2 %: s*(s-a)*(s-b)*(s-c) NB. Hero's formula
perim=: +/"1
perim=: +/"1
isPrimHero=: (0&~: * (= <.@:+))@area * 1 = a +. b +. c</lang>
isPrimHero=: (0&~: * (= <.@:+))@area * 1 = a +. b +. c</syntaxhighlight>


We exclude triangles with zero area, triangles with complex area, non-integer area, and triangles whose sides share a common integer multiple.
We exclude triangles with zero area, triangles with complex area, non-integer area, and triangles whose sides share a common integer multiple.
Line 2,286: Line 2,286:
The implementation above uses the symbols as given in the formula at the top of the page, making it easier to follow along as well as spot any errors. That formula distinguishes between the individual sides of the triangles but J could easily treat these sides as a single entity or array. The implementation below uses this "typical J" approach:
The implementation above uses the symbols as given in the formula at the top of the page, making it easier to follow along as well as spot any errors. That formula distinguishes between the individual sides of the triangles but J could easily treat these sides as a single entity or array. The implementation below uses this "typical J" approach:


<lang J>perim=: +/"1
<syntaxhighlight lang="j">perim=: +/"1
s=: -:@:perim
s=: -:@:perim
area=: [: %: s * [: */"1 s - ] NB. Hero's formula
area=: [: %: s * [: */"1 s - ] NB. Hero's formula
isNonZeroInt=: 0&~: *. (= <.@:+)
isNonZeroInt=: 0&~: *. (= <.@:+)
isPrimHero=: isNonZeroInt@area *. 1 = +./&.|:</lang>
isPrimHero=: isNonZeroInt@area *. 1 = +./&.|:</syntaxhighlight>


'''Required examples'''
'''Required examples'''


<lang J> Tri=:(1-i.3)+"1]3 comb 202 NB. distinct triangles with sides <= 200
<syntaxhighlight lang="j"> Tri=:(1-i.3)+"1]3 comb 202 NB. distinct triangles with sides <= 200
HeroTri=: (#~ isPrimHero) Tri NB. all primitive Heronian triangles with sides <= 200
HeroTri=: (#~ isPrimHero) Tri NB. all primitive Heronian triangles with sides <= 200


Line 2,320: Line 2,320:
17 28 39 _ 84 210
17 28 39 _ 84 210
7 65 68 _ 140 210
7 65 68 _ 140 210
3 148 149 _ 300 210</lang>
3 148 149 _ 300 210</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
<lang java>import java.util.ArrayList;
<syntaxhighlight lang="java">import java.util.ArrayList;


public class Heron {
public class Heron {
Line 2,399: Line 2,399:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Number of primitive Heronian triangles with sides up to 200: 517
<pre>Number of primitive Heronian triangles with sides up to 200: 517
Line 2,429: Line 2,429:
===Imperative===
===Imperative===


<syntaxhighlight lang="javascript">
<lang JavaScript>
window.onload = function(){
window.onload = function(){
var list = [];
var list = [];
Line 2,480: Line 2,480:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517
<pre>Primitive Heronian triangles with sides up to 200: 517
Line 2,517: Line 2,517:
ES6 JavaScript introduces syntactic sugar for list comprehensions, but the list monad pattern can already be used in ES5 – indeed in any language which supports the use of higher-order functions.
ES6 JavaScript introduces syntactic sugar for list comprehensions, but the list monad pattern can already be used in ES5 – indeed in any language which supports the use of higher-order functions.


<lang JavaScript>(function (n) {
<syntaxhighlight lang="javascript">(function (n) {
var chain = function (xs, f) { // Monadic bind/chain
var chain = function (xs, f) { // Monadic bind/chain
Line 2,598: Line 2,598:
) + '\n\n';
) + '\n\n';
})(200);</lang>
})(200);</syntaxhighlight>


{{out}}
{{out}}
Line 2,653: Line 2,653:
=={{header|jq}}==
=={{header|jq}}==
{{works with|jq|1.4}}
{{works with|jq|1.4}}
<lang jq># input should be an array of the lengths of the sides
<syntaxhighlight lang="jq"># input should be an array of the lengths of the sides
def hero:
def hero:
(add/2) as $s
(add/2) as $s
Line 2,692: Line 2,692:
( .[] | select( hero == 210 ) | "\(rjust(11)) \(add|rjust(3)) \(hero|rjust(4))" ) ;
( .[] | select( hero == 210 ) | "\(rjust(11)) \(add|rjust(3)) \(hero|rjust(4))" ) ;


task(200)</lang>
task(200)</syntaxhighlight>
{{out}}
{{out}}
<lang sh>$ time jq -n -r -f heronian.jq
<syntaxhighlight lang="sh">$ time jq -n -r -f heronian.jq
The number of primitive Heronian triangles with sides up to 200: 517
The number of primitive Heronian triangles with sides up to 200: 517
The first ten when ordered by increasing area, then perimeter, then maximum sides:
The first ten when ordered by increasing area, then perimeter, then maximum sides:
Line 2,715: Line 2,715:
[17,28,39] 84 210
[17,28,39] 84 210
[7,65,68] 140 210
[7,65,68] 140 210
[3,148,149] 300 210</lang>
[3,148,149] 300 210</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
Line 2,721: Line 2,721:


'''Types and Functions'''
'''Types and Functions'''
<syntaxhighlight lang="julia">
<lang Julia>
type IntegerTriangle{T<:Integer}
type IntegerTriangle{T<:Integer}
a::T
a::T
Line 2,748: Line 2,748:
σ^2 == t
σ^2 == t
end
end
</syntaxhighlight>
</lang>


'''Main'''
'''Main'''
<syntaxhighlight lang="julia">
<lang Julia>
slim = 200
slim = 200


Line 2,785: Line 2,785:
println(@sprintf "%6d %3d %3d %4d %4d" t.a t.b t.c t.σ t.p)
println(@sprintf "%6d %3d %3d %4d %4d" t.a t.b t.c t.σ t.p)
end
end
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,814: Line 2,814:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Scala}}
{{trans|Scala}}
<lang scala>import java.util.ArrayList
<syntaxhighlight lang="scala">import java.util.ArrayList


object Heron {
object Heron {
Line 2,877: Line 2,877:
}
}


fun main(args: Array<String>) = Heron.run()</lang>
fun main(args: Array<String>) = Heron.run()</syntaxhighlight>
{{out}}
{{out}}
<pre>Number of primitive Heronian triangles with sides up to 200: 517
<pre>Number of primitive Heronian triangles with sides up to 200: 517
Line 2,904: Line 2,904:


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>-- Returns the details of the Heronian Triangle with sides a, b, c or nil if it isn't one
<syntaxhighlight lang="lua">-- Returns the details of the Heronian Triangle with sides a, b, c or nil if it isn't one
local function tryHt( a, b, c )
local function tryHt( a, b, c )
local result
local result
Line 2,964: Line 2,964:
local t = ht[ htPos ];
local t = ht[ htPos ];
if t.area == 210 then htPrint( t ) end
if t.area == 210 then htPrint( t ) end
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,993: Line 2,993:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>ClearAll[Heron]
<syntaxhighlight lang="mathematica">ClearAll[Heron]
Heron[a_, b_, c_] := With[{s = (a + b + c)/2}, Sqrt[s (s - a) (s - b) (s - c)]]
Heron[a_, b_, c_] := With[{s = (a + b + c)/2}, Sqrt[s (s - a) (s - b) (s - c)]]
PrintTemporary[Dynamic[{a, b, c}]];
PrintTemporary[Dynamic[{a, b, c}]];
Line 3,015: Line 3,015:
results // Length
results // Length
Take[results, 10] // Dataset
Take[results, 10] // Dataset
Select[results, #["Area"] == 210 &] // Dataset</lang>
Select[results, #["Area"] == 210 &] // Dataset</syntaxhighlight>
{{out}}
{{out}}
<pre>517
<pre>517
Line 3,040: Line 3,040:


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>import math, algorithm, lenientops, strformat, sequtils
<syntaxhighlight lang="nim">import math, algorithm, lenientops, strformat, sequtils


type HeronianTriangle = tuple[a, b, c: int; p: int; area: int]
type HeronianTriangle = tuple[a, b, c: int; p: int; area: int]
Line 3,096: Line 3,096:
echo Header
echo Header
for t in list.filterIt(it.area == 210): echo t
for t in list.filterIt(it.area == 210): echo t
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>Number of Heronian triangles: 517
<pre>Number of Heronian triangles: 517
Line 3,126: Line 3,126:
=={{header|ooRexx}}==
=={{header|ooRexx}}==
Derived from REXX with some changes
Derived from REXX with some changes
<lang rexx>/*REXX pgm generates primitive Heronian triangles by side length & area.*/
<syntaxhighlight lang="rexx">/*REXX pgm generates primitive Heronian triangles by side length & area.*/
Call time 'R'
Call time 'R'
Numeric Digits 12
Numeric Digits 12
Line 3,263: Line 3,263:
::requires rxmath library
::requires rxmath library
::routine sqrt
::routine sqrt
Return rxCalcSqrt(arg(1),14)</lang>
Return rxCalcSqrt(arg(1),14)</syntaxhighlight>
{{out}}
{{out}}
<pre>517 primitive Heronian triangles found with side length up to 200 (inclusive).
<pre>517 primitive Heronian triangles found with side length up to 200 (inclusive).
Line 3,290: Line 3,290:
=={{header|PARI/GP}}==
=={{header|PARI/GP}}==


<lang parigp>Heron(v)=my([a,b,c]=v); (a+b+c)*(-a+b+c)*(a-b+c)*(a+b-c) \\ returns 16 times the squared area
<syntaxhighlight lang="parigp">Heron(v)=my([a,b,c]=v); (a+b+c)*(-a+b+c)*(a-b+c)*(a+b-c) \\ returns 16 times the squared area
is(a,b,c)=(a+b+c)%2==0 && gcd(a,gcd(b,c))==1 && issquare(Heron([a,b,c]))
is(a,b,c)=(a+b+c)%2==0 && gcd(a,gcd(b,c))==1 && issquare(Heron([a,b,c]))
v=List(); for(a=1,200,for(b=a+1,200,for(c=b+1,200, if(is(a,b,c),listput(v, [a,b,c])))));
v=List(); for(a=1,200,for(b=a+1,200,for(c=b+1,200, if(is(a,b,c),listput(v, [a,b,c])))));
Line 3,300: Line 3,300:
vecsort(u, (a,b)->Heron(a)-Heron(b))
vecsort(u, (a,b)->Heron(a)-Heron(b))
vecsort(u, (a,b)->vecsum(a)-vecsum(b))
vecsort(u, (a,b)->vecsum(a)-vecsum(b))
vecsort(u, 3) \\ shortcut: order by third component</lang>
vecsort(u, 3) \\ shortcut: order by third component</syntaxhighlight>
{{out}}
{{out}}
<pre>%1 = [[1, 2, 3], [1, 3, 4], [1, 4, 5], [1, 5, 6], [1, 6, 7], [1, 7, 8], [1, 8, 9], [1, 9, 10], [1, 10, 11], [1, 11, 12]]
<pre>%1 = [[1, 2, 3], [1, 3, 4], [1, 4, 5], [1, 5, 6], [1, 6, 7], [1, 7, 8], [1, 8, 9], [1, 9, 10], [1, 10, 11], [1, 11, 12]]
Line 3,311: Line 3,311:
=={{header|Pascal}}==
=={{header|Pascal}}==
{{Trans|Lua}}
{{Trans|Lua}}
<lang pascal>program heronianTriangles ( input, output );
<syntaxhighlight lang="pascal">program heronianTriangles ( input, output );
type
type
(* record to hold details of a Heronian triangle *)
(* record to hold details of a Heronian triangle *)
Line 3,411: Line 3,411:
if t^.area = 210 then htPrint( t )
if t^.area = 210 then htPrint( t )
end
end
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,441: Line 3,441:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use List::Util qw(max);
use List::Util qw(max);
Line 3,503: Line 3,503:
}
}


&main();</lang>
&main();</syntaxhighlight>
{{out}}
{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517
<pre>Primitive Heronian triangles with sides up to 200: 517
Line 3,528: Line 3,528:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">function</span> <span style="color: #000000;">heroArea</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">heroArea</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">+</span><span style="color: #000000;">b</span><span style="color: #0000FF;">+</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">+</span><span style="color: #000000;">b</span><span style="color: #0000FF;">+</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span>
Line 3,567: Line 3,567:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 3,596: Line 3,596:


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<lang powershell>
<syntaxhighlight lang="powershell">
function Get-Gcd($a, $b){
function Get-Gcd($a, $b){
if($a -ge $b){
if($a -ge $b){
Line 3,644: Line 3,644:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<lang>
<syntaxhighlight lang="text">
Primitive Heronian triangles with sides up to 200: 517
Primitive Heronian triangles with sides up to 200: 517


Line 3,670: Line 3,670:
7 65 68 140 210
7 65 68 140 210
3 148 149 300 210
3 148 149 300 210
</syntaxhighlight>
</lang>


=={{header|Python}}==
=={{header|Python}}==


<lang python>from __future__ import division, print_function
<syntaxhighlight lang="python">from __future__ import division, print_function
from math import gcd, sqrt
from math import gcd, sqrt


Line 3,718: Line 3,718:
% (sides, sum(sides), hero(*sides)) for sides in h
% (sides, sum(sides), hero(*sides)) for sides in h
if hero(*sides) == 210))
if hero(*sides) == 210))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517
<pre>Primitive Heronian triangles with sides up to 200: 517
Line 3,746: Line 3,746:
Mostly adopted from Python implementation:
Mostly adopted from Python implementation:


<syntaxhighlight lang="r">
<lang R>
area <- function(a, b, c) {
area <- function(a, b, c) {
s = (a + b + c) / 2
s = (a + b + c) / 2
Line 3,783: Line 3,783:
cat("Showing the first ten ordered first by perimeter, then by area:\n")
cat("Showing the first ten ordered first by perimeter, then by area:\n")
print(head(r[order(x=r[,"perimeter"],y=r[,"area"]),],n=10))
print(head(r[order(x=r[,"perimeter"],y=r[,"area"]),],n=10))
</syntaxhighlight>
</lang>


{{out}}
{{out}}


<lang>There are 517 Heronian triangles up to a maximal side length of 200.
<syntaxhighlight lang="text">There are 517 Heronian triangles up to a maximal side length of 200.
Showing the first ten ordered first by perimeter, then by area:
Showing the first ten ordered first by perimeter, then by area:
a b c perimeter area
a b c perimeter area
Line 3,799: Line 3,799:
[8,] 8 15 17 40 60
[8,] 8 15 17 40 60
[9,] 7 15 20 42 42
[9,] 7 15 20 42 42
[10,] 13 14 15 42 84</lang>
[10,] 13 14 15 42 84</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==


<lang>#lang at-exp racket
<syntaxhighlight lang="text">#lang at-exp racket
(require data/order scribble/html)
(require data/order scribble/html)


Line 3,847: Line 3,847:
@; Show a similar ordered table for those triangles with area = 210
@; Show a similar ordered table for those triangles with area = 210
@(triangles->table (tri-sort (filter (λ(t) (eq? 210 (car t))) ts)))
@(triangles->table (tri-sort (filter (λ(t) (eq? 210 (car t))) ts)))
}))</lang>
}))</syntaxhighlight>


This program generates HTML, so the output is inline with the page, not in a <code>&lt;pre></code> block.
This program generates HTML, so the output is inline with the page, not in a <code>&lt;pre></code> block.
Line 3,880: Line 3,880:
(formerly Perl 6)
(formerly Perl 6)
{{works with|Rakudo|2018.09}}
{{works with|Rakudo|2018.09}}
<lang perl6>sub hero($a, $b, $c) {
<syntaxhighlight lang="raku" line>sub hero($a, $b, $c) {
my $s = ($a + $b + $c) / 2;
my $s = ($a + $b + $c) / 2;
($s * ($s - $a) * ($s - $b) * ($s - $c)).sqrt;
($s * ($s - $a) * ($s - $b) * ($s - $c)).sqrt;
Line 3,922: Line 3,922:
say "\nArea $witharea:";
say "\nArea $witharea:";
show @h.grep: *[0] == $witharea;
show @h.grep: *[0] == $witharea;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517
<pre>Primitive Heronian triangles with sides up to 200: 517
Line 3,978: Line 3,978:
This REXX version doesn't need to explicitly sort the triangles as they are listed in the proper order.
This REXX version doesn't need to explicitly sort the triangles as they are listed in the proper order.
<lang rexx>/*REXX program generates & displays primitive Heronian triangles by side length and area*/
<syntaxhighlight lang="rexx">/*REXX program generates & displays primitive Heronian triangles by side length and area*/
parse arg N first area . /*obtain optional arguments from the CL*/
parse arg N first area . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N= 200 /*Not specified? Then use the default.*/
if N=='' | N=="," then N= 200 /*Not specified? Then use the default.*/
Line 4,030: Line 4,030:
end /*k*/
end /*k*/
end /*j*/ /* [↑] use the known perimeters. */
end /*j*/ /* [↑] use the known perimeters. */
end /*i*/; return /* [↑] show any found triangles. */</lang>
end /*i*/; return /* [↑] show any found triangles. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 4,065: Line 4,065:


It is about eight times faster than the 1<sup>st</sup> REXX version.
It is about eight times faster than the 1<sup>st</sup> REXX version.
<lang rexx>/*REXX program generates & displays primitive Heronian triangles by side length and area*/
<syntaxhighlight lang="rexx">/*REXX program generates & displays primitive Heronian triangles by side length and area*/
parse arg N first area . /*obtain optional arguments from the CL*/
parse arg N first area . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N= 200 /*Not specified? Then use the default.*/
if N=='' | N=="," then N= 200 /*Not specified? Then use the default.*/
Line 4,110: Line 4,110:
end /*k*/
end /*k*/
end /*j*/ /* [↑] use the known perimeters. */
end /*j*/ /* [↑] use the known perimeters. */
end /*i*/; return /* [↑] show any found triangles. */</lang>
end /*i*/; return /* [↑] show any found triangles. */</syntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Heronian triangles
# Project : Heronian triangles


Line 4,155: Line 4,155:
end
end
return gcd
return gcd
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 4,183: Line 4,183:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>class Triangle
<syntaxhighlight lang="ruby">class Triangle
def self.valid?(a,b,c) # class method
def self.valid?(a,b,c) # class method
short, middle, long = [a, b, c].sort
short, middle, long = [a, b, c].sort
Line 4,228: Line 4,228:
puts sorted.first(10).map(&:to_s)
puts sorted.first(10).map(&:to_s)
puts "\nTriangles with an area of: #{area}"
puts "\nTriangles with an area of: #{area}"
sorted.each{|tr| puts tr if tr.area == area}</lang>
sorted.each{|tr| puts tr if tr.area == area}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,255: Line 4,255:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>
<syntaxhighlight lang="rust">
use num_integer::Integer;
use num_integer::Integer;
use std::{f64, usize};
use std::{f64, usize};
Line 4,323: Line 4,323:
}
}


</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 4,350: Line 4,350:


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>object Heron extends scala.collection.mutable.MutableList[Seq[Int]] with App {
<syntaxhighlight lang="scala">object Heron extends scala.collection.mutable.MutableList[Seq[Int]] with App {
private final val n = 200
private final val n = 200
for (c <- 1 to n; b <- 1 to c; a <- 1 to b if gcd(gcd(a, b), c) == 1) {
for (c <- 1 to n; b <- 1 to c; a <- 1 to b if gcd(gcd(a, b), c) == 1) {
Line 4,384: Line 4,384:
private final val header = "\nSides Perimeter Area"
private final val header = "\nSides Perimeter Area"
private def format: Seq[Int] => String = "\n%3d x %3d x %3d %5d %10d".format
private def format: Seq[Int] => String = "\n%3d x %3d x %3d %5d %10d".format
}</lang>
}</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang ruby>class Triangle(a, b, c) {
<syntaxhighlight lang="ruby">class Triangle(a, b, c) {


has (sides, perimeter, area)
has (sides, perimeter, area)
Line 4,435: Line 4,435:
say sorted.first(10).join("\n")
say sorted.first(10).join("\n")
say "\nTriangles with an area of: #{area}"
say "\nTriangles with an area of: #{area}"
sorted.each{|tr| say tr if (tr.area == area)}</lang>
sorted.each{|tr| say tr if (tr.area == area)}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,463: Line 4,463:
=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
Works with Squeak 5.x
Works with Squeak 5.x
<lang Smalltalk>perimeter := [:triangle | triangle reduce: #+].
<syntaxhighlight lang="smalltalk">perimeter := [:triangle | triangle reduce: #+].


squaredArea := [:triangle |
squaredArea := [:triangle |
Line 4,503: Line 4,503:
area210 do: tabulate.
area210 do: tabulate.


Transcript flush.</lang>
Transcript flush.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,532: Line 4,532:


=={{header|SPL}}==
=={{header|SPL}}==
<lang spl>h,t = getem(200)
<syntaxhighlight lang="spl">h,t = getem(200)
#.sort(h,4,5,1,2,3)
#.sort(h,4,5,1,2,3)
#.output("There are ",t," Heronian triangles")
#.output("There are ",t," Heronian triangles")
Line 4,564: Line 4,564:
s = (a+b+c)/2
s = (a+b+c)/2
<= (s*(s-a)*(s-b)*(s-c))^0.5, s*2
<= (s*(s-a)*(s-b)*(s-c))^0.5, s*2
.</lang>
.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,591: Line 4,591:
=={{header|Swift}}==
=={{header|Swift}}==
Works with Swift 1.2
Works with Swift 1.2
<lang Swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


typealias PrimitiveHeronianTriangle = (s1:Int, s2:Int, s3:Int, p:Int, a:Int)
typealias PrimitiveHeronianTriangle = (s1:Int, s2:Int, s3:Int, p:Int, a:Int)
Line 4,644: Line 4,644:
for t in triangles[0...9] {
for t in triangles[0...9] {
println("\(t.s1)\t\(t.s2)\t\(t.s3)\t\t\(t.p)\t\t\(t.a)")
println("\(t.s1)\t\(t.s2)\t\(t.s3)\t\t\(t.p)\t\t\(t.a)")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 4,667: Line 4,667:


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>
<syntaxhighlight lang="tcl">
if {[info commands let] eq ""} {
if {[info commands let] eq ""} {


Line 4,759: Line 4,759:
sqlite3 db :memory:
sqlite3 db :memory:
main db
main db
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 4,787: Line 4,787:


=={{header|VBA}}==
=={{header|VBA}}==
{{trans|Phix}}<lang vb>Function heroArea(a As Integer, b As Integer, c As Integer) As Double
{{trans|Phix}}<syntaxhighlight lang="vb">Function heroArea(a As Integer, b As Integer, c As Integer) As Double
s = (a + b + c) / 2
s = (a + b + c) / 2
On Error GoTo Err
On Error GoTo Err
Line 4,838: Line 4,838:
End If
End If
Next i
Next i
End Sub</lang>{{out}}
End Sub</syntaxhighlight>{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517 (of 1353400 tested)
<pre>Primitive Heronian triangles with sides up to 200: 517 (of 1353400 tested)


Line 4,867: Line 4,867:
{{libheader|Wren-sort}}
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/math" for Int, Nums
<syntaxhighlight lang="ecmascript">import "/math" for Int, Nums
import "/sort" for Sort
import "/sort" for Sort
import "/fmt" for Fmt
import "/fmt" for Fmt
Line 4,916: Line 4,916:
var sides = Fmt.swrite("$2d x $3d x $3d", t[0][0], t[0][1], t[0][2])
var sides = Fmt.swrite("$2d x $3d x $3d", t[0][0], t[0][1], t[0][2])
Fmt.print("$-14s $3d $3d $3d", sides, t[1], t[2], t[3])
Fmt.print("$-14s $3d $3d $3d", sides, t[1], t[2], t[3])
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 4,947: Line 4,947:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|Python}}
{{trans|Python}}
<lang zkl>fcn hero(a,b,c){ //--> area (float)
<syntaxhighlight lang="zkl">fcn hero(a,b,c){ //--> area (float)
s,a2:=(a + b + c).toFloat()/2, s*(s - a)*(s - b)*(s - c);
s,a2:=(a + b + c).toFloat()/2, s*(s - a)*(s - b)*(s - c);
(a2 > 0) and a2.sqrt() or 0.0
(a2 > 0) and a2.sqrt() or 0.0
Line 4,954: Line 4,954:
A:=hero(a,b,c);
A:=hero(a,b,c);
(A>0) and A.modf()[1].closeTo(0.0,1.0e-6) and A //--> area or False
(A>0) and A.modf()[1].closeTo(0.0,1.0e-6) and A //--> area or False
}</lang>
}</syntaxhighlight>
<lang zkl>const MAX_SIDE=200;
<syntaxhighlight lang="zkl">const MAX_SIDE=200;
heros:=Sink(List);
heros:=Sink(List);
foreach a,b,c in ([1..MAX_SIDE],[a..MAX_SIDE],[b..MAX_SIDE]){
foreach a,b,c in ([1..MAX_SIDE],[a..MAX_SIDE],[b..MAX_SIDE]){
Line 4,976: Line 4,976:
println("Area Perimeter Sides");
println("Area Perimeter Sides");
heros.filter(fcn([(h,_)]){ h==210 })
heros.filter(fcn([(h,_)]){ h==210 })
.pump(fcn(phabc){ "%3s %8d %3dx%dx%d".fmt(phabc.xplode()).println() });</lang>
.pump(fcn(phabc){ "%3s %8d %3dx%dx%d".fmt(phabc.xplode()).println() });</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>