I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

Tau function

From Rosetta Code
Task
Tau function
You are encouraged to solve this task according to the task description, using any language you may know.

Given a positive integer, count the number of its positive divisors.


Task

Show the result for the first   100   positive integers.


Related task



11l[edit]

Translation of: Python
F tau(n)
V ans = 0
V i = 1
V j = 1
L i * i <= n
I 0 == n % i
ans++
j = n I/ i
I j != i
ans++
i++
R ans
 
print((1..100).map(n -> tau(n)))
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]

ALGOL W[edit]

Translation of: C++
begin % find the count of the divisors of the first 100 positive integers   %
 % calculates the number of divisors of v  %
integer procedure divisor_count( integer value v ) ; begin
integer total, n, p;
total := 1; n := v;
 % Deal with powers of 2 first %
while not odd( n ) do begin
total := total + 1;
n  := n div 2
end while_not_odd_n ;
 % Odd prime factors up to the square root %
p := 3;
while ( p * p ) <= n do begin
integer count;
count := 1;
while n rem p = 0 do begin
count := count + 1;
n  := n div p
end while_n_rem_p_eq_0 ;
p  := p + 2;
total := total * count
end while_p_x_p_le_n ;
 % If n > 1 then it's prime %
if n > 1 then total := total * 2;
total
end divisor_count ;
begin
integer limit;
limit := 100;
write( i_w := 1, s_w := 0, "Count of divisors for the first ", limit, " positive integers:" );
for n := 1 until limit do begin
if n rem 20 = 1 then write();
writeon( i_w := 3, s_w := 1, divisor_count( n ) )
end for_n
end
end.
Output:
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

AppleScript[edit]

on factorCount(n)
if (n < 1) then return 0
set counter to 2
set sqrt to n ^ 0.5
if (sqrt mod 1 = 0) then set counter to 1
repeat with i from (sqrt div 1) to 2 by -1
if (n mod i = 0) then set counter to counter + 2
end repeat
 
return counter
end factorCount
 
-- Task code:
local output, n, astid
set output to {"Positive divisor counts for integers 1 to 100:"}
repeat with n from 1 to 100
if (n mod 20 = 1) then set end of output to linefeed
set end of output to text -3 thru -1 of (" " & factorCount(n))
end repeat
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set output to output as text
set AppleScript's text item delimiters to astid
return output
Output:
"Positive divisor counts for integers 1 to 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"

Arturo[edit]

tau: function [x] -> size factors x
 
loop split.every:20 1..100 => [
print map & => [pad to :string tau & 3]
]
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

AWK[edit]

 
# syntax: GAWK -f TAU_FUNCTION.AWK
BEGIN {
print("The tau functions for the first 100 positive integers:")
for (i=1; i<=100; i++) {
printf("%2d ",count_divisors(i))
if (i % 10 == 0) {
printf("\n")
}
}
exit(0)
}
function count_divisors(n, count,i) {
for (i=1; i*i<=n; i++) {
if (n % i == 0) {
count += (i == n / i) ? 1 : 2
}
}
return(count)
}
 
Output:
The tau functions 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

C[edit]

Translation of: C++
#include <stdio.h>
 
// See https://en.wikipedia.org/wiki/Divisor_function
unsigned int divisor_count(unsigned int n) {
unsigned 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 (unsigned int p = 3; p * p <= n; p += 2) {
unsigned 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;
}
 
int main() {
const unsigned int limit = 100;
unsigned int n;
 
printf("Count of divisors for the first %d positive integers:\n", limit);
for (n = 1; n <= limit; ++n) {
printf("%3d", divisor_count(n));
if (n % 20 == 0) {
printf("\n");
}
}
 
return 0;
}
Output:
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

C++[edit]

#include <iomanip>
#include <iostream>
 
// See https://en.wikipedia.org/wiki/Divisor_function
unsigned int divisor_count(unsigned int n) {
unsigned 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 (unsigned int p = 3; p * p <= n; p += 2) {
unsigned 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;
}
 
int main() {
const unsigned int limit = 100;
std::cout << "Count of divisors for the first " << limit << " positive integers:\n";
for (unsigned int n = 1; n <= limit; ++n) {
std::cout << std::setw(3) << divisor_count(n);
if (n % 20 == 0)
std::cout << '\n';
}
}
Output:
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

D[edit]

Translation of: C++
import std.stdio;
 
// See https://en.wikipedia.org/wiki/Divisor_function
uint divisor_count(uint n) {
uint total = 1;
// Deal with powers of 2 first
for (; (n & 1) == 0; n >>= 1) {
++total;
}
// Odd prime factors up to the square root
for (uint p = 3; p * p <= n; p += 2) {
uint 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() {
immutable limit = 100;
writeln("Count of divisors for the first ", limit, " positive integers:");
for (uint n = 1; n <= limit; ++n) {
writef("%3d", divisor_count(n));
if (n % 20 == 0) {
writeln;
}
}
}
Output:
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

Delphi[edit]

Translation of: Go
 
program Tau_function;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils;
 
function CountDivisors(n: Integer): Integer;
begin
Result := 0;
var i := 1;
var k := 2;
if (n mod 2) = 0 then
k := 1;
 
while i * i <= n do
begin
if (n mod i) = 0 then
begin
inc(Result);
var j := n div i;
if j <> i then
inc(Result);
end;
inc(i, k);
end;
end;
 
begin
writeln('The tau functions for the first 100 positive integers are:');
for var i := 1 to 100 do
begin
write(CountDivisors(i): 2, ' ');
if (i mod 20) = 0 then
writeln;
end;
readln;
end.

Dyalect[edit]

Translation of: Swift
func divisorCount(number) {
var n = number
var total = 1
 
while (n &&& 1) == 0 {
total += 1
n >>>= 1
}
 
var p = 3
while p * p <= n {
var count = 1
while n % p == 0 {
count += 1
n /= p
}
total *= count
p += 2
}
 
if n > 1 {
total *= 2
}
 
total
}
 
let limit = 100
print("Count of divisors for the first \(limit) positive integers:")
for n in 1..limit {
print(divisorCount(number: n).toString().padLeft(2, ' ') + " ", terminator: "")
print() when n % 20 == 0
}
Output:
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 

F#[edit]

This task uses Extensible Prime Generator (F#).

 
// Tau function. Nigel Galloway: March 10th., 2021
let tau u=let P=primes32()
let rec fN g=match u%g with 0->g |_->fN(Seq.head P)
let rec fG n i g e l=match n=u,u%l with (true,_)->e |(_,0)->fG (n*i) i g (e+g)(l*i) |_->let q=fN(Seq.head P) in fG (n*q) q e (e+e) (q*q)
let n=Seq.head P in fG 1 n 1 1 n
[1..100]|>Seq.iter(tau>>printf "%d "); printfn ""
 
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

Factor[edit]

Works with: Factor version 0.99 2020-08-14
USING: assocs formatting io kernel math math.primes.factors
math.ranges sequences sequences.extras ;
 
ERROR: nonpositive n ;
 
: (tau) ( n -- count )
group-factors values [ 1 + ] map-product ; inline
 
: tau ( n -- count ) dup 0 > [ (tau) ] [ nonpositive ] if ;
 
"Number of divisors for integers 1-100:" print nl
" n | tau(n)" print
"-----+-----------------------------------------" print
1 100 10 <range> [
[ "%2d |" printf ]
[ dup 10 + [a,b) [ tau "%4d" printf ] each nl ] bi
] each
Output:
Number of divisors for integers 1-100:

 n   |                   tau(n)
-----+-----------------------------------------
 1   |   1   2   2   3   2   4   2   4   3   4
11   |   2   6   2   4   4   5   2   6   2   6
21   |   4   4   2   8   3   4   4   6   2   8
31   |   2   6   4   4   4   9   2   4   4   8
41   |   2   8   2   6   6   4   2  10   3   6
51   |   4   6   2   8   4   8   4   4   2  12
61   |   2   4   6   7   4   8   2   6   4   8
71   |   2  12   2   4   6   6   4   8   2  10
81   |   5   4   2  12   4   4   4   8   2  12
91   |   4   6   4   4   4  12   2   6   6   9

Forth[edit]

Translation of: C++
: divisor_count ( n -- n )
1 >r
begin
dup 2 mod 0=
while
r> 1+ >r
2/
repeat
3
begin
2dup dup * >=
while
1 >r
begin
2dup mod 0=
while
r> 1+ >r
tuck / swap
repeat
2r> * >r
2 +
repeat
drop 1 > if r> 2* else r> then ;
 
: print_divisor_counts ( n -- )
." Count of divisors for the first " dup . ." positive integers:" cr
1+ 1 do
i divisor_count 2 .r
i 20 mod 0= if cr else space then
loop ;
 
100 print_divisor_counts
bye
Output:
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

FreeBASIC[edit]

function numdiv( n as uinteger ) as uinteger
dim as uinteger c = 1
for i as uinteger = 1 to (n+1)\2
if n mod i = 0 then c += 1
next i
if n=1 then c-=1
return c
end function
 
for i as uinteger = 1 to 100
print numdiv(i),
if i mod 10 = 0 then print
next i
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

Go[edit]

package main
 
import "fmt"
 
func countDivisors(n int) int {
count := 0
i := 1
k := 2
if n%2 == 0 {
k = 1
}
for i*i <= n {
if n%i == 0 {
count++
j := n / i
if j != i {
count++
}
}
i += k
}
return count
}
 
func main() {
fmt.Println("The tau functions for the first 100 positive integers are:")
for i := 1; i <= 100; i++ {
fmt.Printf("%2d ", countDivisors(i))
if i%20 == 0 {
fmt.Println()
}
}
}
Output:
The tau functions for the first 100 positive integers 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  

GW-BASIC[edit]

10 FOR N = 1 TO 100
20 IF N < 3 THEN T=N: GOTO 70
30 T=2
40 FOR A = 2 TO INT( (N+1)/2 )
50 IF N MOD A = 0 THEN T = T + 1
60 NEXT A
70 PRINT T;
80 IF N MOD 10 = 0 THEN PRINT
90 NEXT N
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

Haskell[edit]

tau :: Integral a => a -> a
tau n | n <= 0 = error "Not a positive integer"
tau n = go 0 (1, 1)
where
yo i = (i, i * i)
go r (i, ii)
| n < ii = r
| n == ii = r + 1
| 0 == mod n i = go (r + 2) (yo $ i + 1)
| otherwise = go r (yo $ i + 1)
 
main = print $ map tau [1..100]
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]

Java[edit]

Translation of: D
public class TauFunction {
private static long divisorCount(long n) {
long total = 1;
// Deal with powers of 2 first
for (; (n & 1) == 0; n >>= 1) {
++total;
}
// Odd prime factors up to the square root
for (long p = 3; p * p <= n; p += 2) {
long 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;
}
 
public static void main(String[] args) {
final int limit = 100;
System.out.printf("Count of divisors for the first %d positive integers:\n", limit);
for (long n = 1; n <= limit; ++n) {
System.out.printf("%3d", divisorCount(n));
if (n % 20 == 0) {
System.out.println();
}
}
}
}
Output:
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

Julia[edit]

Recycles code from http://www.rosettacode.org/wiki/Sequence:_smallest_number_greater_than_previous_term_with_exactly_n_divisors#Julia

using Primes
 
function numfactors(n)
f = [one(n)]
for (p, e) in factor(n)
f = reduce(vcat, [f * p^j for j in 1:e], init = f)
end
length(f)
end
 
for i in 1:100
print(rpad(numfactors(i), 3), i % 25 == 0 ? " \n" : " ")
end
 
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 

Lua[edit]

Translation of: Java
function divisorCount(n)
local total = 1
-- Deal with powers of 2 first
while (n & 1) == 0 do
total = total + 1
n = math.floor(n / 2)
end
-- Odd prime factors up tot eh square root
local p = 3
while p * p <= n do
local count = 1
while n % p == 0 do
count = count + 1
n = n / p
end
total = total * count
p = p + 2
end
-- If n > 1 then it's prime
if n > 1 then
total = total * 2
end
return total
end
 
limit = 100
print("Count of divisors for the first " .. limit .. " positive integers:")
for n=1,limit do
io.write(string.format("%3d", divisorCount(n)))
if n % 20 == 0 then
print()
end
end
Output:
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

Mathematica[edit]

DivisorSum[#, 1 &] & /@ Range[100]
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}

Nim[edit]

import math, strutils
 
func divcount(n: Natural): Natural =
for i in 1..sqrt(n.toFloat).int:
if n mod i == 0:
inc result
if n div i != i: inc result
 
echo "Count of divisors for the first 100 positive integers:"
for i in 1..100:
stdout.write ($divcount(i)).align(3)
if i mod 20 == 0: echo()
Output:
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

PARI/GP[edit]

vector(100,X,numdiv(X))
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]

Perl[edit]

Library: ntheory
use strict;
use warnings;
use feature 'say';
use ntheory 'divisors';
 
my @x;
push @x, scalar divisors($_) for 1..100;
 
say "Tau function - first 100:\n" .
((sprintf "@{['%4d' x 100]}", @x[0..100-1]) =~ s/(.{80})/$1\n/gr);
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

Phix[edit]

imperative[edit]

for i=1 to 100 do
    printf(1,"%3d",{length(factors(i,1))})
    if remainder(i,20)=0 then puts(1,"\n") end if
end for
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

functional[edit]

same output

sequence r = apply(apply(true,factors,{tagset(100),{1}}),length)
puts(1,join_by(apply(true,sprintf,{{"%3d"},r}),1,20,""))

PureBasic[edit]

If OpenConsole()
For i=1 To 100
If i<3 : Print(RSet(Str(i),4)) : Continue :EndIf
c=2
For j=2 To i/2+1 : c+Bool(i%j=0) : Next
Print(RSet(Str(c),4))
If i%10=0 : PrintN("") : EndIf
Next
Input()
EndIf
End
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

Python[edit]

Using prime factorization[edit]

def factorize(n):
assert(isinstance(n, int))
if n < 0:
n = -n
if n < 2:
return
k = 0
while 0 == n%2:
k += 1
n //= 2
if 0 < k:
yield (2,k)
p = 3
while p*p <= n:
k = 0
while 0 == n%p:
k += 1
n //= p
if 0 < k:
yield (p,k)
p += 2
if 1 < n:
yield (n,1)
 
def tau(n):
assert(n != 0)
ans = 1
for (p,k) in factorize(n):
ans *= 1 + k
return ans
 
if __name__ == "__main__":
print([tau(n) for n in range(1,101)])

Finding divisors efficiently[edit]

def tau(n):
assert(isinstance(n, int) and 0 < n)
ans, i, j = 0, 1, 1
while i*i <= n:
if 0 == n%i:
ans += 1
j = n//i
if j != i:
ans += 1
i += 1
return ans
 
if __name__ == "__main__":
print([tau(n) for n in range(1,101)])
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]

Choosing the right abstraction[edit]

Yet another exercise in defining a divisors function.

'''The number of divisors of n'''
 
from itertools import count, islice
from math import floor, sqrt
 
 
# oeisA000005 :: [Int]
def oeisA000005():
'''tau(n) (also called d(n) or sigma_0(n)),
the number of divisors of n.
'''

return map(tau, count(1))
 
 
# tau :: Int -> Int
def tau(n):
'''The number of divisors of n.
'''

return len(divisors(n))
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''The first 100 terms of OEIS A000005.
(Shown in rows of 10)
'''

terms = take(100)(
oeisA000005()
)
columnWidth = 1 + len(str(max(terms)))
 
print(
'\n'.join(
''.join(
str(term).rjust(columnWidth)
for term in row
)
for row in chunksOf(10)(terms)
)
)
 
 
# ----------------------- GENERIC ------------------------
 
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
'''A series of lists of length n, subdividing the
contents of xs. Where the length of xs is not evenly
divible, the final list will be shorter than n.
'''

def go(xs):
return [
xs[i:n + i] for i in range(0, len(xs), n)
] if 0 < n else None
return go
 
 
# divisors :: Int -> [Int]
def divisors(n):
'''List of all divisors of n including n itself.
'''

root = floor(sqrt(n))
lows = [x for x in range(1, 1 + root) if 0 == n % x]
return lows + [n // x for x in reversed(lows)][
(1 if n == (root * root) else 0):
]
 
 
# take :: Int -> [a] -> [a]
# take :: Int -> String -> String
def take(n):
'''The prefix of xs of length n,
or xs itself if n > length xs.
'''

def go(xs):
return (
xs[0:n]
if isinstance(xs, (list, tuple))
else list(islice(xs, n))
)
return go
 
 
# MAIN ---
if __name__ == '__main__':
main()
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

Quackery[edit]

factors is defined at Factors of an integer#Quackery.


  [ factors size ] is tau ( n --> n )
 
[] []
100 times [ i^ 1+ tau join ]
witheach [ number$ nested join ]
70 wrap$
 
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

Raku[edit]

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.

use Prime::Factor:ver<0.3.0+>;
use Lingua::EN::Numbers;
 
say "\nTau function - first 100:\n", # ID
(1..*).map({ +.&divisors })[^100]\ # the task
.batch(20)».fmt("%3d").join("\n"); # display formatting
 
say "\nTau numbers - first 100:\n", # ID
(1..*).grep({ $_ %% +.&divisors })[^100]\ # the task
.batch(10)».&comma».fmt("%5s").join("\n"); # display formatting
 
say "\nDivisor sums - first 100:\n", # ID
(1..*).map({ [+] .&divisors })[^100]\ # the task
.batch(20)».fmt("%4d").join("\n"); # display formatting
 
say "\nDivisor products - first 100:\n", # ID
(1..*).map({ [×] .&divisors })[^100]\ # the task
.batch(5)».&comma».fmt("%16s").join("\n"); # display formatting
Output:
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

REXX[edit]

/*REXX program counts the number of divisors (tau,  or sigma_0)  up to and including  N.*/
parse arg LO HI cols . /*obtain optional argument from the CL.*/
if LO=='' | LO=="," then LO= 1 /*Not specified? Then use the default.*/
if HI=='' | HI=="," then HI= LO + 100 - 1 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 20 /* " " " " " " */
w= 2 + (HI>45359) /*W: used to align the output columns.*/
say 'The number of divisors (tau) for integers up to ' n " (inclusive):"; say
say '─index─' center(" tau (number of divisors) ", cols * (w+1) + 1, '─')
$=; c= 0 /*$: the output list, shown ROW/line.*/
do j=LO to HI; c= c + 1 /*list # proper divisors (tau) 1 ──► N */
$= $ right( tau(j), w) /*add a tau number to the output list. */
if c//cols \== 0 then iterate /*Not a multiple of ROW? Don't display.*/
idx= j - cols + 1 /*calculate index value (for this row).*/
say center(idx, 7) $; $= /*display partial list to the terminal.*/
end /*j*/
 
if $\=='' then say center(idx+cols, 7) $ /*there any residuals left to display ?*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
tau: procedure; parse arg x 1 y /*X and $ are both set from the arg.*/
if x<6 then return 2 + (x==4) - (x==1) /*some low #s should be handled special*/
odd= x // 2 /*check if X is odd (remainder of 1).*/
if odd then #= 2 /*Odd? Assume divisor count of 2. */
else do; #= 4; y= x % 2; end /*Even? " " " " 4. */
/* [↑] start with known number of divs*/
do j=3 for x%2-3 by 1+odd while j<y /*for odd number, skip even numbers. */
if x//j==0 then do /*if no remainder, then found a divisor*/
#= # + 2; y= x % j /*bump # of divisors; calculate limit.*/
if j>=y then do; #= # - 1; leave; end /*reached limit?*/
end /* ___ */
else if j*j>x then leave /*only divide up to √ x */
end /*j*/; return # /* [↑] this form of DO loop is faster.*/
output   when using the default input:
The number of divisors  (tau)  for integers up to  100  (inclusive):

─index─ ──────────────────────────── tau (number of divisors) ────────────────────────────
   1       1   2   2   3   2   4   2   4   3   4   2   6   2   4   4   5   2   6   2   6
  21       4   4   2   8   3   4   4   6   2   8   2   6   4   4   4   9   2   4   4   8
  41       2   8   2   6   6   4   2  10   3   6   4   6   2   8   4   8   4   4   2  12
  61       2   4   6   7   4   8   2   6   4   8   2  12   2   4   6   6   4   8   2  10
  81       5   4   2  12   4   4   4   8   2  12   4   6   4   4   4  12   2   6   6   9

Ring[edit]

 
see "The tau functions for the first 100 positive integers are:" + nl
 
n = 0
num = 0
limit = 100
while num < limit
n = n + 1
tau = 0
for m = 1 to n
if n%m = 0
tau = tau + 1
ok
next
num = num + 1
if num%10 = 1
see nl
ok
tau = string(tau)
if len(tau) = 1
tau = " " + tau
ok
see "" + tau + " "
end
 

Output:

The tau functions for the first 100 positive integers 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 

Ruby[edit]

require 'prime'
 
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}
 
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

Rust[edit]

// returns the highest power of i that is a factor of n,
// and n divided by that power of i
fn factor_exponent(n: i32, i: i32) -> (i32, i32) {
if n % i == 0 {
let (a, b) = factor_exponent(n / i, i);
(a + 1, b)
} else {
(0, n)
}
}
 
fn tau(n: i32) -> i32 {
for i in 2..(n+1) {
if n % i == 0 {
let (count, next) = factor_exponent(n, i);
return (count + 1) * tau(next);
}
}
return 1;
}
 
fn main() {
for i in 1..101 {
print!("{} ", tau(i));
}
}

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

Sidef[edit]

Built-in:

say { .sigma0 }.map(1..100).join(' ')
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

Swift[edit]

import Foundation
 
// See https://en.wikipedia.org/wiki/Divisor_function
func divisorCount(number: Int) -> Int {
var n = number
var total = 1
// Deal with powers of 2 first
while (n & 1) == 0 {
total += 1
n >>= 1
}
// Odd prime factors up to the square root
var p = 3
while p * p <= n {
var count = 1
while n % p == 0 {
count += 1
n /= p
}
total *= count
p += 2
}
// If n > 1 then it's prime
if n > 1 {
total *= 2
}
return total
}
 
let limit = 100
print("Count of divisors for the first \(limit) positive integers:")
for n in 1...limit {
print(String(format: "%3d", divisorCount(number: n)), terminator: "")
if n % 20 == 0 {
print()
}
}
Output:
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

Tiny BASIC[edit]

    LET N = 0
10 LET N = N + 1
IF N < 3 THEN GOTO 100
LET T = 2
LET A = 1
20 LET A = A + 1
IF (N/A)*A = N THEN LET T = T + 1
IF A<(N+1)/2 THEN GOTO 20
30 PRINT "Tau(",N,") = ",T
IF N<100 THEN GOTO 10
END
100 LET T = N
GOTO 30

Wren[edit]

Library: Wren-math
Library: Wren-fmt
import "/math" for Int
import "/fmt" for Fmt
 
System.print("The tau functions for the first 100 positive integers are:")
for (i in 1..100) {
Fmt.write("$2d ", Int.divisors(i).count)
if (i % 20 == 0) System.print()
}
Output:
The tau functions for the first 100 positive integers 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