Jump to content

Anti-primes: Difference between revisions

→‎{{header|Groovy}}: Initial solution
m (Fix broken task header)
(→‎{{header|Groovy}}: Initial solution)
Line 547:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</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}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.