How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the jq programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the jq programming language
Table of Contents
Problem Statement
These define three classifications of positive integers based on their proper divisors. Let P(n) be the sum of the proper divisors of n where the proper divisors are all positive divisors of n other than n itself.
6 has proper divisors of 1, 2, and 3. 1 + 2 + 3 = 6, so 6 is classed as a perfect number.
Calculate how many of the integers 1 to 20,000 (inclusive) are in each of the three classes. Show the results here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the jq programming language
Source code in the jq programming language
# 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;
def sum(stream): reduce stream as $i (0; . + $i);
def classify:
. as $n
| sum(proper_divisors)
| if . < $n then "deficient" elif . == $n then "perfect" else "abundant" end;
reduce (range(1; 20001) | classify) as $c ({}; .[$c] += 1 )
$ jq -n -c -f AbundantDeficientPerfect.jq
{"deficient":15043,"perfect":4,"abundant":4953}
You may also check:How to resolve the algorithm Sieve of Pritchard step by step in the Julia programming language
You may also check:How to resolve the algorithm Null object step by step in the Pascal programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Ursalang programming language
You may also check:How to resolve the algorithm Calendar - for REAL programmers step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Temperature conversion step by step in the ML programming language