How to resolve the algorithm Weird numbers step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Weird numbers step by step in the jq programming language

Table of Contents

Problem Statement

In number theory, a weird number is a natural number that is abundant but not semiperfect (and therefore not perfect either). In other words, the sum of the proper divisors of the number (divisors including 1 but not itself) is greater than the number itself (the number is abundant), but no subset of those divisors sums to the number itself (the number is not semiperfect). For example:

Find and display, here on this page, the first 25 weird numbers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Weird numbers 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;

# Is n semiperfect given that divs are the proper divisors
def semiperfect(n; divs):
  (divs|length) as $le
  | if $le == 0 then false
    else divs[0] as $h
    | if n == $h then true
      elif $le == 1 then false
      else  divs[1:] as $t
      | if n < $h then semiperfect(n; $t)
        else semiperfect(n-$h; $t) or semiperfect(n; $t)
	end
      end
    end ;

def sieve(limit):
    # 'false' denotes abundant and not semi-perfect.
    # Only interested in even numbers >= 2
    (reduce range(6; limit; 6) as $j ([]; .[$j] = true)) # eliminates multiples of 3
    | reduce range(2; limit; 2) as $i (.;
        if (.[$i]|not)
        then [$i|proper_divisors] as $divs
        | ($divs | add) as $sum
        | if $sum <= $i
          then .[$i] = true
          elif (semiperfect($sum-$i; $divs))
          then reduce range($i; limit; $i) as $j (.; .[$j] = true)
          else .
          end
	else .
	end) ;

# Print up to $max weird numbers based on the given sieve size, $limit.
def task($limit; $max): 
  sieve($limit) as $w
  | def weirds:
      range(2; $w|length; 2) | select($w[.]|not);

      # collect into an array for ease of counting
      [limit($max; weirds)]
      | "The first \(length) weird numbers are:", . ;

# The parameters should be set on the command line:
task($sieve; $limit)

  

You may also check:How to resolve the algorithm Function definition step by step in the Yorick programming language
You may also check:How to resolve the algorithm Program termination step by step in the Delphi programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the BASIC programming language
You may also check:How to resolve the algorithm Abbreviations, automatic step by step in the Rust programming language
You may also check:How to resolve the algorithm Operator precedence step by step in the REALbasic programming language