Product of divisors: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring the hard way) |
|||
Line 207: | Line 207: | ||
41 3111696 43 85184 91125 2116 47 254803968 343 125000 |
41 3111696 43 85184 91125 2116 47 254803968 343 125000 |
||
</pre> |
</pre> |
||
=={{header|C}}== |
|||
{{trans|C++}} |
|||
<lang c>#include <math.h> |
|||
#include <stdio.h> |
|||
// See https://en.wikipedia.org/wiki/Divisor_function |
|||
unsigned int divisor_count(unsigned int n) { |
|||
unsigned int total = 1; |
|||
unsigned int p; |
|||
// Deal with powers of 2 first |
|||
for (; (n & 1) == 0; n >>= 1) { |
|||
++total; |
|||
} |
|||
// Odd prime factors up to the square root |
|||
for (p = 3; p * p <= n; p += 2) { |
|||
unsigned int count = 1; |
|||
for (; n % p == 0; n /= p) { |
|||
++count; |
|||
} |
|||
total *= count; |
|||
} |
|||
// If n > 1 then it's prime |
|||
if (n > 1) { |
|||
total *= 2; |
|||
} |
|||
return total; |
|||
} |
|||
// See https://mathworld.wolfram.com/DivisorProduct.html |
|||
unsigned int divisor_product(unsigned int n) { |
|||
return pow(n, divisor_count(n) / 2.0); |
|||
} |
|||
int main() { |
|||
const unsigned int limit = 50; |
|||
unsigned int n; |
|||
printf("Product of divisors for the first %d positive integers:\n", limit); |
|||
for (n = 1; n <= limit; ++n) { |
|||
printf("%11d", divisor_product(n)); |
|||
if (n % 5 == 0) { |
|||
printf("\n"); |
|||
} |
|||
} |
|||
return 0; |
|||
}</lang> |
|||
{{out}} |
|||
<pre>Product of divisors for the first 50 positive integers: |
|||
1 2 3 8 5 |
|||
36 7 64 27 100 |
|||
11 1728 13 196 225 |
|||
1024 17 5832 19 8000 |
|||
441 484 23 331776 125 |
|||
676 729 21952 29 810000 |
|||
31 32768 1089 1156 1225 |
|||
10077696 37 1444 1521 2560000 |
|||
41 3111696 43 85184 91125 |
|||
2116 47 254803968 343 125000</pre> |
|||
=={{header|C++}}== |
=={{header|C++}}== |