How to resolve the algorithm Numbers which are the cube roots of the product of their proper divisors step by step in the jq programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Numbers which are the cube roots of the product of their proper divisors step by step in the jq programming language
Table of Contents
Problem Statement
Consider the number 24. Its proper divisors are: 1, 2, 3, 4, 6, 8 and 12. Their product is 13,824 and the cube root of this is 24. So 24 satisfies the definition in the task title. Compute and show here the first 50 positive integers which are the cube roots of the product of their proper divisors. Also show the 500th and 5,000th such numbers. Compute and show the 50,000th such number. OEIS considers 1 to be the first number in this sequence even though, strictly speaking, it has no proper divisors. Please therefore do likewise.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Numbers which are the cube roots of the product of their proper divisors step by step in the jq programming language
Source code in the jq programming language
# Notice that `prod(empty)` evaluates to 1.
def prod(s): reduce s as $x (1; . * $x);
# Output: the unordered stream of proper divisors of .
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;
# Emit a stream beginning with 1 and followed by the integers that are
# cube-roots of their proper divisors
def numbers_being_cube_roots_of_their_proper_divisors:
range(1; infinite)
| select(prod(proper_divisors) == .*.*.);
# print first 50 and then the 500th, 5000th, and $limit-th
def harness(generator; $limit):
label $out
| foreach generator as $n (
{ numbers50: [],
count: 0 };
.emit = null
| .count += 1
| if .count > $limit
then break $out
else if .count <= 50
then .numbers50 += [$n]
else .
end
| if .count == 50
then .emit = .numbers50
elif .count | IN(500, 5000, $limit)
then .emit = "\(.count)th: \($n)"
else .
end
end )
| .emit // empty ;
"First 50 numbers which are the cube roots of the products of their proper divisors:",
harness(numbers_being_cube_roots_of_their_proper_divisors; 50000)
You may also check:How to resolve the algorithm Command-line arguments step by step in the DCL programming language
You may also check:How to resolve the algorithm Knapsack problem/0-1 step by step in the Tcl programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the Action! programming language
You may also check:How to resolve the algorithm Cut a rectangle step by step in the Tcl programming language
You may also check:How to resolve the algorithm Pragmatic directives step by step in the Erlang programming language