Tau function: Difference between revisions

Added Asymptote
(Tau function in various dialects BASIC (Gambas, Run BASIC and XBasic))
(Added Asymptote)
 
(8 intermediate revisions by 6 users not shown)
Line 490:
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>
 
=={{header|Asymptote}}==
<syntaxhighlight lang="Asymptote">write("The tau functions for the first 100 positive integers are:");
for (int N = 1; N <= 100; ++N) {
int T;
if (N < 3) {
T = N;
} else {
T = 2;
for (int A = 2; A <= (N + 1) / 2; ++A) {
if (N % A == 0) T = T + 1;
}
}
write(format("%3d", T), suffix=none);
if (N % 10 == 0) write("");
}</syntaxhighlight>
 
=={{header|AutoHotkey}}==
Line 624 ⟶ 640:
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|QBasic}}===
Line 1,152 ⟶ 1,176:
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>
 
=={{header|Dart}}==
{{trans|C++}}
<syntaxhighlight lang="dart">int divisorCount(int n) {
int total = 1;
// Deal with powers of 2 first
for (; (n & 1) == 0; n >>= 1) total++;
// Odd prime factors up to the square root
for (int p = 3; p * p <= n; p += 2) {
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;
}
 
void main() {
const int limit = 100;
print("Count of divisors for the first $limit positive integers:");
for (int n = 1; n <= limit; ++n) {
print(divisorCount(n).toString().padLeft(3));
}
}</syntaxhighlight>
 
=={{header|Delphi}}==
Line 1,280 ⟶ 1,329:
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>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func cntdiv n .
i = 1
while i <= sqrt n
if n mod i = 0
cnt += 1
if i <> n div i
cnt += 1
.
.
i += 1
.
return cnt
.
for i to 100
write cntdiv i & " "
.
</syntaxhighlight>
 
=={{header|EMal}}==
Line 1,549 ⟶ 1,618:
{{out}}
<pre>[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>
 
 
Or using primeFactors from the Data.Numbers.Primes library:
 
<syntaxhighlight lang="haskell">import Data.Numbers.Primes
import Data.List (group, intercalate, transpose)
import Data.List.Split (chunksOf)
import Text.Printf
 
----------------------- OEISA000005 ----------------------
 
oeisA000005 :: [Int]
oeisA000005 = tau <$> [1..]
 
tau :: Integer -> Int
tau = product . fmap (succ . length) . group . primeFactors
 
 
--------------------------- TEST -------------------------
 
main :: IO ()
main = putStrLn $
(table " " . chunksOf 10 . fmap show . take 100)
oeisA000005
 
 
------------------------ FORMATTING ----------------------
 
table :: String -> [[String]] -> String
table gap rows =
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</syntaxhighlight>
 
{{Out}}
<pre>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>
 
=={{header|J}}==
Line 1,704 ⟶ 1,818:
{{out}}
<pre>{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>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
tau = function(n)
ans = 0
i = 1
while i * i <= n
if n % i == 0 then
ans += 1
j = floor(n / i)
if j != i then ans += 1
end if
i += 1
end while
return ans
end function
 
taus = []
for n in range(1, 100)
taus.push(tau(n))
end for
 
print taus.join(", ")
</syntaxhighlight>
{{out}}
<pre>
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>
 
=={{header|Modula-2}}==
Line 2,569 ⟶ 2,711:
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>
Line 2,671 ⟶ 2,870:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
System.print("The tau functions for the first 100 positive integers are:")
2,130

edits