Tau function: Difference between revisions

Add Scala implementation
(Added Easylang)
(Add Scala implementation)
Line 2,670:
5 4 2 12 4 4 4 8 2 12
4 6 4 4 4 12 2 6 6 9
</pre>
 
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
object TauFunction {
 
private def divisorCount(n: Long): Long = {
var count = 1L
var number = n
 
// Deal with powers of 2 first
while ((number & 1L) == 0) {
count += 1
number >>= 1
}
 
// Odd prime factors up to the square root
var p = 3L
while (p * p <= number) {
var tempCount = 1L
while (number % p == 0) {
tempCount += 1
number /= p
}
count *= tempCount
p += 2
}
 
// If n > 1 then it's prime
if (number > 1) {
count *= 2
}
 
count
}
 
def main(args: Array[String]): Unit = {
val limit = 100
println(s"Count of divisors for the first $limit positive integers:")
for (n <- 1 to limit) {
print(f"${divisorCount(n)}%3d")
if (n % 20 == 0) println()
}
}
}
</syntaxhighlight>
{{out}}
<pre>
Count of divisors for the first 100 positive integers:
1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
4 4 2 8 3 4 4 6 2 8 2 6 4 4 4 9 2 4 4 8
2 8 2 6 6 4 2 10 3 6 4 6 2 8 4 8 4 4 2 12
2 4 6 7 4 8 2 6 4 8 2 12 2 4 6 6 4 8 2 10
5 4 2 12 4 4 4 8 2 12 4 6 4 4 4 12 2 6 6 9
 
</pre>
338

edits