Anti-primes: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (Fix broken task header) |
(→{{header|Groovy}}: Initial solution) |
||
Line 547: | Line 547: | ||
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
||
</pre> |
</pre> |
||
=={{header|Groovy}}== |
|||
Solution (uses [[Factors_of_an_integer#Groovy|Factors of an integer]] function "factorize()"): |
|||
<lang groovy>def getAntiPrimes(def limit = 10) { |
|||
def antiPrimes = [] |
|||
def candidate = 1L |
|||
def maxFactors = 0 |
|||
while (antiPrimes.size() < limit) { |
|||
def factors = factorize(candidate) |
|||
if (factors.size() > maxFactors) { |
|||
maxFactors = factors.size() |
|||
antiPrimes << candidate |
|||
} |
|||
candidate++ |
|||
} |
|||
antiPrimes |
|||
}</lang> |
|||
Test: |
|||
<lang groovy>println (getAntiPrimes(20))</lang> |
|||
Output: |
|||
<pre>[1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560]</pre> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |