How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the REXX programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the REXX programming language
Table of Contents
Problem Statement
Calculate the sequence where each term an is the smallest natural number greater than the previous term, that has exactly 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: smallest number greater than previous term with exactly n divisors step by step in the REXX programming language
Source code in the rexx programming language
/*REXX program finds and displays N numbers of the "anti─primes plus" sequence. */
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/
idx= 1 /*the maximum number of divisors so far*/
say '──index── ──anti─prime plus──' /*display a title for the numbers shown*/
#= 0 /*the count of anti─primes found " " */
do i=1 until #==N /*step through possible numbers by twos*/
d= #divs(i); if d\==idx then iterate /*get # divisors; Is too small? Skip.*/
#= # + 1; idx= idx + 1 /*found an anti─prime #; set new minD.*/
say center(#, 8) right(i, 15) /*display the index and the anti─prime.*/
end /*i*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
#divs: procedure; parse arg x 1 y /*X and Y: both set from 1st argument.*/
if x<7 then do /*handle special cases for numbers < 7.*/
if x<3 then return x /* " " " " one and two.*/
if x<5 then return x - 1 /* " " " " three & four*/
if x==5 then return 2 /* " " " " five. */
if x==6 then return 4 /* " " " " six. */
end
odd= x // 2 /*check if X is odd or not. */
if odd then #= 1; /*Odd? Assume Pdivisors count of 1.*/
else do; #= 3; y= x % 2 /*Even? " " " " 3.*/
end /* [↑] Even, so add 2 known divisors.*/
/* [↓] start with known number of Pdivs*/
do k=3 for x%2-3 by 1+odd while k
if x//k==0 then do /*if no remainder, then found a divisor*/
#= # + 2; y= x % k /*bump the # Pdivs; calculate limit Y.*/
if k>=y then do; #= # - 1; leave
end /* [↑] has the limit been reached? */
end /* ___ */
else if k*k>x then leave /*only divide up to the √ x */
end /*k*/ /* [↑] this form of DO loop is faster.*/
return # + 1 /*bump "proper divisors" to "divisors".*/
You may also check:How to resolve the algorithm Bulls and cows step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Temperature conversion step by step in the VBScript programming language
You may also check:How to resolve the algorithm Stack step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Filter step by step in the Nemerle programming language