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 = () => [ |
||
"First 100 Sundaram primes", |
|||
"(starting at 3):\n", |
|||
table(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; |
||
}; |
}; |
||
⚫ | |||
// (Int -> Char -> String -> String) -> |
|||
// [[String]] -> String |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
w = lastRow[lastRow.length - 1].length; |
|||
⚫ | |||
row => row.map( |
|||
⚫ | |||
).join(gap) |
|||
).join("\n"); |
|||
}; |
|||
Line 545: | Line 522: | ||
s.padStart(n, c) |
s.padStart(n, c) |
||
) : ""; |
) : ""; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return main(); |
return main(); |
||
})();</lang> |
})();</lang> |
||
{{Out}} |
{{Out}} |
||
<pre>First |
<pre>First 100 Sundaram primes |
||
(starting at 3): |
(starting at 3): |
||