I have an array of the first 50 Fibonacci numbers in the picture. Your program should ask the user for a number between 5 and 21. Using that input as a location in the array, with that location and the next three locations (for example base, base+1, base+2 and base+3) you will multiply the outer values, base and base+3, (the first and last of the four). Do the same for the inner values (base+1 and base+2 multiplied) and then double that value. These two values form the two sides of a right triangle. Now find the hypotenuse. Remember the Pythagorean theorem, which says a2 + b2 = c2. Our two sides are a and b, so we square each of them (remember a * a = a2, so just multiply each value by itself) and add them together. That gives you c2. Now all you need do is take the square root of that sum. Given that the sqrt method normally returns a floating-point value (a double), we must force it to return a long integer for us in this case, so use the following line (with your own variable names of course) to get the square root. long hypot = (long)Math.sqrt((leg1*leg1) + (leg2*leg2)); Search your Fibonacci array for the hypotenuse value. Your final output will be the value entered, the four Fibonacci numbers in the sequence starting at that location, the length of each leg, the length of the hypotenuse, and the index number (position) of the hypotenuse value in the Fibonacci array. (And yes, the hypotenuse of the triangle will also be a Fibonacci number.) Here is a sample output for a user entering the value 4: (Numbers greater than 21 will not be in your array.) Enter a number between 5 and 21: 4 5 8 13 21 leg 1 = 105, leg 2 = 208 and the hypotenuse = 233, which is Fibonacci positio
I have an array of the first 50 Fibonacci numbers in the picture.
Your program should ask the user for a number between 5 and 21. Using that input as a location in the array, with that location and the next three locations (for example base, base+1, base+2 and base+3) you will multiply the outer values, base and base+3, (the first and last of the four). Do the same for the inner values (base+1 and base+2 multiplied) and then double that value.
These two values form the two sides of a right triangle. Now find the hypotenuse. Remember the Pythagorean theorem, which says a2 + b2 = c2. Our two sides are a and b, so we square each of them (remember a * a = a2, so just multiply each value by itself) and add them together. That gives you c2. Now all you need do is take the square root of that sum.
Given that the sqrt method normally returns a floating-point value (a double), we must force it to return a long integer for us in this case, so use the following line (with your own variable names of course) to get the square root.
long hypot = (long)Math.sqrt((leg1*leg1) + (leg2*leg2));
Search your Fibonacci array for the hypotenuse value. Your final output will be the value entered, the four Fibonacci numbers in the sequence starting at that location, the length of each leg, the length of the hypotenuse, and the index number (position) of the hypotenuse value in the Fibonacci array. (And yes, the hypotenuse of the triangle will also be a Fibonacci number.)
Here is a sample output for a user entering the value 4: (Numbers greater than 21 will not be in your array.)
Enter a number between 5 and 21: 4
5 8 13 21
leg 1 = 105, leg 2 = 208 and the hypotenuse = 233, which is Fibonacci position 12
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images