Product of divisors: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added personal tag) |
|||
Line 743: | Line 743: | ||
41 3111696 43 85184 91125 2116 47 254803968 343 125000 |
41 3111696 43 85184 91125 2116 47 254803968 343 125000 |
||
</pre> |
</pre> |
||
=={{header|Kotlin}}== |
|||
{{trans|Java}} |
|||
<lang scala>import kotlin.math.pow |
|||
private fun divisorCount(n: Long): Long { |
|||
var nn = n |
|||
var total: Long = 1 |
|||
// Deal with powers of 2 first |
|||
while (nn and 1 == 0L) { |
|||
++total |
|||
nn = nn shr 1 |
|||
} |
|||
// Odd prime factors up to the square root |
|||
var p: Long = 3 |
|||
while (p * p <= nn) { |
|||
var count = 1L |
|||
while (nn % p == 0L) { |
|||
++count |
|||
nn /= p |
|||
} |
|||
total *= count |
|||
p += 2 |
|||
} |
|||
// If n > 1 then it's prime |
|||
if (nn > 1) { |
|||
total *= 2 |
|||
} |
|||
return total |
|||
} |
|||
private fun divisorProduct(n: Long): Long { |
|||
return n.toDouble().pow(divisorCount(n) / 2.0).toLong() |
|||
} |
|||
fun main() { |
|||
val limit: Long = 50 |
|||
println("Product of divisors for the first $limit positive integers:") |
|||
for (n in 1..limit) { |
|||
print("%11d".format(divisorProduct(n))) |
|||
if (n % 5 == 0L) { |
|||
println() |
|||
} |
|||
} |
|||
}</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|MAD}}== |
=={{header|MAD}}== |