How to resolve the algorithm Law of cosines - triples step by step in the Kotlin programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Law of cosines - triples step by step in the Kotlin programming language

Table of Contents

Problem Statement

The Law of cosines states that for an angle γ, (gamma) of any triangle, if the sides adjacent to the angle are A and B and the side opposite is C; then the lengths of the sides are related by this formula: For an angle of of   90º   this becomes the more familiar "Pythagoras equation": For an angle of   60º   this becomes the less familiar equation: And finally for an angle of   120º   this becomes the equation:

Note: Triangles with the same length sides but different order are to be treated as the same.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Law of cosines - triples step by step in the Kotlin programming language

Program Overview:

This Kotlin program finds solutions to Pythagorean triples (sets of three integers a, b, and c such that a^2 + b^2 = c^2) based on a given angle and maximum side length. It includes code to initialize a map of squares for efficient calculations and a function to solve for the triples.

Code Structure:

  1. Constants and Variables:

    • squares13 and squares10000 are mutable maps used to store squares of integers up to 13 and 10000, respectively, for quick lookup.
  2. Class:

    • Trio: A class representing a Pythagorean triple with three integers a, b, and c.
  3. Functions:

    • init(): Initializes the squares maps.

    • solve(angle, maxLen, allowSame):

      • Finds solutions based on the given angle, maximum length, and whether same-length sides are allowed.
      • Calculates the left-hand side of the Pythagorean equation based on the angle (60, 90, or 120 degrees).
      • Uses the squares maps to find the third side (c).
      • If allowSame is false, filters out solutions where all sides are equal.
  4. main Function:

    • Calls init() to initialize the squares maps.

    • Iterates over the angles 90, 60, and 120 degrees and finds solutions for each angle with a maximum length of 13, allowing same-length sides.

    • Finds solutions for an angle of 60 degrees with a maximum length of 10000, disallowing same-length sides.

    • Prints the number of solutions found for each case and displays the solutions as a list of triples.

Detailed Explanation:

  1. Initialization:

    • When the program runs, it initializes the squares maps with squares of integers up to 13 and 10000 to optimize the lookup of squares during the calculation.
  2. Solving for Pythagorean Triples:

    • The solve function takes an angle, maximum length, and a flag indicating whether same-length sides are allowed.
    • It iterates through the possible side lengths a and b up to the maximum length.
    • For each a and b, it computes the left-hand side of the Pythagorean equation (a * a + b * b).
    • It then adjusts the result based on the angle using the following rules:
      • Angle 60: Subtract a * b.
      • Angle 120: Add a * b.
    • It looks up the result in the appropriate squares map to find the third side (c).
    • If same-length sides are not allowed, it checks if a, b, and c are all equal and skips the solution if true.
    • Valid solutions are added to a mutable list.
  3. Main Logic:

    • In the main function:
      • It initializes the squares maps.
      • It iterates through the angles 90, 60, and 120 degrees and finds solutions for each with a maximum length of 13, allowing same-length sides.
      • It then finds solutions for an angle of 60 degrees with a maximum length of 10000, disallowing same-length sides.
      • It prints the number of solutions found for each case and displays the solutions as a list of triples.

Source code in the kotlin programming language

// Version 1.2.70

val squares13 = mutableMapOf<Int, Int>()
val squares10000 = mutableMapOf<Int, Int>()

class Trio(val a: Int, val b: Int, val c: Int) {
    override fun toString() = "($a $b $c)"
}

fun init() {
    for (i in 1..13) squares13.put(i * i, i)
    for (i in 1..10000) squares10000.put(i * i, i)
}

fun solve(angle :Int, maxLen: Int, allowSame: Boolean): List<Trio> {
    val solutions = mutableListOf<Trio>()
    for (a in 1..maxLen) {
        inner@ for (b in a..maxLen) {
            var lhs = a * a + b * b
            if (angle != 90) {
                when (angle) {
                    60   -> lhs -= a * b
                    120  -> lhs += a * b
                    else -> throw RuntimeException("Angle must be 60, 90 or 120 degrees")
                }
            }
            when (maxLen) {
                13 -> {
                    val c = squares13[lhs]
                    if (c != null) {
                        if (!allowSame && a == b && b == c) continue@inner
                        solutions.add(Trio(a, b, c))
                    }
                }

                10000 -> {
                    val c = squares10000[lhs]
                    if (c != null) {
                        if (!allowSame && a == b && b == c) continue@inner
                        solutions.add(Trio(a, b, c))
                    }
                }

                else -> throw RuntimeException("Maximum length must be either 13 or 10000")
            }
        }
    }
    return solutions
}

fun main(args: Array<String>) {
    init()
    print("For sides in the range [1, 13] ")
    println("where they can all be of the same length:-\n")
    val angles = intArrayOf(90, 60, 120)
    lateinit var solutions: List<Trio>
    for (angle in angles) {
        solutions = solve(angle, 13, true)
        print("  For an angle of ${angle} degrees")
        println(" there are ${solutions.size} solutions, namely:")
        println("  ${solutions.joinToString(" ", "[", "]")}\n")
    }
    print("For sides in the range [1, 10000] ")
    println("where they cannot ALL be of the same length:-\n")
    solutions = solve(60, 10000, false)
    print("  For an angle of 60 degrees")
    println(" there are ${solutions.size} solutions.")
}


  

You may also check:How to resolve the algorithm Next highest int from digits step by step in the C++ programming language
You may also check:How to resolve the algorithm Loops/Wrong ranges step by step in the Maple programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the Arturo programming language
You may also check:How to resolve the algorithm Maze solving step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Church numerals step by step in the Tailspin programming language