How to resolve the algorithm Magnanimous numbers step by step in the jq programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Magnanimous numbers step by step in the jq programming language
Table of Contents
Problem Statement
A magnanimous number is an integer where there is no place in the number where a + (plus sign) could be added between any two digits to give a non-prime sum.
Traditionally the single digit numbers 0 through 9 are included as magnanimous numbers as there is no place in the number where you can add a plus between two digits at all. (Kind of weaselly but there you are...) Except for the actual value 0, leading zeros are not permitted. Internal zeros are fine though, 1001 -> 1 + 001 (prime), 10 + 01 (prime) 100 + 1 (prime). There are only 571 known magnanimous numbers. It is strongly suspected, though not rigorously proved, that there are no magnanimous numbers above 97393713331910, the largest one known.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Magnanimous numbers step by step in the jq programming language
Source code in the jq programming language
# To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
def divrem($x; $y):
[$x/$y|floor, $x % $y];
def ismagnanimous:
. as $n
| if $n < 10 then true
else first(range( 1; tostring|length) as $i
| divrem($n; (10|power($i))) as [$q, $r]
| if ($q + $r) | is_prime == false then 0 else empty end)
// true
| . == true
end;
# An unbounded stream ...
def magnanimous:
range(0; infinite)
| select(ismagnanimous);
[limit(400; magnanimous)]
| "First 45 magnanimous numbers:", .[:45],
"\n241st through 250th magnanimous numbers:", .[241:251],
"\n391st through 400th magnanimous numbers:", .[391:]
You may also check:How to resolve the algorithm File modification time step by step in the C# programming language
You may also check:How to resolve the algorithm Truncatable primes step by step in the Elena programming language
You may also check:How to resolve the algorithm Loops/While step by step in the Vim Script programming language
You may also check:How to resolve the algorithm Pythagorean triples step by step in the Factor programming language
You may also check:How to resolve the algorithm Extensible prime generator step by step in the JavaScript programming language