Anti-primes: Difference between revisions
Content deleted Content added
Added C# |
|||
Line 1,515:
<pre>The first 20 anti-primes are:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 </pre>
=={{header|Scala}}==
This program uses an iterator to count the factors of a number, then builds a lazily evaluated list of all anti-primes. Finding the first 20 anti-primes involves merely taking the first 20 elements of the list.
<lang scala>def factorCount(num: Int): Int = Iterator.range(1, num/2 + 1).count(num%_ == 0) + 1
def antiPrimes: LazyList[Int] = LazyList.iterate((1: Int, 1: Int)){case (n, facs) => Iterator.from(n + 1).map(i => (i, factorCount(i))).dropWhile(_._2 <= facs).next}.map(_._1)</lang>
{{out}}
<pre>scala> print(antiPrimes.take(20).mkString(", "))
1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560</pre>
=={{header|Seed7}}==
|