How to resolve the algorithm Sequence: nth number with exactly n divisors step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sequence: nth number with exactly n divisors step by step in the zkl programming language

Table of Contents

Problem Statement

Calculate the sequence where each term an is the nth that has n divisors. Show here, on this page, at least the first 15 terms of the sequence.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sequence: nth number with exactly n divisors step by step in the zkl programming language

Source code in the zkl programming language

var [const] BI=Import("zklBigNum"), pmax=25;  // libGMP
p:=BI(1);
primes:=pmax.pump(List(0), p.nextPrime, "copy");  //-->(0,3,5,7,11,13,17,19,...)
 
fcn countDivisors(n){
   count:=1;
   while(n%2==0){ n/=2; count+=1; }
   foreach d in ([3..*,2]){
      q,r := n/d, n%d;
      if(r==0){
	 dc:=0;
	 while(r==0){
	    dc+=count;
	    n,q,r = q, n/d, n%d;
	 }
	 count+=dc;
      }
      if(d*d > n) break;
   }
   if(n!=1) count*=2;
   count
}

println("The first ", pmax, " terms in the sequence are:");
foreach i in ([1..pmax]){
   if(BI(i).probablyPrime()) println("%2d : %,d".fmt(i,primes[i].pow(i-1)));
   else{
      count:=0;
      foreach j in ([1..*]){
         if(i%2==1 and j != j.toFloat().sqrt().toInt().pow(2)) continue;
	 if(countDivisors(j) == i){
	    count+=1;
	    if(count==i){
	       println("%2d : %,d".fmt(i,j));
	       break;
	    }
	 }
      }
   }
}

  

You may also check:How to resolve the algorithm Inheritance/Single step by step in the ooRexx programming language
You may also check:How to resolve the algorithm MAC vendor lookup step by step in the OCaml programming language
You may also check:How to resolve the algorithm Naming conventions step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the Fortran programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the App Inventor programming language