SOFTENG 282 - Assignment 2
The Fibonacci GCD Calculator
With two positive integers A and B given by the user, this program calculates the greatest common divisor (GCD) of the sum of the first A Fibonacci numbers and the sum of the first B Fibonacci numbers.
It is recommended to view this README.md file in the github repos: https://github.com/InZaneManInAZuite/Fibonacci-GCD-Calculator
Copies of the images will be on the images directory.
Clone this repository to your device using git. Select a new folder you want to place the repository into by going into the command line and inputting "cd (new folder directory)" then using "git clone (link to repository)". Link to the repository: https://github.com/InZaneManInAZuite/Fibonacci-GCD-Calculator
or - simply download the FibonacciGCD.jar file into your device. Then follow instructions on how to use the program. This can be done by opening the link to the repository in your browser. Then, selecting the FibonacciGCD.jar file. At the top right of your browser will be three dots which if you click would show you the option to download the file.
The code and an outline on how the program works in general (Especially regarding the front end commands menu and other displays) can be seen on the SOFTENG282Assign2Doc.pdf, which is a documentation of the classes made for the program, including the tests.
The solution to the task:
The back end processes are mediated by the FibonacciGCD class in the code. To simplify the coding process, the method that actually looks for the GCD (Greatest Common Divisor) of the sum of the first A and first B Fibonacci numbers called computeGCD() calls on two different methods. This essentially splits the task into two parts. A method which looks for the sum of all the first elements of a Fibonacci sequence of a certain length, and a method which looks for the GCD of two numbers.
The method that looks for the sum of Fibonacci numbers is the sumOfFirstNFibonacciNumbers(). This method takes in an integer input n, and outputs an integer. This utilizes three main integer variables called a, b, and sum; and a for loop.
This algorithm works as follows:
Initialize the variables with a and sum starting at 0 and b starting at 1. This sets a and b equal to the first two elements of a Fibonacci sequence.
Inside a for loop that tracks how many numbers have been added to the sum, ei the variable n, first add the element within a to sum.
Then a temporary variable temp is set to equal to a.
Then a is changed to be b;
Then b is changed to be the sum of the new a and temp. This essentially changes a and b to be the next consecutive pair of numbers in the Fibonacci sequence. And thus when the for loop has looped n times the first N fibonacci numbers have been added to sum.
NOTE: On its own, if the input is non-positive, the resulting sum will be 0. This will be representative of summing no Fibonacci number as there is no "0th" or below Fibonacci number.
NOTE: The entire program will reject user inputs that are non-positive and will ask the user to input a new input.
Lastly return the sum variable.
The method that looks for the GCD of two numbers is the getGCD(). This method takes in two integer input a and b, and outputs an integer. This directly follows the Euclidean Algorithm as a recurssive method.
Euclidean Algorithm works out the GCD by expressing the two numbers A and B, where A is larger, as A = Bn + C for some integer n, with C being a remainder. Then repeating this for B and C until eventually the remainder becomes equal to 0. In such case, the "B" when the remainder is 0 is shown to be the GCD of the initial input A and B. This repetitiveness can be turned into a recursive method.
First to make it similar to how Euclidean Algorithm is done by hand, we set a to be greater than b.
To do so if a is less than b, we set a temporary variable temp equal to a.
Then set a equal to b.
And finally, set b equal to temp. Switching the values of input a and b. Remember all numbers in the Fibonacci Sequence are non-negative, so let's not expect any negative inputs.
Then as a base case, if b is equal to 0, we return a.
Then for the recursive case, we return what is outputted when we input to itself b and a mod b. Remember a mod b essentially is equal to the remainder just like how C is in A = Bn + C.
To reiterate, the full algorithm looks for the GCD of the sum of the first A and first B Fibonacci numbers called in the program as the method computeGCD(). This method takes in two integer input called firstFibonacciSeries and secondFibonacciSeries, and outputs an integer. Let us call these two inputs A and B for now.
First, it obtains the sum of the first A fibonacci numbers by calling sumOfFirstNFibonacciNumbers(). Let's call this sumA.
Then repeats this process for B and call it sumB.
Lastly we find the GCD between sumA and sumB by calling getGCD().
NOTE: All actual code from the entire program may differ slightly from the explaination on how it works. These differences are simply made to print out into the command line certain information.
Open the command line and set the current directory to the location of the FibonacciGCD.jar file. Then use the "java -jar FibonacciGCD.jar" in the command line to open the file.
In cases where the user is unable to use a .jar file, use instead from the same directory: "java src/main/java/Main.java". This method would require that you installed the entire repository rather than just the jar file.
When the program starts running, a menu screen similar to one below can be seen:
At the end of this screen we see arrows ">>" where the user can input "CALCULATE" or "EXIT".
NOTE: These commands ignore cases, thus the user can simply write commands in lower case.
NOTE: Please mind the spelling of the commands.
If the CALCULATE command is selected, the program asks for an input for A and B. This is to calculate the GCD of the sum of the first A and B Fibonacci numbers.
If the input is invalid, such as if the input is not a number, or is not greater than zero, the program will ask the user repeatedly until a valid input A is given.
NOTE: The max input possible is 47. Inputting 48 or above will make the program ask the user for valid input once again as input 48 and above causes an integer overflow.
When two valid inputs A and B have been given by the user, the first A fibonacci numbers and the first B fibonacci numbers are shown along with the sum of those collection of numbers. Then, the program then showns what the GCD (Greatest Common Divisor) of these two sums are.
NOTE: Once the input values exceeds 11, the text display for the fibonacci is shortend to have a ... and only shows the last number on the sequence afterwards.
NOTE: If A and B inputted are equal to each other, the sequence of numbers and the sum will only appear once.
The program then asks the user to press enter to continue using the program. Pressing enter will bring back the commands menu. From here, the user can use the program once again to calculate the GCD of the sum of the first A and B Fibonacci numbers or exit the program.
If the user chose the EXIT command, the program displays some texts and the program ends.
If the user inputs an invalid command, it repeats the command menu and asks the user for a command until a valid command is given.
This is made by Toshiro Mendoza for Assignment 2 in SOFTENG 282.










