The sieve of Sundaram: Difference between revisions

Content added Content deleted
m (→‎{{header|AppleScript}}: Optimisation.)
Line 464: Line 464:
);
);
};
};



// nSundaramsPrimes :: Int -> [Int]
// nSundaramsPrimes :: Int -> [Int]
Line 472: Line 473:
)
)
.slice(0, n);
.slice(0, n);



// ---------------------- TEST -----------------------
// ---------------------- TEST -----------------------
const main = () => (
const main = () => [
xs => [
"First 100 Sundaram primes",
"First hundred Sundaram primes",
"(starting at 3):\n",
"(starting at 3):",
table(10)(" ")(
`\n${xs}`
nSundaramPrimes(100)
].join("\n")
.map(n => `${n}`)
)(
table(" ")(
chunksOf(10)(
nSundaramPrimes(100)
.map(n => `${n}`)
)
)
)
);
].join("\n");




Line 504: Line 500:
// xs split into sublists of length n.
// xs split into sublists of length n.
// The last sublist will be short if n
// The last sublist will be short if n
// does not evenly divide the length of xs .
// does not evenly divide the length of xs.
const go = xs => {
const go = xs => {
const chunk = xs.slice(0, n);
const chunk = xs.slice(0, n);
Line 517: Line 513:
return go;
return go;
};
};


// table :: String ->
// (Int -> Char -> String -> String) ->
// [[String]] -> String
const table = gap =>
// A tabulation of rows of string values,
// with a specified gap between columns.
rows => {
const
lastRow = rows[rows.length - 1],
w = lastRow[lastRow.length - 1].length;

return rows.map(
row => row.map(
justifyRight(w)(" ")
).join(gap)
).join("\n");
};




Line 545: Line 522:
s.padStart(n, c)
s.padStart(n, c)
) : "";
) : "";


// table :: Int -> String -> [[String]] -> String
const table = colCount =>
// A tabulation of rows of string values,
// with a specified gap between columns.
gap => xs => {
const w = xs[xs.length - 1].length;

return chunksOf(colCount)(xs)
.map(
row => row.map(
justifyRight(w)(" ")
).join(gap)
)
.join("\n");
};


return main();
return main();
})();</lang>
})();</lang>
{{Out}}
{{Out}}
<pre>First hundred Sundaram primes
<pre>First 100 Sundaram primes
(starting at 3):
(starting at 3):