mirror of
https://bitbucket.org/Mattrixwv/csclasses.git
synced 2025-12-06 18:23:58 -05:00
Added getAllFib functions
This commit is contained in:
99
CSClasses/Algorithms.cs
Normal file
99
CSClasses/Algorithms.cs
Normal file
@@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
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<int> getAllFib(int goalNumber){
|
||||
//Setup the variables
|
||||
List<int> fibNums = new List<int>();
|
||||
|
||||
//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<long> getAllFib(long goalNumber){
|
||||
//Setup the variables
|
||||
List<long> fibNums = new List<long>();
|
||||
|
||||
//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<BigInteger> getAllFib(BigInteger goalNumber){
|
||||
//Setup the variables
|
||||
List<BigInteger> fibNums = new List<BigInteger>();
|
||||
|
||||
//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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user