Tau number: Difference between revisions
m →{{header|REXX}}: added/changed comments, added a foot title. |
|||
Line 1,613: | Line 1,613: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX pgm displays N tau numbers, an integer divisible by the # of its divisors). */ |
<lang rexx>/*REXX pgm displays N tau numbers, an integer divisible by the # of its divisors). */ |
||
parse arg n . |
parse arg n cols . /*obtain optional argument from the CL.*/ |
||
if n=='' | n=="," then |
if n=='' | n=="," then n= 100 /*Not specified? Then use the default.*/ |
||
if cols=='' | cols=="," then cols= 10 /*Not specified? Then use the default.*/ |
|||
⚫ | |||
@tau= ' the first ' commas(n) " tau numbers " /*the title of the tau numbers table. */ |
|||
⚫ | |||
say ' index │'center(@tau, 1 + cols*(w+1) ) /*display the title of the output table*/ |
|||
⚫ | |||
say '───────┼'center("" , 1 + cols*(w+1), '─') /* " " header " " " " */ |
|||
⚫ | |||
do j=1 until #==n /*search for N tau numbers */ |
do j=1 until #==n /*search for N tau numbers */ |
||
if j//tau(j) \==0 then iterate /*Is this a tau number? No, then skip.*/ |
if j//tau(j) \==0 then iterate /*Is this a tau number? No, then skip.*/ |
||
#= # + 1 /*bump the count of tau numbers found. */ |
#= # + 1 /*bump the count of tau numbers found. */ |
||
$= $ |
$= $ right( commas(j), w) /*add a tau number to the output list. */ |
||
if #// |
if #//cols\==0 then iterate /*Not a multiple of cols? Don't show. */ |
||
say |
say center(idx, 7)'│' substr($, 2) /*display partial list to the terminal.*/ |
||
idx= idx + cols; $= /*bump idx by number of cols; nullify $*/ |
|||
end /*j*/ |
end /*j*/ |
||
if $\=='' then say center( |
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ |
||
say '───────┴'center("" , 1 + cols*(w+1), '─') |
|||
exit 0 /*stick a fork in it, we're all done. */ |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Line 1,650: | Line 1,652: | ||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
<pre> |
<pre> |
||
index │ the first 100 tau numbers |
|||
───────┼─────────────────────────────────────────────────────────────────────────────────────────── |
|||
1 │ 1 2 8 9 12 18 24 36 40 56 |
|||
─index─ ──────────────────────────── tau numbers ───────────────────────────── |
|||
11 │ 60 72 80 84 88 96 104 108 128 132 |
|||
21 │ 136 152 156 180 184 204 225 228 232 240 |
|||
31 │ 248 252 276 288 296 328 344 348 360 372 |
|||
41 │ 376 384 396 424 441 444 448 450 468 472 |
|||
51 │ 480 488 492 504 516 536 560 564 568 584 |
|||
61 │ 600 612 625 632 636 640 664 672 684 708 |
|||
71 │ 712 720 732 776 792 804 808 824 828 852 |
|||
81 │ 856 864 872 876 880 882 896 904 936 948 |
|||
91 │ 972 996 1,016 1,040 1,044 1,048 1,056 1,068 1,089 1,096 |
|||
───────┴─────────────────────────────────────────────────────────────────────────────────────────── |
|||
91 972 996 1,016 1,040 1,044 1,048 1,056 1,068 1,089 1,096 |
|||
</pre> |
</pre> |
||
Revision as of 21:22, 15 May 2021
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
A Tau number is a positive integer divisible by the count of its positive divisors.
- Task
Show the first 100 Tau numbers.
- Related task
11l
<lang 11l>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
F is_tau_number(n)
I n <= 0 R 0B R 0 == n % tau(n)
V n = 1 [Int] ans L ans.len < 100
I is_tau_number(n) ans.append(n) n++
print(ans)</lang>
- Output:
[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, 1016, 1040, 1044, 1048, 1056, 1068, 1089, 1096]
ALGOL 68
<lang algol68>BEGIN # find tau numbers - numbers divisible by the count of theoir divisors #
# calculates the number of divisors of v # PROC divisor count = ( INT v )INT: BEGIN INT total := 1, n := v; # Deal with powers of 2 first # WHILE NOT ODD n DO total +:= 1; n OVERAB 2 OD; # Odd prime factors up to the square root # INT p := 1; WHILE p +:= 2; ( p * p ) <= n DO INT count := 1; WHILE n MOD p = 0 DO count +:= 1; n OVERAB p OD; total *:= count OD; # If n > 1 then it's prime # IF n > 1 THEN total *:= 2 FI; total END # divisor count #; BEGIN INT tau limit = 100; INT tau count := 0; print( ( "The first ", whole( tau limit, 0 ), " tau numbers:", newline ) ); FOR n WHILE tau count < tau limit DO IF n MOD divisor count( n ) = 0 THEN tau count +:= 1; print( ( whole( n, -6 ) ) ); IF tau count MOD 10 = 0 THEN print( ( newline ) ) FI FI OD END
END</lang>
- Output:
The first 100 tau numbers: 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 1016 1040 1044 1048 1056 1068 1089 1096
ALGOL-M
<lang algolm>begin
integer array dcount[1:1100]; integer i, j, n;
integer function mod(a,b); integer a,b; mod := a-a/b*b;
% Calculate counts of divisors for 1 .. 1100 % for i := 1 step 1 until 1100 do dcount[i] := 1; for i := 2 step 1 until 1100 do begin
j := i; while j <= 1100 do begin dcount[j] := dcount[j] + 1; j := j + i; end;
end;
n := 0; i := 1; while n < 100 do begin
if mod(i, dcount[i])=0 then begin if mod(n, 10)=0 then write(i) else writeon(i); n := n + 1; end; i := i + 1;
end; end</lang>
- Output:
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 1016 1040 1044 1048 1056 1068 1089 1096
APL
<lang APL>(⊢(/⍨)(0=(0+.=⍳|⊢)|⊢)¨)⍳ 1096</lang>
- Output:
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 1016 1040 1044 1048 1056 1068 1089 1096
AppleScript
<lang applescript>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, counter, astid set output to {"First 100 tau numbers:"} set n to 0 set counter to 0 repeat until (counter = 100)
set n to n + 1 if (n mod (factorCount(n)) = 0) then set counter to counter + 1 if (counter mod 20 = 1) then set end of output to linefeed set end of output to text -5 thru -1 of (" " & n) end if
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</lang>
- Output:
<lang applescript>"First 100 tau numbers:
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 1016 1040 1044 1048 1056 1068 1089 1096"</lang>
Arturo
<lang rebol>tau: function [x] -> size factors x
found: 0 i:1 while [found<100][
if 0 = i % tau i [ prints pad to :string i 5 found: found + 1 if 0 = found % 10 -> print "" ] i: i + 1
]</lang>
- Output:
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 1016 1040 1044 1048 1056 1068 1089 1096
AWK
<lang AWK>
- syntax: GAWK -f TAU_NUMBER.AWK
BEGIN {
print("The first 100 tau numbers:") while (count < 100) { i++ if (i % count_divisors(i) == 0) { printf("%4d ",i) if (++count % 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)
} </lang>
- Output:
The first 100 tau numbers: 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 1016 1040 1044 1048 1056 1068 1089 1096
BASIC
<lang BASIC>10 DEFINT A-Z 20 S=0: N=1 30 C=1 40 IF N<>1 THEN FOR I=1 TO N/2: C=C-(N MOD I=0): NEXT 50 IF N MOD C=0 THEN PRINT N,: S=S+1 60 N=N+1 70 IF S<100 THEN 30 80 END</lang>
- Output:
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 1016 1040 1044 1048 1056 1068 1089 1096
BCPL
<lang BCPL>get "libhdr"
// Count the divisors of 1..N let divcounts(v, n) be $( // Every positive number is divisible by 1
for i=1 to n do v!i := 1; for i=2 to n do $( let j = i while j <= n do $( // J is divisible by I v!j := v!j + 1 j := j + i $) $)
$)
// Given a stored vector of divisors counts, is a number a tau number? let tau(v, i) = i rem v!i = 0
let start() be $( let dvec = vec 1100
let n, seen = 1, 0 divcounts(dvec, 1100) // find amount of divisors for each number while seen < 100 do $( if tau(dvec, n) then $( writed(n, 5) seen := seen + 1 if seen rem 10 = 0 then wrch('*N') $) n := n + 1 $)
$)</lang>
- Output:
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 1016 1040 1044 1048 1056 1068 1089 1096
C
<lang c>#include <stdio.h>
unsigned int divisor_count(unsigned int n) {
unsigned int total = 1; unsigned int p;
// Deal with powers of 2 first for (; (n & 1) == 0; n >>= 1) { ++total; } // Odd prime factors up to the square root for (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 count = 0; unsigned int n;
printf("The first %d tau numbers are:\n", limit); for (n = 1; count < limit; ++n) { if (n % divisor_count(n) == 0) { printf("%6d", n); ++count; if (count % 10 == 0) { printf("\n"); } } }
return 0;
}</lang>
- Output:
The first 100 tau numbers are: 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 1016 1040 1044 1048 1056 1068 1089 1096
C++
<lang cpp>#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 << "The first " << limit << " tau numbers are:\n"; unsigned int count = 0; for (unsigned int n = 1; count < limit; ++n) { if (n % divisor_count(n) == 0) { std::cout << std::setw(6) << n; ++count; if (count % 10 == 0) std::cout << '\n'; } }
}</lang>
- Output:
The first 100 tau numbers are: 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 1016 1040 1044 1048 1056 1068 1089 1096
Cowgol
<lang cowgol>include "cowgol.coh";
- Get count of positive divisors of number
sub pos_div(num: uint16): (count: uint16) is
count := 1; if num != 1 then var cur: uint16 := 1; while cur <= num/2 loop if num % cur == 0 then count := count + 1; end if; cur := cur + 1; end loop; end if;
end sub;
- Print first 100 Tau numbers
var nums: uint8 := 0; var cur: uint16 := 0; var col: uint16 := 10; while nums < 100 loop
cur := cur + 1; if cur % pos_div(cur) == 0 then print_i16(cur); col := col - 1; if col == 0 then print_nl(); col := 10; else print_char('\t'); end if; nums := nums + 1; end if;
end loop;</lang>
- Output:
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 1016 1040 1044 1048 1056 1068 1089 1096
D
<lang d>import std.stdio;
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("The first ", limit, " tau numbers are:"); uint count = 0; for (uint n = 1; count < limit; ++n) { if (n % divisor_count(n) == 0) { writef("%6d", n); ++count; if (count % 10 == 0) { writeln; } } }
}</lang>
- Output:
The first 100 tau numbers are: 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 1016 1040 1044 1048 1056 1068 1089 1096
Delphi
<lang Delphi> program Tau_number;
{$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 first 100 tau numbers are:'); var count := 0; var i := 1; while count < 100 do begin var tf := CountDivisors(i); if i mod tf = 0 then begin write(format('%4d ', [i])); inc(count); if count mod 10 = 0 then writeln; end; inc(i); end;
{$IFNDEF UNIX} readln; {$ENDIF}
end.</lang>
F#
This task uses [Tau_function#F.23] <lang fsharp> // Tau number. Nigel Galloway: March 9th., 2021 Seq.initInfinite((+)1)|>Seq.filter(fun n->n%(tau n)=0)|>Seq.take 100|>Seq.iter(printf "%d "); printfn "" </lang>
- Output:
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 1016 1040 1044 1048 1056 1068 1089 1096
Factor
<lang factor>USING: assocs grouping io kernel lists lists.lazy math math.functions math.primes.factors prettyprint sequences sequences.extras ;
- tau ( n -- count ) group-factors values [ 1 + ] map-product ;
- tau? ( n -- ? ) dup tau divisor? ;
- taus ( -- list ) 1 lfrom [ tau? ] lfilter ;
! Task "The first 100 tau numbers are:" print 100 taus ltake list>array 10 group simple-table. </lang>
- Output:
The first 100 tau numbers are: 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 1016 1040 1044 1048 1056 1068 1089 1096
Forth
<lang forth>: 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_tau_numbers ( n -- )
." The first " dup . ." tau numbers are:" cr 0 >r 1 begin over r@ > while dup dup divisor_count mod 0= if dup 6 .r r> 1+ dup 10 mod 0= if cr else space then >r then 1+ repeat 2drop rdrop ;
100 print_tau_numbers bye</lang>
- Output:
The first 100 tau numbers are: 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 1016 1040 1044 1048 1056 1068 1089 1096
FreeBASIC
<lang freebasic>function numdiv( n as uinteger ) as uinteger
dim as uinteger c = 2 for i as uinteger = 2 to (n+1)\2 if n mod i = 0 then c += 1 next i return c
end function
function istau( n as uinteger ) as boolean
if n = 1 then return true if n mod numdiv(n) = 0 then return true else return false
end function
dim as uinteger c = 0, i=1 while c < 100
if istau(i) then print i, c += 1 if c mod 10 = 0 then print end if i += 1
wend</lang>
- Output:
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 1016 1040 1044 1048 1056 1068 1089 1096
Go
<lang go>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 first 100 tau numbers are:") count := 0 i := 1 for count < 100 { tf := countDivisors(i) if i%tf == 0 { fmt.Printf("%4d ", i) count++ if count%10 == 0 { fmt.Println() } } i++ }
}</lang>
- Output:
The first 100 tau numbers are: 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 1016 1040 1044 1048 1056 1068 1089 1096
Haskell
<lang Haskell>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)
isTau :: Integral a => a -> Bool isTau n = 0 == mod n (tau n)
main = print . take 100 . filter isTau $ [1..]</lang>
- Output:
[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,1016,1040,1044,1048,1056,1068,1089,1096]
and we could also define Tau numbers in terms of a more general divisors function:
<lang haskell>import Data.List (group, scanl) import Data.List.Split (chunksOf) import Data.Numbers.Primes (primeFactors)
TAU NUMBERS ----------------------
tauNumbers :: [Int] tauNumbers =
filter ((0 ==) . (rem <*> (length . divisors))) [1 ..]
TEST -------------------------
main :: IO () main =
let xs = take 100 $ fmap show tauNumbers w = length $ last xs in (putStrLn . unlines) $ unwords . fmap (justifyRight w ' ') <$> chunksOf 10 xs
GENERIC ------------------------
divisors :: Int -> [Int] divisors =
foldr (flip ((<*>) . fmap (*)) . scanl (*) 1) [1] . group . primeFactors
justifyRight :: Int -> Char -> String -> String justifyRight n c = (drop . length) <*> (replicate n c <>)</lang>
- Output:
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 1016 1040 1044 1048 1056 1068 1089 1096
J
NB. use _25]\100{.(#~ tau_number&>) #\i.2000 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 1016 1040 1044 1048 1056 1068 1089 1096 NB. definitions tau_number 0 = (|~ tally_factors) tally_factors [: */ [: >: [: {: __&q:
Java
<lang java>public class Tau {
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 long limit = 100; System.out.printf("The first %d tau numbers are:%n", limit); long count = 0; for (long n = 1; count < limit; ++n) { if (n % divisorCount(n) == 0) { System.out.printf("%6d", n); ++count; if (count % 10 == 0) { System.out.println(); } } } }
}</lang>
- Output:
The first 100 tau numbers are: 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 1016 1040 1044 1048 1056 1068 1089 1096
Julia
<lang 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
function taunumbers(toget = 100)
n = 0 for i in 1:100000000 if i % numfactors(i) == 0 n += 1 print(rpad(i, 5), n % 20 == 0 ? " \n" : "") n == toget && break end end
end
taunumbers()
</lang>
- Output:
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 1016 1040 1044 1048 1056 1068 1089 1096
Lua
<lang lua>function divisor_count(n)
local total = 1
-- Deal with powers of 2 first while (n & 1) == 0 do total = total + 1 n = n >> 1 end -- Odd prime factors up to the square root local p = 3 while p * p <= n do local count = 1 while n % p == 0 do count = count + 1 n = math.floor(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
local limit = 100 local count = 0 print("The first " .. limit .. " tau numbers are:") local n = 1 while count < limit do
if n % divisor_count(n) == 0 then io.write(string.format("%6d", n)) count = count + 1 if count % 10 == 0 then print() end end n = n + 1
end</lang>
- Output:
The first 100 tau numbers are: 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 1016 1040 1044 1048 1056 1068 1089 1096
MAD
<lang MAD> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(N) ENTRY TO POSDIV. COUNT = 1 THROUGH DIV, FOR I=2, 1, I.G.N
DIV WHENEVER N/I*I.E.N, COUNT = COUNT+1
FUNCTION RETURN COUNT END OF FUNCTION SEEN=0 THROUGH TAU, FOR X=1, 1, SEEN.GE.100 DIVS=POSDIV.(X) WHENEVER X/DIVS*DIVS.E.X PRINT FORMAT NUM,X SEEN = SEEN+1
TAU END OF CONDITIONAL
VECTOR VALUES NUM = $I4*$ END OF PROGRAM </lang>
- Output:
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 1016 1040 1044 1048 1056 1068 1089 1096
Nim
<lang Nim>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
var count = 0 var n = 1 var tauNumbers: seq[Natural] while true:
if n mod divcount(n) == 0: tauNumbers.add n inc count if count == 100: break inc n
echo "First 100 tau numbers:" for i, n in tauNumbers:
stdout.write ($n).align(5) if i mod 20 == 19: echo()</lang>
- Output:
First 100 tau numbers: 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 1016 1040 1044 1048 1056 1068 1089 1096
Perl
<lang perl>use strict; use warnings; use feature 'say'; use ntheory 'divisors';
my(@x,$n);
do { push(@x,$n) unless $n % scalar(divisors(++$n)) } until 100 == @x;
say "Tau numbers - first 100:\n" .
((sprintf "@{['%5d' x 100]}", @x[0..100-1]) =~ s/(.{80})/$1\n/gr);</lang>
- Output:
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 1016 1040 1044 1048 1056 1068 1089 1096
Phix
imperative
integer n = 1, found = 0 while found<100 do if remainder(n,length(factors(n,1)))=0 then found += 1 printf(1,"%,6d",n) if remainder(found,10)=0 then puts(1,"\n") end if end if n += 1 end while
- Output:
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
functional/memoised
same output
sequence tau_cache = {1} function tau(integer n) while n>length(tau_cache) do integer nt = tau_cache[$]+1 while remainder(nt,length(factors(nt,1)))!=0 do nt += 1 end while tau_cache &= nt end while return tau_cache[n] end function puts(1,join_by(apply(true,sprintf,{{"%,6d"},apply(tagset(100),tau)}),1,10,""))
PL/M
<lang plm>100H: BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS; EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT; PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;
/* PRINT NUMBER RIGHT-ALIGNED IN 7 POSITIONS */ PRINT$NUMBER: PROCEDURE (N);
DECLARE S (7) BYTE INITIAL (' .....$'); DECLARE N ADDRESS, I BYTE; I = 6;
DIGIT:
I = I - 1; S(I) = N MOD 10 + '0'; N = N / 10; IF N > 0 THEN GO TO DIGIT; DO WHILE I <> 0; I = I - 1; S(I) = ' '; END; CALL PRINT(.S);
END PRINT$NUMBER;
/* COUNT AND STORE AMOUNT OF DIVISORS FOR 1..N AT VEC */ COUNT$DIVS: PROCEDURE (VEC, N);
DECLARE (VEC, N, V BASED VEC) ADDRESS; DECLARE (I, J) ADDRESS; DO I=1 TO N; V(I) = 1; END; DO I=2 TO N; J = I; DO WHILE J <= N; V(J) = V(J) + 1; J = J + I; END; END;
END COUNT$DIVS;
/* GIVEN VECTOR OF COUNT OF DIVISORS, SEE IF N IS A TAU NUMBER */ TAU: PROCEDURE (VEC, N) BYTE;
DECLARE (VEC, N, V BASED VEC) ADDRESS; RETURN N MOD V(N) = 0;
END TAU;
DECLARE AMOUNT LITERALLY '100'; DECLARE LIMIT LITERALLY '1100';
DECLARE SEEN BYTE INITIAL (0); DECLARE N ADDRESS INITIAL (1);
CALL COUNT$DIVS(.MEMORY, LIMIT); DO WHILE SEEN < AMOUNT;
IF TAU(.MEMORY, N) THEN DO; CALL PRINT$NUMBER(N); SEEN = SEEN + 1; IF SEEN MOD 10 = 0 THEN CALL PRINT(.(13,10,'$')); END; N = N + 1;
END;
CALL EXIT; EOF</lang>
- Output:
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 1016 1040 1044 1048 1056 1068 1089 1096
PureBasic
<lang PureBasic>OpenConsole()
Procedure.i numdiv(n)
c=2 For i=2 To (n+1)/2 : If n%i=0 : c+1 : EndIf : Next ProcedureReturn c
EndProcedure
Procedure.b istau(n)
If n=1 : ProcedureReturn #True : EndIf If n%numdiv(n)=0 : ProcedureReturn #True : Else : ProcedureReturn #False : EndIf
EndProcedure
c=0 : i=1 While c<100
If istau(i) : Print(RSet(Str(i),4)+#TAB$) : c+1 : If c%10=0 : PrintN("") : EndIf: EndIf i+1
Wend
Input()</lang>
- Output:
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 1016 1040 1044 1048 1056 1068 1089 1096
Python
Python: Procedural
<lang Python>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
def is_tau_number(n):
assert(isinstance(n, int)) if n <= 0: return False return 0 == n%tau(n)
if __name__ == "__main__":
n = 1 ans = [] while len(ans) < 100: if is_tau_number(n): ans.append(n) n += 1 print(ans)</lang>
- Output:
[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, 1016, 1040, 1044, 1048, 1056, 1068, 1089, 1096]
Python: Functional
Composing pure functions, and defining a non-finite stream of Tau numbers in terms of a generic `divisors` function: <lang python>Tau numbers
from operator import mul from math import floor, sqrt from functools import reduce from itertools import (
accumulate, chain, count, groupby, islice, product
)
- tauNumbers :: Generator [Int]
def tauNumbers():
Positive integers divisible by the count of their positive divisors. return ( n for n in count(1) if 0 == n % len(divisors(n)) )
- ------------------------- TEST -------------------------
- main :: IO ()
def main():
The first hundred Tau numbers. xs = take(100)( tauNumbers() ) w = len(str(xs[-1])) print('\n'.join([ ' '.join([ str(cell).rjust(w, ' ') for cell in row ]) for row in chunksOf(10)(xs) ]))
- ----------------------- 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):
The ordered divisors of n. def go(a, x): return [a * b for a, b in product( a, accumulate(chain([1], x), mul) )] return sorted( reduce(go, [ list(g) for _, g in groupby(primeFactors(n)) ], [1]) ) if 1 < n else [1]
- primeFactors :: Int -> [Int]
def primeFactors(n):
A list of the prime factors of n. def f(qr): r = qr[1] return step(r), 1 + r
def step(x): return 1 + (x << 2) - ((x >> 1) << 1)
def go(x): root = floor(sqrt(x))
def p(qr): q = qr[0] return root < q or 0 == (x % q)
q = until(p)(f)( (2 if 0 == x % 2 else 3, 1) )[0] return [x] if q > root else [q] + go(x // q)
return go(n)
- 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
- until :: (a -> Bool) -> (a -> a) -> a -> a
def until(p):
The result of repeatedly applying f until p holds. The initial seed value is x. def go(f): def g(x): v = x while not p(v): v = f(v) return v return g return go
- MAIN ---
if __name__ == '__main__':
main()
</lang>
- Output:
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 1016 1040 1044 1048 1056 1068 1089 1096
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.
<lang perl6>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</lang>
- 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
<lang rexx>/*REXX pgm displays N tau numbers, an integer divisible by the # of its divisors). */ parse arg n cols . /*obtain optional argument from the CL.*/ if n== | n=="," then n= 100 /*Not specified? Then use the default.*/ if cols== | cols=="," then cols= 10 /*Not specified? Then use the default.*/ w= max(8, length(n) ) /*W: used to align 1st output column. */ @tau= ' the first ' commas(n) " tau numbers " /*the title of the tau numbers table. */ say ' index │'center(@tau, 1 + cols*(w+1) ) /*display the title of the output table*/ say '───────┼'center("" , 1 + cols*(w+1), '─') /* " " header " " " " */ idx= 1; #= 0; $= /*idx: line; #: tau numbers; $: #s */
do j=1 until #==n /*search for N tau numbers */ if j//tau(j) \==0 then iterate /*Is this a tau number? No, then skip.*/ #= # + 1 /*bump the count of tau numbers found. */ $= $ right( commas(j), w) /*add a tau number to the output list. */ if #//cols\==0 then iterate /*Not a multiple of cols? Don't show. */ say center(idx, 7)'│' substr($, 2) /*display partial list to the terminal.*/ idx= idx + cols; $= /*bump idx by number of cols; nullify $*/ end /*j*/
if $\== then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ say '───────┴'center("" , 1 + cols*(w+1), '─') exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ? /*──────────────────────────────────────────────────────────────────────────────────────*/ 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 do; #= 2; end /*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*/ /* [↑] this form of DO loop is faster.*/ return #</lang>
- output when using the default input:
index │ the first 100 tau numbers ───────┼─────────────────────────────────────────────────────────────────────────────────────────── 1 │ 1 2 8 9 12 18 24 36 40 56 11 │ 60 72 80 84 88 96 104 108 128 132 21 │ 136 152 156 180 184 204 225 228 232 240 31 │ 248 252 276 288 296 328 344 348 360 372 41 │ 376 384 396 424 441 444 448 450 468 472 51 │ 480 488 492 504 516 536 560 564 568 584 61 │ 600 612 625 632 636 640 664 672 684 708 71 │ 712 720 732 776 792 804 808 824 828 852 81 │ 856 864 872 876 880 882 896 904 936 948 91 │ 972 996 1,016 1,040 1,044 1,048 1,056 1,068 1,089 1,096 ───────┴───────────────────────────────────────────────────────────────────────────────────────────
Ring
<lang ring> see "The first 100 tau numbers are:" + nl + nl
n = 1 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 if n%tau = 0 num = num + 1 if num%10 = 1 see nl ok see "" + n + " " ok
end </lang> Output:
The first 100 tau numbers are: 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 1016 1040 1044 1048 1056 1068 1089 1096
Ruby
<lang ruby>require 'prime'
taus = Enumerator.new do |y|
(1..).each do |n| num_divisors = n.prime_division.inject(1){|prod, n| prod *= n[1] + 1 } y << n if n % num_divisors == 0 end
end
p taus.take(100) </lang>
- Output:
[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, 1016, 1040, 1044, 1048, 1056, 1068, 1089, 1096]
Rust
<lang rust> /// Gets all divisors of a number, including itself fn get_divisors(n: u32) -> Vec<u32> {
let mut results = Vec::new();
for i in 1..(n / 2 + 1) { if n % i == 0 { results.push(i); } } results.push(n); results
}
fn is_tau_number(i: u32) -> bool {
0 == i % get_divisors(i).len() as u32
}
fn main() {
println!("\nFirst 100 Tau numbers:"); let mut counter: u32 = 0; let mut i: u32 = 1; while counter < 100 { if is_tau_number(i) { print!("{:>4}", i); counter += 1; print!("{}", if counter % 20 == 0 { "\n" } else { "," }); } i += 1; }
}
</lang>
- Output:
First 100 Tau numbers: 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,1016,1040,1044,1048,1056,1068,1089,1096
Swift
<lang swift>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("The first \(limit) tau numbers are:") var count = 0 var n = 1 while count < limit {
if n % divisorCount(number: n) == 0 { print(String(format: "%5d", n), terminator: "") count += 1 if count % 10 == 0 { print() } } n += 1
}</lang>
- Output:
The first 100 tau numbers are: 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 1016 1040 1044 1048 1056 1068 1089 1096
Wren
<lang ecmascript>import "/math" for Int import "/fmt" for Fmt
System.print("The first 100 tau numbers are:") var count = 0 var i = 1 while (count < 100) {
var tf = Int.divisors(i).count if (i % tf == 0) { Fmt.write("$,5d ", i) count = count + 1 if (count % 10 == 0) System.print() } i = i + 1
}</lang>
- Output:
The first 100 tau numbers are: 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