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

Published on 12 May 2024 09:40 PM

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

Source code in the freebasic programming language

' version 03-03-2019
' compile with: fbc -s console

#Define max 13

#Define Format(_x)  Right("    " + Str(_x), 4)

Dim As UInteger a, b, c, a2, b2, c2, c60 , c90, c120
Dim As String s60, s90, s120

For a = 1 To max
    a2 = a * a
    For b = a To max
        b2 = b * b
        ' 60 degrees
        c2 = a2 + b * b - a * b
        c = Sqr(c2)
        If c * c = c2 AndAlso c <= max Then
            s60 += Format(a) + Format(b) + Format(c) + Chr(10, 13)
            c60 += 1
        End If
        ' 90 degrees
        c2 = a2 + b * b
        c = Sqr(c2)
        If c * c = c2 AndAlso c <= max Then
            s90 += Format(a) + Format(b) + Format(c) + Chr(10, 13)
            c90 += 1
        End If
        ' 120 degrees
        c2 = a2 + b * b + a * b
        c = Sqr(c2)
        If c * c = c2 AndAlso c <= max Then
            s120 += Format(a) + Format(b) + Format(c) + Chr(10, 13)
            c120 += 1
        End If
    Next
Next


Print Using "###: 60 degree triangles"; c60
Print s60
Print

Print Using "###: 90 degree triangles"; c90
Print s90
Print

Print Using "###: 120 degree triangles"; c120
Print s120
Print

#Undef max
#Define max 10000

c60 = 0
For a = 1 To max
    a2 = a * a
    For b = a +1 To max
        c2 = a2 + b * (b - a)
        c = Sqr(c2)
        If c * c = c2 AndAlso c <= max Then
            c60 += 1
        End If
    Next
Next

Print "For 60 degree triangles in the range [1, 10000]"
Print "There are "; c60; " triangles that have different length for a, b and c"

' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

  

You may also check:How to resolve the algorithm Split a character string based on change of character step by step in the Fortran programming language
You may also check:How to resolve the algorithm Tau function step by step in the jq programming language
You may also check:How to resolve the algorithm Perfect numbers step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the jq programming language
You may also check:How to resolve the algorithm Permutations by swapping step by step in the Arturo programming language