How to resolve the algorithm Aliquot sequence classifications step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Aliquot sequence classifications step by step in the jq programming language

Table of Contents

Problem Statement

An aliquot sequence of a positive integer K is defined recursively as the first member being K and subsequent members being the sum of the Proper divisors of the previous term.

Show all output on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Aliquot sequence classifications step by step in the jq programming language

Source code in the jq programming language

# "until" is available in more recent versions of jq
# than jq 1.4
def until(cond; next):
  def _until:
    if cond then . else (next|_until) end;
  _until;

# unordered
def proper_divisors:
  . as $n
  | if $n > 1 then 1,
      ( range(2; 1 + (sqrt|floor)) as $i
        | if ($n % $i) == 0 then $i,
            (($n / $i) | if . == $i then empty else . end)
          else empty
          end)
    else empty
    end;

# sum of proper divisors, or 0
def pdsum:
  [proper_divisors] | add // 0;

# input is n
# maxlen defaults to 16;
# maxterm defaults to 2^47
def aliquot(maxlen; maxterm):
  (maxlen // 15) as $maxlen
  | (maxterm // 40737488355328) as $maxterm
  | if . == 0 then "terminating at 0" 
    else
    # [s, slen, new] = [[n], 1, n]
    [ [.], 1, .]
    | until( type == "string" or .[1] > $maxlen or .[2] > $maxterm;
             .[0] as $s | .[1] as $slen
             | ($s | .[length-1] | pdsum) as $new
             | if ($s|index($new)) then
                 if $s[0] == $new then
                     if $slen == 1 then "perfect \($s)"
                     elif $slen == 2 then "amicable: \($s)"
                     else "sociable of length \($slen): \($s)"
		     end
                 elif ($s | .[length-1]) == $new then "aspiring: \($s)"
                 else "cyclic back to \($new): \($s)"
		 end
               elif $new == 0 then "terminating: \($s + [0])"
               else [ ($s + [$new]), ($slen + 1), $new ]
               end )
    | if type == "string" then . else "non-terminating: \(.[0])" end
    end;
 
def task:
  def pp: "\(.): \(aliquot(null;null))";
     (range(1; 11) | pp),
     "",
     ((11, 12, 28, 496, 220, 1184, 12496, 1264460,
      790, 909, 562, 1064, 1488, 15355717786080) | pp);
        
task

$ jq -n -r -f aliquot.jq
1: terminating: [1,0]
2: terminating: [2,1,0]
3: terminating: [3,1,0]
4: terminating: [4,3,1,0]
5: terminating: [5,1,0]
6: perfect [6]
7: terminating: [7,1,0]
8: terminating: [8,7,1,0]
9: terminating: [9,4,3,1,0]
10: terminating: [10,8,7,1,0]

11: terminating: [11,1,0]
12: terminating: [12,16,15,9,4,3,1,0]
28: perfect [28]
496: perfect [496]
220: amicable: [220,284]
1184: amicable: [1184,1210]
12496: sociable of length 5: [12496,14288,15472,14536,14264]
1264460: sociable of length 4: [1264460,1547860,1727636,1305184]
790: aspiring: [790,650,652,496]
909: aspiring: [909,417,143,25,6]
562: cyclic back to 284: [562,284,220]
1064: cyclic back to 1184: [1064,1336,1184,1210]
1488: non-terminating: [1488,2480,3472,4464,8432,9424,10416,21328,22320,55056,95728,96720,236592,459792,881392,882384]
15355717786080: non-terminating: [15355717786080,44534663601120]


  

You may also check:How to resolve the algorithm Circular primes step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Write language name in 3D ASCII step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the EDSAC order code programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the Pop11 programming language
You may also check:How to resolve the algorithm Product of min and max prime factors step by step in the Modula-2 programming language