Jump to content

Sequence: smallest number with exactly n divisors: Difference between revisions

Added C
(→‎{{header|REXX}}: added optimization (but not really needed for only finding 15 numbers).)
(Added C)
Line 14:
:*[[Sequence: smallest number greater than previous term with exactly n divisors]]
:*[[Sequence: nth number with exactly n divisors‎‎]]
 
=={{header|C}}==
{{trans|Go}}
<lang c>#include <stdio.h>
 
#define MAX 15
 
int count_divisors(int n) {
int i, count = 0;
for (i = 1; i * i <= n; ++i) {
if (!(n % i)) {
if (i == n / i)
count++;
else
count += 2;
}
}
return count;
}
 
int main() {
int i, k, n, seq[MAX];
for (i = 0; i < MAX; ++i) seq[i] = 0;
printf("The first %d terms of the sequence are:\n", MAX);
for (i = 1, n = 0; n < MAX; ++i) {
k = count_divisors(i);
if (k <= MAX && seq[k - 1] == 0) {
seq[k - 1] = i;
++n;
}
}
for (i = 0; i < MAX; ++i) printf("%d ", seq[i]);
printf("\n");
return 0;
}</lang>
 
{{out}}
<pre>
The first 15 terms of the sequence are:
1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144
</pre>
 
=={{header|Go}}==
9,490

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.