How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the Ruby programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the Ruby programming language

Table of Contents

Problem Statement

Replace       a, b, c, d, e, f,   and   g       with the decimal digits   LOW   ───►   HIGH such that the sum of the letters inside of each of the four large squares add up to the same sum. Show all output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the Ruby programming language

The provided Ruby code is an implementation of the famous Four Squares puzzle, which asks for ways to express a given number as a sum of four perfect squares. It primarily consists of nested f and four_squares functions that efficiently find and present the unique or non-unique solutions.

  1. f Function:

    • This is a lambda function that takes seven integer arguments: a, b, c, d, e, f, and g.
    • It checks if the sum of the first, second, third, and fourth arguments ([a+b, b+c+d, d+e+f, f+g]) contains only one unique value.
    • It returns true if the size of the uniq array is 1, and false otherwise. This condition ensures that f only evaluates to true when the sums are all identical.
  2. four_squares Function:

    • It is the primary function for finding the solutions to the puzzle.
    • It takes three arguments: low, high, unique, and show (optional).
    • low and high represent the lower and upper bounds for the search, while unique determines whether to find unique or non-unique solutions.
    • show controls whether to print the actual solutions.
    • It initializes uniq with the appropriate string based on the value of the unique argument, either "unique" or "non-unique".
    • It creates a range from low to high with [*low..high].
    • If unique is set to true, it uses permutation(7) to generate all possible permutations of 7 elements from the range, and filters them using the select method with the f function as the condition.
    • If unique is false, it uses repeated_permutation(7) instead, which allows for repeated elements in the permutations.
    • The result is assigned to the solutions variable.
    • If show is true, it prints the headings for the columns ("a" to "g") and iterates over each solutions array, printing its elements as rows.
    • Finally, it prints the total number of solutions found and the type (unique or non-unique) within the specified range.
  3. Usage:

    • The code calls four_squares twice with different input ranges: [1,7] and [3,9]. In both cases, it finds and prints the unique solutions.
    • It then calls four_squares a third time with [0,9] and false for unique, which finds and prints all non-unique solutions within that range.

Source code in the ruby programming language

def four_squares(low, high, unique=true, show=unique)
  f = -> (a,b,c,d,e,f,g) {[a+b, b+c+d, d+e+f, f+g].uniq.size == 1}
  if unique
    uniq = "unique"
    solutions = [*low..high].permutation(7).select{|ary| f.call(*ary)}
  else
    uniq = "non-unique"
    solutions = [*low..high].repeated_permutation(7).select{|ary| f.call(*ary)}
  end
  if show
    puts " " + [*"a".."g"].join("  ")
    solutions.each{|ary| p ary}
  end
  puts "#{solutions.size} #{uniq} solutions in #{low} to #{high}"
  puts
end

[[1,7], [3,9]].each do |low, high|
  four_squares(low, high)
end
four_squares(0, 9, false)


  

You may also check:How to resolve the algorithm Longest increasing subsequence step by step in the Pascal programming language
You may also check:How to resolve the algorithm Polymorphic copy step by step in the TXR programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/PCG32 step by step in the J programming language
You may also check:How to resolve the algorithm Balanced brackets step by step in the BQN programming language
You may also check:How to resolve the algorithm Extend your language step by step in the PHL programming language