Tau function: Difference between revisions
Added Asymptote
(add task to aarch64 assembly raspberry pi) |
(Added Asymptote) |
||
(26 intermediate revisions by 18 users not shown) | |||
Line 16:
{{trans|Python}}
<
V ans = 0
V i = 1
Line 29:
R ans
print((1..100).map(n -> tau(n)))</
{{out}}
Line 37:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program taufunction64.s */
Line 126:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
<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
Line 134:
=={{header|Action!}}==
<
CARD result,p,count
Line 171:
PrintC(divCount) Put(32)
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Tau_function.png Screenshot from Atari 8-bit computer]
Line 184:
=={{header|ALGOL 68}}==
{{Trans|ALGOL W}}{{Trans|C++}}
<
# calculates the number of divisors of v #
PROC divisor count = ( INT v )INT:
Line 215:
OD
END
END</
{{out}}
<pre>
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}}==
{{Trans|C++}}
<
% calculates the number of divisors of v %
integer procedure divisor_count( integer value v ) ; begin
Line 263 ⟶ 321:
end for_n
end
end.</
{{out}}
<pre>
Line 276 ⟶ 334:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
tau¨ 5 20⍴⍳100</
{{out}}
<pre>1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
Line 286 ⟶ 344:
=={{header|AppleScript}}==
<
if (n < 1) then return 0
set counter to 2
Line 309 ⟶ 367:
set output to output as text
set AppleScript's text item delimiters to astid
return output</
{{output}}
<
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"</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* program taufunction.s */
Line 411 ⟶ 469:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
<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
Line 419 ⟶ 477:
=={{header|Arturo}}==
<
loop split.every:20 1..100 => [
print map & => [pad to :string tau & 3]
]</
{{out}}
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}}==
<syntaxhighlight lang="autohotkey">loop 100
result .= SubStr(" " Tau(A_Index), -3) . (Mod(A_Index, 10) ? " " : "`n")
MsgBox % result
return
Tau(n){
return StrSplit(Factors(n), ",").Count()
}
Factors(n) {
Loop, % floor(sqrt(n))
v := A_Index = 1 ? 1 "," n : mod(n,A_Index) ? v : v "," A_Index "," n//A_Index
Sort, v, N U D,
Return, v
}</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|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f TAU_FUNCTION.AWK
BEGIN {
Line 454 ⟶ 555:
return(count)
}
</syntaxhighlight>
{{out}}
<pre>
Line 472 ⟶ 573:
=={{header|BASIC}}==
{{trans|C}}
<
20 FOR I=1 TO 100
30 N=I: GOSUB 100
Line 489 ⟶ 590:
180 IF P*P<=N GOTO 140
190 IF N>1 THEN T=T*2
200 RETURN</
{{out}}
<pre> 1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
Line 499 ⟶ 600:
==={{header|BASIC256}}===
<
print
Line 514 ⟶ 615:
if N mod 10 = 0 then print
next N
end</
==={{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}}===
{{works with|QBasic}}
<
FOR N = 1 TO 100
Line 532 ⟶ 665:
IF N MOD 10 = 0 THEN PRINT
NEXT N
END</
==={{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}}===
<
PRINT
Line 550 ⟶ 702:
IF REMAINDER (N, 10) = 0 THEN PRINT
NEXT N
END</
==={{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}}===
<
for N = 1 to 100
Line 567 ⟶ 746:
if mod(N, 10) = 0 then print : fi
next N
end</
=={{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}}==
<
let tau(n) = valof
Line 596 ⟶ 785:
$( writed(tau(n), 3)
if n rem 20 = 0 then wrch('*N')
$)</
{{out}}
<pre> 1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
Line 605 ⟶ 794:
=={{header|BQN}}==
<
Tau¨ 5‿20⥊1+↕100</
{{out}}
<pre>┌─
Line 618 ⟶ 807:
=={{header|C}}==
{{trans|C++}}
<
// See https://en.wikipedia.org/wiki/Divisor_function
Line 655 ⟶ 844:
return 0;
}</
{{out}}
<pre>Count of divisors for the first 100 positive integers:
Line 665 ⟶ 854:
=={{header|C++}}==
<
#include <iostream>
Line 695 ⟶ 884:
std::cout << '\n';
}
}</
{{out}}
Line 706 ⟶ 895:
5 4 2 12 4 4 4 8 2 12 4 6 4 4 4 12 2 6 6 9
</pre>
=={{header|Clojure}}==
{{trans|Raku}}
<syntaxhighlight lang="clojure">(require '[clojure.string :refer [join]])
(require '[clojure.pprint :refer [cl-format]])
(defn divisors [n] (filter #(zero? (rem n %)) (range 1 (inc n))))
(defn display-results [label per-line width nums]
(doall (map println (cons (str "\n" label ":") (list
(join "\n" (map #(join " " %)
(partition-all per-line (map #(cl-format nil "~v:d" width %) nums)))))))))
(display-results "Tau function - first 100" 20 3
(take 100 (map (comp count divisors) (drop 1 (range)))))
(display-results "Tau numbers – first 100" 10 5
(take 100 (filter #(zero? (rem % (count (divisors %)))) (drop 1 (range)))))
(display-results "Divisor sums – first 100" 20 4
(take 100 (map #(reduce + (divisors %)) (drop 1 (range)))))
(display-results "Divisor products – first 100" 5 16
(take 100 (map #(reduce * (divisors %)) (drop 1 (range)))))</syntaxhighlight>
{{Out}}
<pre>
Tau function - first 100:
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
Tau numbers – first 100:
1 2 8 9 12 18 24 36 40 56
60 72 80 84 88 96 104 108 128 132
136 152 156 180 184 204 225 228 232 240
248 252 276 288 296 328 344 348 360 372
376 384 396 424 441 444 448 450 468 472
480 488 492 504 516 536 560 564 568 584
600 612 625 632 636 640 664 672 684 708
712 720 732 776 792 804 808 824 828 852
856 864 872 876 880 882 896 904 936 948
972 996 1,016 1,040 1,044 1,048 1,056 1,068 1,089 1,096
Divisor sums – first 100:
1 3 4 7 6 12 8 15 13 18 12 28 14 24 24 31 18 39 20 42
32 36 24 60 31 42 40 56 30 72 32 63 48 54 48 91 38 60 56 90
42 96 44 84 78 72 48 124 57 93 72 98 54 120 72 120 80 90 60 168
62 96 104 127 84 144 68 126 96 144 72 195 74 114 124 140 96 168 80 186
121 126 84 224 108 132 120 180 90 234 112 168 128 144 120 252 98 171 156 217
Divisor products – first 100:
1 2 3 8 5
36 7 64 27 100
11 1,728 13 196 225
1,024 17 5,832 19 8,000
441 484 23 331,776 125
676 729 21,952 29 810,000
31 32,768 1,089 1,156 1,225
10,077,696 37 1,444 1,521 2,560,000
41 3,111,696 43 85,184 91,125
2,116 47 254,803,968 343 125,000
2,601 140,608 53 8,503,056 3,025
9,834,496 3,249 3,364 59 46,656,000,000
61 3,844 250,047 2,097,152 4,225
18,974,736 67 314,432 4,761 24,010,000
71 139,314,069,504 73 5,476 421,875
438,976 5,929 37,015,056 79 3,276,800,000
59,049 6,724 83 351,298,031,616 7,225
7,396 7,569 59,969,536 89 531,441,000,000
8,281 778,688 8,649 8,836 9,025
782,757,789,696 97 941,192 970,299 1,000,000,000
</pre>
=={{header|CLU}}==
{{trans|C}}
<
total: int := 1
while n//2 = 0 do
Line 737 ⟶ 1,003:
if n//20=0 then stream$putl(po, "") end
end
end start_up</
{{out}}
<pre> 1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
Line 747 ⟶ 1,013:
=={{header|COBOL}}==
{{trans|C}}
<
PROGRAM-ID. TAU-FUNCTION.
Line 811 ⟶ 1,077:
ADD 1 TO F-COUNT,
GO TO ODD-FACTOR-LOOP.
MULTIPLY F-COUNT BY TOTAL.</
{{out}}
<pre> 1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
Line 821 ⟶ 1,087:
=={{header|Cowgol}}==
{{trans|C}}
<
typedef N is uint8;
Line 859 ⟶ 1,125:
if n % 20 == 0 then print_nl(); end if;
n := n + 1;
end loop;</
{{out}}
<pre> 1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
Line 869 ⟶ 1,135:
=={{header|D}}==
{{trans|C++}}
<
// See https://en.wikipedia.org/wiki/Divisor_function
Line 902 ⟶ 1,168:
}
}
}</
{{out}}
<pre>Count of divisors for the first 100 positive integers:
Line 910 ⟶ 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}}==
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Tau_function;
Line 952 ⟶ 1,243:
end;
readln;
end.</
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc nonrec tau(word n) word:
word count, total, p;
total := 1;
while n & 1 = 0 do
total := total + 1;
n := n >> 1
od;
p := 3;
while p*p <= n do
count := 1;
while n % p = 0 do
count := count + 1;
n := n / p
od;
total := total * count;
p := p + 2
od;
if n>1
then total << 1
else total
fi
corp
proc nonrec main() void:
byte n;
for n from 1 upto 100 do
write(tau(n):3);
if n%20=0 then writeln() fi
od
corp</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|Dyalect}}==
Line 958 ⟶ 1,287:
{{trans|Swift}}
<
var n = number
var total = 1
Line 988 ⟶ 1,317:
print("Count of divisors for the first \(limit) positive integers:")
for n in 1..limit {
print(divisorCount(number: n).
print() when n % 20 == 0
}</
{{out}}
Line 1,000 ⟶ 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#}}==
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br>
<
// Tau function. Nigel Galloway: March 10th., 2021
let tau u=let P=primes32()
Line 1,010 ⟶ 1,391:
let n=Seq.head P in fG 1 n 1 1 n
[1..100]|>Seq.iter(tau>>printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
Line 1,018 ⟶ 1,399:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
<
math.ranges sequences sequences.extras ;
Line 1,034 ⟶ 1,415:
[ "%2d |" printf ]
[ dup 10 + [a,b) [ tau "%4d" printf ] each nl ] bi
] each</
{{out}}
<pre>
Line 1,054 ⟶ 1,435:
=={{header|Fermat}}==
<syntaxhighlight lang="text">Func Tau(t) =
if t<3 then Return(t) else
numdiv:=2;
Line 1,066 ⟶ 1,447:
for i = 1 to 100 do
!(Tau(i),' ');
od;</
{{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>
Line 1,072 ⟶ 1,453:
=={{header|Forth}}==
{{trans|C++}}
<
1 >r
begin
Line 1,104 ⟶ 1,485:
100 print_divisor_counts
bye</
{{out}}
Line 1,117 ⟶ 1,498:
=={{header|FreeBASIC}}==
<
dim as uinteger c = 1
for i as uinteger = 1 to (n+1)\2
Line 1,129 ⟶ 1,510:
print numdiv(i),
if i mod 10 = 0 then print
next i</
{{out}}
<pre>1 2 2 3 2 4 2 4 3 4
Line 1,141 ⟶ 1,522:
5 4 2 12 4 4 4 8 2 12
4 6 4 4 4 12 2 6 6 9</pre>
=={{header|Frink}}==
<syntaxhighlight lang="frink">tau[n] := length[allFactors[n]]
for n=1 to 100
print[tau[n] + " "]
println[]</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|Go}}==
<
import "fmt"
Line 1,175 ⟶ 1,567:
}
}
}</
{{out}}
Line 1,188 ⟶ 1,580:
=={{header|GW-BASIC}}==
<
20 IF N < 3 THEN T=N: GOTO 70
30 T=2
Line 1,196 ⟶ 1,588:
70 PRINT T;
80 IF N MOD 10 = 0 THEN PRINT
90 NEXT N</
{{out}}
<pre>
Line 1,212 ⟶ 1,604:
=={{header|Haskell}}==
<
tau n | n <= 0 = error "Not a positive integer"
tau n = go 0 (1, 1)
Line 1,223 ⟶ 1,615:
| otherwise = go r (yo $ i + 1)
main = print $ map tau [1..100]</
{{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}}==
<
echo tau"0 [5 20$>:i.100
exit ''</
{{out}}
<pre>1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
Line 1,240 ⟶ 1,677:
=={{header|Java}}==
{{trans|D}}
<
private static long divisorCount(long n) {
long total = 1;
Line 1,272 ⟶ 1,709:
}
}
}</
{{out}}
<pre>Count of divisors for the first 100 positive integers:
Line 1,286 ⟶ 1,723:
'''Preliminaries'''
See https://rosettacode.org/wiki/Sum_of_divisors#jq for the definition of `divisors` used here.<
# For pretty-printing
Line 1,293 ⟶ 1,730:
n;
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;</
'''The task'''
<
| nwise(10) | map(lpad(4)) | join("")</
{{out}}
<pre>
Line 1,313 ⟶ 1,750:
=={{header|Julia}}==
Recycles code from http://www.rosettacode.org/wiki/Sequence:_smallest_number_greater_than_previous_term_with_exactly_n_divisors#Julia
<
function numfactors(n)
Line 1,326 ⟶ 1,763:
print(rpad(numfactors(i), 3), i % 25 == 0 ? " \n" : " ")
end
</
<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
Line 1,336 ⟶ 1,773:
=={{header|Lua}}==
{{trans|Java}}
<
local total = 1
-- Deal with powers of 2 first
Line 1,368 ⟶ 1,805:
print()
end
end</
{{out}}
<pre>Count of divisors for the first 100 positive integers:
Line 1,378 ⟶ 1,815:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
{{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}}==
{{trans|C}}
<
FROM InOut IMPORT WriteCard, WriteLn;
Line 1,416 ⟶ 1,881:
IF i MOD 20 = 0 THEN WriteLn END
END
END TauFunc.</
{{out}}
<pre> 1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
Line 1,425 ⟶ 1,890:
=={{header|Nim}}==
<
func divcount(n: Natural): Natural =
Line 1,436 ⟶ 1,901:
for i in 1..100:
stdout.write ($divcount(i)).align(3)
if i mod 20 == 0: echo()</
{{out}}
Line 1,447 ⟶ 1,912:
=={{header|PARI/GP}}==
<
{{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>
Line 1,453 ⟶ 1,918:
=={{header|Pascal}}==
{{works with|Extended Pascal}}
<
type
Line 1,495 ⟶ 1,960:
writeLn(f:8, ') = ', tau(i):5)
end
end.</
==={{header|Free Pascal}}===
{{works with|Free Pascal}} and on TIO.RUN. Only test 0..99 for a nicer table.
<
{$IFDEF Windows} {$APPTYPE CONSOLE} {$ENDIF}
function CountDivisors(n: NativeUint): integer;
Line 1,553 ⟶ 2,018:
writeln;
{$Ifdef Windows}readln;{$ENDIF}
end.</
{{out|TIO.RUN}}
<pre>
Line 1,571 ⟶ 2,036:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 1,580 ⟶ 2,045:
say "Tau function - first 100:\n" .
((sprintf "@{['%4d' x 100]}", @x[0..100-1]) =~ s/(.{80})/$1\n/gr);</
{{out}}
<pre> 1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
Line 1,590 ⟶ 2,055:
=={{header|Phix}}==
=== imperative ===
<!--<
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">100</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%3d"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 1,606 ⟶ 2,071:
=== functional ===
same output
<!--<
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%3d"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">r</span><span style="color: #0000FF;">}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">))</span>
<!--</
=={{header|PL/I}}==
{{trans|C}}
<
tau: procedure(nn) returns(fixed);
declare (n, nn, tot, pf, cnt) fixed;
Line 1,635 ⟶ 2,100:
if mod(n,20)=0 then put skip;
end;
end taufunc;</
{{out}}
<pre> 1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
Line 1,646 ⟶ 2,111:
=={{header|PL/M}}==
{{trans|C}}
<
/* CP/M BDOS FUNCTIONS */
Line 1,695 ⟶ 2,160:
END;
CALL EXIT;
EOF</
{{out}}
<pre> 1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
Line 1,704 ⟶ 2,169:
=={{header|PureBasic}}==
<
For i=1 To 100
If i<3 : Print(RSet(Str(i),4)) : Continue :EndIf
Line 1,714 ⟶ 2,179:
Input()
EndIf
End</
{{out}}
<pre> 1 2 2 3 2 4 2 4 3 4
Line 1,729 ⟶ 2,194:
=={{header|Python}}==
===Using prime factorization===
<
assert(isinstance(n, int))
if n < 0:
Line 1,761 ⟶ 2,226:
if __name__ == "__main__":
print(
===Finding divisors efficiently===
<
assert(isinstance(n, int) and 0 < n)
p
while p * p
while n % p
if n != 1:
t += t
return t
if __name__ == "__main__":
print(
{{out}}
<pre>
===Choosing the right abstraction===
Yet another exercise in defining a '''divisors''' function.
<
from itertools import count, islice
Line 1,869 ⟶ 2,337:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre> 1 2 2 3 2 4 2 4 3 4
Line 1,887 ⟶ 2,355:
<
[] []
Line 1,893 ⟶ 2,361:
witheach [ number$ nested join ]
70 wrap$
</syntaxhighlight>
{{out}}
Line 1,904 ⟶ 2,372:
=={{header|R}}==
This only takes one line.
<
=={{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}}==
Yet more tasks that are tiny variations of each other. [[Tau function]], [[Tau number]], [[Sum of divisors]] and [[Product of divisors]] all use code with minimal changes. What the heck, post 'em all.
<syntaxhighlight lang="raku"
use Lingua::EN::Numbers;
Line 1,926 ⟶ 2,424:
say "\nDivisor products - first 100:\n", # ID
(1..*).map({ [×] .&divisors })[^100]\ # the task
.batch(5)».&comma».fmt("%16s").join("\n"); # display formatting</
{{out}}
<pre>Tau function - first 100:
Line 1,977 ⟶ 2,475:
=={{header|REXX}}==
<
parse arg LO HI cols . /*obtain optional argument from the CL.*/
if LO=='' | LO=="," then LO= 1 /*Not specified? Then use the default.*/
Line 2,008 ⟶ 2,506:
end /* ___ */
else if j*j>x then leave /*only divide up to √ x */
end /*j*/; return # /* [↑] this form of DO loop is faster.*/</
{{out|output|text= when using the default input:}}
<pre>
Line 2,022 ⟶ 2,520:
=={{header|Ring}}==
<
see "The tau functions for the first 100 positive integers are:" + nl
Line 2,046 ⟶ 2,544:
see "" + tau + " "
end
</syntaxhighlight>
Output:
<pre>
Line 2,062 ⟶ 2,560:
4 6 4 4 4 12 2 6 6 9
</pre>
=={{header|RPL}}==
{{trans|Python}}
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL Code
! Python code
|-
|
≪ → n
≪ 0 1
1 n √ '''FOR''' ii
'''IF''' n ii MOD NOT '''THEN'''
SWAP 1 + SWAP
DROP n ii / IP
'''IF''' DUP ii ≠
'''THEN''' SWAP 1 + SWAP '''END END'''
'''NEXT'''
DROP
≫ ≫ ‘TAU’ STO
|
''(n -- tau(n) )''
ans, j = 0, 1
while i*i <= n:
if 0 == n%i:
ans += 1
j = n//i
if j != i:
ans += 1
i += 1
return ans
.
|}
The following line of command delivers what is required:
≪ {} 1 100 '''FOR''' j j TAU + '''NEXT''' ≫ EVAL
{{out}}
<pre>
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}}==
<
def tau(n) = n.prime_division.inject(1){|res, (d, exp)| res *= exp + 1}
(1..100).map{|n| tau(n).to_s.rjust(3) }.each_slice(20){|ar| puts ar.join}
</syntaxhighlight>
{{out}}
<pre> 1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
Line 2,079 ⟶ 2,636:
=={{header|Rust}}==
<
// and n divided by that power of i
fn factor_exponent(n: i32, i: i32) -> (i32, i32) {
Line 2,104 ⟶ 2,661:
print!("{} ", tau(i));
}
}</
Output:
<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|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}}==
Built-in:
<
{{out}}
Line 2,120 ⟶ 2,781:
=={{header|Swift}}==
<
// See https://en.wikipedia.org/wiki/Divisor_function
Line 2,156 ⟶ 2,817:
print()
}
}</
{{out}}
Line 2,169 ⟶ 2,830:
=={{header|Tiny BASIC}}==
<
10 LET N = N + 1
IF N < 3 THEN GOTO 100
Line 2,181 ⟶ 2,842:
END
100 LET T = N
GOTO 30</
=={{header|Verilog}}==
<
integer N, T, A;
Line 2,204 ⟶ 2,865:
$finish ;
end
endmodule</
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
System.print("The tau functions for the first 100 positive integers are:")
Line 2,216 ⟶ 2,877:
Fmt.write("$2d ", Int.divisors(i).count)
if (i % 20 == 0) System.print()
}</
{{out}}
Line 2,226 ⟶ 2,887:
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|XPL0}}==
<syntaxhighlight lang="xpl0">int N, D, C;
[Format(3, 0);
for N:= 1 to 100 do
[C:= 0;
for D:= 1 to N do
if rem(N/D) = 0 then C:= C+1;
RlOut(0, float(C));
if rem(N/20) = 0 then CrLf(0);
];
]</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>
|