Anti-primes | Rosetta Code | #3
URL to the problem page: https://rosettacode.org/wiki/Anti-primes
The anti-primes (or highly composite numbers) are the natural numbers with more factors than any smaller than itself.
Generate and show here, the first twenty anti-primes.
The anti-primes (or highly composite numbers) are the natural numbers with more factors than any smaller than itself.
Generate and show here, the first twenty anti-primes.
#include <iostream>
using namespace std;
int main()
{
int counter = 1, counter2, counter3, check, numbers[20] = { 0 }, a = 0;
for (int i = 1; counter < 21; i++) {
check = 0;
counter2 = 0;
for (int j = 1; j <= (i / 2); j++) {
if (i % j == 0) {
counter2++;
}
}
for (int j = 1; j < i; j++) {
counter3 = 0;
for (int k = 1; k <= (j / 2); k++) {
if (j % k == 0) {
counter3++;
}
}
if (counter3 >= counter2) {
check++;
break;
}
}
if (check == 0) {
numbers[a] = i;
a++;
counter++;
}
}
cout << "First twenty anti-primes are:" << endl;
for (int i = 0; i < 20; i++) {
cout << numbers[i] << endl;
}
return 0;
}
Comments
Post a Comment