Tau number

From Rosetta Code
Revision as of 16:55, 20 December 2020 by Blek (talk | contribs) (Python added)
Tau number is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

A Tau number is a positive integer divisible by the count of its positive divisors.


Task

Show the first 100 Tau numbers.


Related task



Python

<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]

REXX

<lang rexx>/*REXX program displays the first N tau numbers (an integer divisible by its tau number)*/ parse arg n . /*obtain optional argument from the CL.*/ if n== | n=="," then n= 100 /*Not specified? Then use the default.*/ say 'The first ' n " tau numbers:"; say /*display what the output being shown. */ say '─index─' center(" tau numbers ", 70, '─') /*display a title for the tau numbers. */ w= max(7, length(n) ) /*W: used to align 1st output column. */ $= /*$: the output list, shown ten/line. */

                        #= 0                    /*#:  the count of tau numbers so far. */
          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), 7)        /*add a tau number to the output list. */
          if #//10\==0  then iterate            /*Not a multiple of 10?  Don't display.*/
          say right(commas(#-9), 6)' '  $       /*display partial list to the terminal.*/
          $=                                    /*start with a blank line for next line*/
          end   /*j*/

if $\== then say center(#//10, 7) $ /*any residuals tau #s left to display?*/ 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:
The first  100  tau numbers:

─index─ ──────────────────────────── 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