From 1b71bab4f4b425a88e5f8fe5e7d0256fb9652b10 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sun, 23 Aug 2020 02:54:28 -0400 Subject: [PATCH] Added getAllFib functions --- CSClasses/Algorithms.cs | 99 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 CSClasses/Algorithms.cs diff --git a/CSClasses/Algorithms.cs b/CSClasses/Algorithms.cs new file mode 100644 index 0000000..0e1e82b --- /dev/null +++ b/CSClasses/Algorithms.cs @@ -0,0 +1,99 @@ +//C#/CSClasses/Algorithms.cs +//Matthew Ellison +// Created: 08-23-20 +//Modified: 08-23-20 +//This file contains a class that is used to time the execution time of other programs +/* +Copyright (C) 2020 Matthew Ellison + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + + +using System; +using System.Collections.Generic; +using System.Numerics; + + +namespace mee{ + public class Algorithms{ + //These functions return a list of all Fibonacci numbers <= goalNumber + public static List getAllFib(int goalNumber){ + //Setup the variables + List fibNums = new List(); + + //If the number is <= 0 return an empty list + if(goalNumber <= 0){ + return fibNums; + } + + //This means that at least 2 1's are elements + fibNums.Add(1); + fibNums.Add(1); + + //Loop to generate the rest of the Fibonacci numbers + while(fibNums[fibNums.Count - 1] <= goalNumber){ + fibNums.Add(fibNums[fibNums.Count - 1] + fibNums[fibNums.Count - 2]); + } + + //At this point the most recent number is > goalNumber, so remove it and return the rest of the list + fibNums.RemoveAt(fibNums.Count - 1); + return fibNums; + } + public static List getAllFib(long goalNumber){ + //Setup the variables + List fibNums = new List(); + + //If the number is <= 0 return an empty list + if(goalNumber <= 0){ + return fibNums; + } + + //This means that at least 2 1's are elements + fibNums.Add(1); + fibNums.Add(1); + + //Loop to generate the rest of the Fibonacci numbers + while(fibNums[fibNums.Count - 1] <= goalNumber){ + fibNums.Add(fibNums[fibNums.Count - 1] + fibNums[fibNums.Count - 2]); + } + + //At this point the most recent number is > goalNumber, so remove it and return the rest of the list + fibNums.RemoveAt(fibNums.Count - 1); + return fibNums; + } + public static List getAllFib(BigInteger goalNumber){ + //Setup the variables + List fibNums = new List(); + + //If the number is <= 0 return am empty list + if(goalNumber <= 0){ + return fibNums; + } + + //This means that at least 2 1's are elements + fibNums.Add(1); + fibNums.Add(1); + + //Loop to generate the rest of Fibonacci numbers + while(fibNums[fibNums.Count - 1] <= goalNumber){ + fibNums.Add(fibNums[fibNums.Count - 1] + fibNums[fibNums.Count - 2]); + } + + //At this point the most recent number is > goalNumber, so remove it and return the rest of the list + fibNums.RemoveAt(fibNums.Count - 1); + return fibNums; + } + } +}