Tau function: Difference between revisions

11,684 bytes added ,  2 months ago
Added Asymptote
(→‎Finding divisors efficiently: speed-up three times)
(Added Asymptote)
 
(17 intermediate revisions by 11 users not shown)
Line 225:
5 4 2 12 4 4 4 8 2 12 4 6 4 4 4 12 2 6 6 9
</pre>
 
=={{header|ALGOL-M}}==
<syntaxhighlight lang="ALGOL">
begin
 
% return the value of n mod m %
integer function mod(n, m);
integer n, m;
begin
mod := n - m * (n / m);
end;
 
% return the tau value (i.e, number of divisors) of n %
integer function tau(n);
integer n;
begin
integer i, t, limit;
if n < 3 then
t := n
else
begin
t := 2;
limit := (n + 1) / 2;
for i := 2 step 1 until limit do
begin
if mod(n, i) = 0 then t := t + 1;
end;
end;
tau := t;
end;
 
% test by printing the tau value of the first 100 numbers %
integer i;
write("Count of divisors for first 100 numbers:");
write("");
for i := 1 step 1 until 100 do
begin
writeon(tau(i));
if mod(i,10) = 0 then write(""); % print 10 across %
end;
 
end
</syntaxhighlight>
{{out}}
<pre>
Count of divisors for first 100 numbers:
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|ALGOL W}}==
Line 432 ⟶ 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 542 ⟶ 616:
next N
end</syntaxhighlight>
 
==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">Public Sub Main()
Print "The tau functions for the first 100 positive integers are:\n"
For i As Integer = 1 To 100
Print Format$(numdiv(i), "####");
If i Mod 10 = 0 Then Print
Next
End
 
Public Function numdiv(n As Integer) As Integer
 
Dim c As Integer = 1
For i As Integer = 1 To (n + 1) \ 2
If n Mod i = 0 Then c += 1
Next
If n = 1 Then c -= 1
Return c
 
End Function</syntaxhighlight>
{{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 560 ⟶ 666:
NEXT N
END</syntaxhighlight>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<syntaxhighlight lang="vb">print "The tau functions for the first 100 positive integers are:"
print
for N = 1 to 100
if N < 3 then
T = N
else
T = 2
for A = 2 to int((N+1)/2)
if N mod A = 0 then T = T + 1
next A
end if
print using("####", T);
if N mod 10 = 0 then print
next N
end</syntaxhighlight>
 
==={{header|True BASIC}}===
Line 578 ⟶ 703:
NEXT N
END</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Tau"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
DECLARE FUNCTION numdiv(n)
 
FUNCTION Entry ()
PRINT "The tau functions for the first 100 positive integers are:\n"
FOR i = 1 TO 100
PRINT FORMAT$("###", numdiv(i));
IF i MOD 10 = 0 THEN PRINT
NEXT i
END FUNCTION
 
FUNCTION numdiv(n)
c = 1
FOR i = 1 TO (n+1)\2
IF n MOD i = 0 THEN INC c
NEXT i
IF n = 1 THEN DEC c
RETURN c
 
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
Line 596 ⟶ 748:
end</syntaxhighlight>
 
=={{header|bc}}==
<syntaxhighlight lang="bc">define t(n) {
auto a, d, p
for (d = 1; n % 2 == 0; n /= 2) d += 1
for (p = 3; p * p <= n; p += 2) for (a = d; n % p == 0; n /= p) d += a
if (n != 1) d += d
return(d)
}
 
for (i = 1; i <= 100; ++i) t(i)</syntaxhighlight>
 
=={{header|BCPL}}==
Line 1,014 ⟶ 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,142 ⟶ 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}}==
{{trans|Java}}
<syntaxhighlight lang="emal">
fun divisorCount = int by int n
int total = 1
for ; (n & 1) == 0; n /= 2 do ++total end
for int p = 3; p * p <= n; p += 2
int count = 1
for ; n % p == 0; n /= p do ++count end
total *= count
end
if n > 1 do total *= 2 end
return total
end
int limit = 100
writeLine("Count of divisors for the first " + limit + " positive integers:")
for int n = 1; n <= limit; ++n
text value = text!divisorCount(n)
write((" " * (3 - value.length)) + value)
if n % 20 == 0 do writeLine() end
end
</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>
 
=={{header|F_Sharp|F#}}==
Line 1,379 ⟶ 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,534 ⟶ 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 1,925 ⟶ 2,237:
while n % p == 0:
t += a
n //= p
p += 2
if n != 1:
Line 2,061 ⟶ 2,373:
This only takes one line.
<syntaxhighlight lang="rsplus">lengths(sapply(1:100, function(n) c(Filter(function(x) n %% x == 0, seq_len(n %/% 2)), n)))</syntaxhighlight>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
 
(define limit 100)
 
(define (divisor-count n)
(length (filter (λ (x) (zero? (remainder n x))) (range 1 (add1 n)))))
 
(printf "Count of divisors of the integers from 1 to ~a are~n" limit)
(for ([n (in-range 1 (add1 limit))])
(printf (~a (divisor-count n) #:width 5 #:align 'right))
(when (zero? (remainder n 10))
(newline)))
</syntaxhighlight>
{{out}}
<pre>
Count of divisors of the integers from 1 to 100 are
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|Raku}}==
Line 2,257 ⟶ 2,599:
1: { 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>
===Optimized algorithm===
{| class="wikitable"
! RPL code
! Comment
|-
|
DUP √ DUP FP 0 -1 '''IFTE'''
1 ROT '''FOR''' j
OVER j MOD NOT DUP + +
'''NEXT''' SWAP DROP
≫ ‘TAU’ STO
|
''( n -- tau(n) )''
counter set at -1 if n square, 0 otherwise
while j ≤ n²
add 2 to counter for each dividing j
forget n
|}
 
=={{header|Ruby}}==
Line 2,304 ⟶ 2,666:
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|S-BASIC}}==
<syntaxhighlight lang="BASIC">
rem - return the value of n mod m
function mod(n, m = integer) = integer
end = n - m * (n / m)
 
rem - return the tau value (number of divisors) of n
function tau(n = integer) = integer
var i, t, limit = integer
if n < 3 then
t = n
else
begin
t = 2
limit = (n + 1) / 2
for i = 2 to limit
if mod(n, i) = 0 then t = t + 1
next i
end
end = t
 
rem - test by printing the tau value of the first 100 numbers
var i = integer
print "Number of divisors for first 100 numbers:"
for i = 1 to 100
print using "## "; tau(i);
if mod(i, 10) = 0 then print
next i
 
end
</syntaxhighlight>
{{out}}
<pre>
Number of divisors for first 100 numbers:
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|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>
 
=={{header|Sidef}}==
Line 2,404 ⟶ 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