#!/usr/bin/perl -w

#################################################################################

# This Perl script calculates a million triangle hypotenuses with sides of length

# varying from 0.0 to 50.0. Some interesting statistics are then captured during

# the scripts runtime.

#

# The triangle takes creates two values, the length of Side "A" and

# the length of Side "B" to calculate teh hypotenuse using the

# Pythagorean Theorem: A^2 + B^2 = C^2

#################################################################################

srand ; # Intialize the random number generator

# Print out some messages

print ( "This is the awesome hypotenuse calculator Perl script.

" ) ;

print ( "The script will now create a million randomly sized

" ) ;

print ( "triangles with a,b values from 1 to fifty and calculate the average

" ) ;

print ( "hypotenuse size.

" ) ;

print ( "*******************************************************************

" ) ;

print ( "Please press a key to begin:" ) ;

<STDIN> ; # Grab a response from the user and begin!

$start_time = `time /t` ; # Capture start time using the Windows "time" command

chomp ( $start_time ) ; # Get rid of carriage return at end of line captured from the Windows "time" command

$total_a = 0 ; # Initialize Side A Length Totals in order to calculate an average

$total_b = 0 ; # Initialize Side B Length Totals in order to calculate an average

$total_hypot = 0 ; # Initialize the Hypotenuse Length in order to calculate an average

for ( $i = 0 ; $i < 1000000 ; $i ++ ) { # This is the loop that runs from zero to 999,999 before exiting -- running 1,000,000 times

$a = rand 50 ; # Calculate a random value for Side A - a value from 0 to 50.

$b = rand 50 ; # Calculate a random value for Side B - a value from 0 to 50.

if ( $i == 0 ) { # If this is the first time through the loop...

$smallest_a = $a ; # Then the smallest A is the current one.

$largest_a = $a ; # Then the largest A is the current one.

$smallest_b = $b ; # Then the smallest B is the current one.

$largest_b = $b ; # Then the largest B is the current one.

}

if ( $a < $smallest_a ) { # Check to see if the current Side A length is smaller than the current smallest Side A length.

$smallest_a = $a ; # If the current Side A length *is* smaller than the current record, then the current Side A is now the recode smallest Side A length

}

if ( $a > $largest_a ) { # Check to see if the current Side A length is larger than the current largest Side A length

$largest_a = $a ; # If the current Side A length *is* larger than the current record, then the current Side A is now the record largest Side A length

}

if ( $b < $smallest_b ) { # Check to see if the current Side B length is smaller than the current smallest Side B length.

$smallest_b = $b ; # If the current Side B length *is* smaller than the current record, then the current Side B is now the recode smallest Side B length

}

if ( $b > $largest_b ) { # If the current Side B length *is* larger than the current record, then the current Side B is now the record largest Side B length

$largest_b = $b ; # If the current Side B length *is* larger than the current record, then the current Side B is now the record largest Side B length

}

$hypot = sqrt ( $a ** 2 + $b ** 2 ) ; # Calculate the hypotenuse = square root of (A^2 + B^2)

if ( $i == 0 ) { # Is this the first time through the loop?

$smallest_hypot = $hypot ; # If so, then initialize the value of the smallest hypotenuse.

$largest_hypot = $hypot ; # If so, then initialize the value of the largest hypotenuse.

}

if ( $hypot < $smallest_hypot ) { # Is the current hypotenuse smaller than the current record smallest hypotenuse?

$smallest_hypot = $hypot ; # If so, then make the current hypotenuse the new record smallest hypotenuse.

}

if ( $hypot > $largest_hypot ) { # Is the current hypotenuse larger than the current record largest hypotenuse?

$largest_hypot = $hypot # If so, then make the current hypotenuse the new record largest hypotenuse.

}

$total_a = $total_a + $a ; # Add new Side A Length to total.

$total_b = $total_b + $b ; # Add new Side B Length to total.

$total_hypot = $total_hypot + $hypot ; # Adde new hypotenuse to total.

printf ( "Your hypotenuse with a=%9.5f and b=%9.5f is:%9.5f

" , $a , $b , $hypot ) ;

}

$end_time = `time /t` ; # Capture script ending time by using the Windows "time" command

chomp ( $end_time ) ; # Get rid of the carriage return at the end of the captured line from the Windows "time" command

print ( "

A total of $i triangles have been calculated.



" ) ;

$average_a = $total_a / $i ; # Calculate average Side A Length

$average_b = $total_b / $i ; # Calculate average Side B Length

$average_hypot = $total_hypot / $i ; # Calculate average hypotenuse length

printf ( "The smallest value for \" a \" is.......:%9.5f

" , $smallest_a ) ;

printf ( "The smallest value for \" b \" is.......:%9.5f

" , $smallest_b ) ;

printf ( "The smallest hypotenuse value is....:%9.5f



" , $smallest_hypot ) ;

printf ( "The largest value for \" a \" is........:%9.5f

" , $largest_a ) ;

printf ( "The largest value for \" b \" is........:%9.5f

" , $largest_b ) ;

printf ( "The largest hypotenuse value is.....:%9.5f



" , $largest_hypot ) ;

printf ( "The average value for \" a \" is........:%9.5f

" , $average_a ) ;

printf ( "The average value for \" b \" is........:%9.5f

" , $average_b ) ;

printf ( "The average hypotenuse value is.....:%9.5f



" , $average_hypot ) ;

print "

" ;

print "Script started at: $start_time

" ;