Multiplicatively perfect numbers: Difference between revisions
Content deleted Content added
→{{header|Wren}}: Optimized to enable 5 million to be reached in a reasonable time. |
→{{header|C}}: Optimized and extended to 5 million, |
||
Line 101: | Line 101: | ||
=={{header|C}}== |
=={{header|C}}== |
||
This includes '1' as an MPN. |
This includes '1' as an MPN. Run time around 3.2 seconds. |
||
<syntaxhighlight lang="c">#include <stdio.h> |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdbool.h> |
#include <stdbool.h> |
||
Line 121: | Line 121: | ||
void divisors(int n, int *divs, int *length) { |
void divisors(int n, int *divs, int *length) { |
||
if (n < 1) { |
|||
*length = 0; |
|||
return; |
|||
} |
|||
int i, j, k = 1, c = 0; |
int i, j, k = 1, c = 0; |
||
if (n%2) k = 2; |
if (n%2) k = 2; |
||
Line 131: | Line 127: | ||
if (!(n%i)) { |
if (!(n%i)) { |
||
divs[c++] = i; |
divs[c++] = i; |
||
if (c > 3) break; // not eligible if has > 2 divisors |
|||
j = n / i; |
j = n / i; |
||
if (j != i) divs[c++] = j; |
if (j != i) divs[c++] = j; |
||
Line 140: | Line 137: | ||
int main() { |
int main() { |
||
int i, d, j, k, t, length, prod; |
int i, d, j, k, t, length, prod; |
||
int divs[ |
int divs[5], count = 0, limit = 500, s = 3, c = 3, squares = 1, cubes = 1; |
||
printf("Multiplicatively perfect numbers under %d:\n", limit); |
printf("Multiplicatively perfect numbers under %d:\n", limit); |
||
setlocale(LC_NUMERIC, ""); |
setlocale(LC_NUMERIC, ""); |
||
for (i = |
for (i = 1; ; ++i) { |
||
if (i != 1) { |
if (i != 1) { |
||
divisors(i, divs, &length); |
divisors(i, divs, &length); |
||
Line 150: | Line 147: | ||
length = 2; |
length = 2; |
||
} |
} |
||
if (length |
if (length == 2 && divs[0] * divs[1] == i) { |
||
++count; |
|||
if (i < 500) { |
|||
printf("%3d ", i); |
|||
if (!(count%10)) printf("\n"); |
|||
if (i < 500) { |
|||
printf("%3d ", i); |
|||
if (!(count%10)) printf("\n"); |
|||
} |
|||
} |
} |
||
} |
} |
||
Line 166: | Line 159: | ||
for (k = c; k * k * k < limit; k +=2 ) if (isPrime(k)) ++cubes; |
for (k = c; k * k * k < limit; k +=2 ) if (isPrime(k)) ++cubes; |
||
t = count + squares - cubes - 1; |
t = count + squares - cubes - 1; |
||
printf("Counts under %' |
printf("Counts under %'9d: MPNs = %'7d Semi-primes = %'7d\n", limit, count, t); |
||
if (limit == |
if (limit == 5000000) break; |
||
s = j; |
s = j; |
||
c = k; |
c = k; |
||
Line 195: | Line 188: | ||
469 471 473 478 481 482 485 489 493 497 |
469 471 473 478 481 482 485 489 493 497 |
||
Counts under 500: MPNs = 150 Semi-primes = 153 |
Counts under 500: MPNs = 150 Semi-primes = 153 |
||
Counts under 5,000: MPNs = 1,354 Semi-primes = 1,365 |
Counts under 5,000: MPNs = 1,354 Semi-primes = 1,365 |
||
Counts under 50,000: MPNs = 12,074 Semi-primes = 12,110 |
Counts under 50,000: MPNs = 12,074 Semi-primes = 12,110 |
||
Counts under 500,000: MPNs = 108,223 Semi-primes = 108,326 |
Counts under 500,000: MPNs = 108,223 Semi-primes = 108,326 |
||
Counts under 5,000,000: MPNs = 978,983 Semi-primes = 979,274 |
|||
</pre> |
</pre> |
||