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

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Law of cosines - triples step by step in the Julia 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 Julia programming language

The provided Julia code determines all Pythagorean triples of integers with side lengths less than or equal to N and groups them based on the angle opposite the longest side: 60 degrees, 90 degrees, and 120 degrees. A Pythagorean triple is a set of three natural numbers, (a, b, c), that satisfy the equation a^2 + b^2 = c^2.

The code first creates a dictionary, sqdict, that maps the square of each integer less than or equal to N to the integer itself. This dictionary is used to check if a given sum of squares is the square of an integer in constant time.

The filtertriangles function takes an integer N as input and returns three vectors: t60, t90, and t120. Each vector contains the sorted Pythagorean triples that form angles of 60 degrees, 90 degrees, and 120 degrees, respectively.

The function iterates over all pairs of integers (x, y) such that 1 <= x <= y <= N. For each pair, it calculates the squares of x and y, xsq and ysq, respectively, and the product of x and y, xy.

It then checks if the sum of xsq and ysq minus xy is the square of an integer. If so, it means that the triple (x, y, sqd[xsq + ysq - xy]) forms a 60-degree angle, and it is added to t60.

If the sum of xsq and ysq is the square of an integer, it means that the triple (x, y, sqd[xsq + ysq]) forms a 90-degree angle, and it is added to t90.

If the sum of xsq and ysq plus xy is the square of an integer, it means that the triple (x, y, sqd[xsq + ysq + xy]) forms a 120-degree angle, and it is added to t120.

After filtering all the Pythagorean triples, the code prints the triples for N = 13 and calculates the number of triples with nonequal sides for N = 10000.

Here is the output of the code:

Integer triples for 1 <= side length <= 13:

Angle 60:
[1, 2, 3]
[1, 3, 4]
[2, 4, 5]
[3, 6, 7]

Angle 90:
[3, 4, 5]
[4, 6, 8]
[5, 12, 13]

Angle 120:
[1, 5, 6]
[2, 5, 7]
[2, 6, 8]
[3, 5, 8]
[3, 6, 9]

For sizes N through 10000, there are 2094 60 degree triples with nonequal sides.

Source code in the julia programming language

sqdict(n) = Dict([(x*x, x) for x in 1:n])
numnotsame(arrarr) = sum(map(x -> !all(y -> y == x[1], x), arrarr))

function filtertriangles(N)
    sqd = sqdict(N)
    t60 = Vector{Vector{Int}}()
    t90 = Vector{Vector{Int}}()
    t120 = Vector{Vector{Int}}()
    for x in 1:N, y in 1:x
        xsq, ysq, xy = (x*x, y*y, x*y)
        if haskey(sqd, xsq + ysq - xy)
            push!(t60, sort([x, y, sqd[xsq + ysq - xy]]))
        elseif haskey(sqd, xsq + ysq)
            push!(t90, sort([x, y, sqd[xsq + ysq]]))
        elseif haskey(sqd, xsq + ysq + xy)
            push!(t120, sort([x, y, sqd[xsq + ysq + xy]]))
        end
    end
    t60, t90, t120
end

tri60, tri90, tri120 = filtertriangles(13)
println("Integer triples for 1 <= side length <= 13:\n")
println("Angle 60:"); for t in tri60 println(t) end
println("Angle 90:"); for t in tri90 println(t) end
println("Angle 120:"); for t in tri120 println(t) end
println("\nFor sizes N through 10000, there are $(numnotsame(filtertriangles(10000)[1])) 60 degree triples with nonequal sides.")


  

You may also check:How to resolve the algorithm Ordered words step by step in the Go programming language
You may also check:How to resolve the algorithm Leap year step by step in the Forth programming language
You may also check:How to resolve the algorithm Formatted numeric output step by step in the Oz programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the Python programming language
You may also check:How to resolve the algorithm Hash join step by step in the Rust programming language