Anti-primes: Difference between revisions
Content added Content deleted
(Added Java) |
(Added C++) |
||
Line 37: | Line 37: | ||
} |
} |
||
printf("\n"); |
printf("\n"); |
||
return 0; |
|||
}</lang> |
|||
{{out}} |
|||
<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|C++}}== |
|||
{{trans|C}} |
|||
<lang cpp>#include <iostream> |
|||
int countDivisors(int n) { |
|||
if (n < 2) return 1; |
|||
int count = 2; // 1 and n |
|||
for (int i = 2; i <= n/2; ++i) { |
|||
if (n%i == 0) ++count; |
|||
} |
|||
return count; |
|||
} |
|||
int main() { |
|||
int maxDiv = 0, count = 0; |
|||
std::cout << "The first 20 anti-primes are:" << std::endl; |
|||
for (int n = 1; count < 20; ++n) { |
|||
int d = countDivisors(n); |
|||
if (d > maxDiv) { |
|||
std::cout << n << " "; |
|||
maxDiv = d; |
|||
count++; |
|||
} |
|||
} |
|||
std::cout << std::endl; |
|||
return 0; |
return 0; |
||
}</lang> |
}</lang> |