How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the Scala 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 Scala 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 Scala programming language
Source code in the scala programming language
object FourRings {
def fourSquare(low: Int, high: Int, unique: Boolean, print: Boolean): Unit = {
def isValid(needle: Integer, haystack: Integer*) = !unique || !haystack.contains(needle)
if (print) println("a b c d e f g")
var count = 0
for {
a <- low to high
b <- low to high if isValid(a, b)
fp = a + b
c <- low to high if isValid(c, a, b)
d <- low to high if isValid(d, a, b, c) && fp == b + c + d
e <- low to high if isValid(e, a, b, c, d)
f <- low to high if isValid(f, a, b, c, d, e) && fp == d + e + f
g <- low to high if isValid(g, a, b, c, d, e, f) && fp == f + g
} {
count += 1
if (print) println(s"$a $b $c $d $e $f $g")
}
println(s"There are $count ${if(unique) "unique" else "non-unique"} solutions in [$low, $high]")
}
def main(args: Array[String]): Unit = {
fourSquare(1, 7, unique = true, print = true)
fourSquare(3, 9, unique = true, print = true)
fourSquare(0, 9, unique = false, print = false)
}
}
You may also check:How to resolve the algorithm 100 doors step by step in the Erlang programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the Elixir programming language
You may also check:How to resolve the algorithm Latin Squares in reduced form step by step in the Go programming language
You may also check:How to resolve the algorithm Read a configuration file step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the PL/I programming language