Law of cosines - triples: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
→‎{{header|JavaScript}}: Edited to collect solutions in a Set object
Hout (talk | contribs)
Line 359: Line 359:
const
const
xs = enumFromTo(1, n),
xs = enumFromTo(1, n),
dctSquares = foldl(
fr = f(xs.reduce((a, x) => (a[x * x] = x, a), {})),
(a, x) => (a[x] = x * x, a), {},
gc = xs.reduce((a, _) => a, {}),
xs
),
dctRoots = foldl(
(a, x) => (a[x * x] = x, a), {},
xs
),
fr = f(dctRoots),
setSoln = new Set();
setSoln = new Set();
return (
return (
xs.forEach(
xs.forEach(
a => {
a => {
const a2 = dctSquares[a];
const a2 = a * a;
enumFromTo(1, 1 + a).forEach(
enumFromTo(1, 1 + a).forEach(
b => {
b => {
const
const
suma2b2 = a2 + dctSquares[b],
suma2b2 = a2 + b * b,
c = fr(suma2b2, a * b, a, b);
c = fr(suma2b2, a * b, a, b);
if (undefined !== c) {
if (undefined !== c) {
Line 412: Line 405:


// GENERIC FUNCTIONS ----------------------------
// GENERIC FUNCTIONS ----------------------------


// descending :: a -> a -> Ordering
const descending = (a, b) => a < b ? 1 : (a > b ? -1 : 0);


// concatMap :: (a -> [b]) -> [a] -> [b]
// concatMap :: (a -> [b]) -> [a] -> [b]
Line 428: Line 417:
m
m
) : [];
) : [];

// flip :: (a -> b -> c) -> b -> a -> c
const flip = f =>
1 < f.length ? (
(a, b) => f(b, a)
) : (x => y => f(y)(x));

// foldl :: (a -> b -> a) -> a -> [b] -> a
const foldl = (f, a, xs) => xs.reduce(f, a);



// iterateUntil :: (a -> Bool) -> (a -> a) -> a -> [a]
// iterateUntil :: (a -> Bool) -> (a -> a) -> a -> [a]
Line 453: Line 432:
// length :: [a] -> Int
// length :: [a] -> Int
const length = xs => xs.length || Infinity;
const length = xs => xs.length || Infinity;


// sortBy :: (a -> a -> Ordering) -> [a] -> [a]
const sortBy = (f, xs) =>
xs.slice()
.sort(f);



// take :: Int -> [a] -> [a]
// take :: Int -> [a] -> [a]
Line 475: Line 447:
// unlines :: [String] -> String
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
const unlines = xs => xs.join('\n');

// Use of `take` and `length` here allows zipping with non-finite lists
// i.e. generators like cycle, repeat, iterate.


// Use of `take` and `length` here allows zipping with non-finite lists
// Use of `take` and `length` here allows zipping with non-finite lists