Added number generators
This commit is contained in:
138
src/main/java/com/mattrixwv/ArrayAlgorithms.java
Normal file
138
src/main/java/com/mattrixwv/ArrayAlgorithms.java
Normal file
@@ -0,0 +1,138 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/ArrayAlgorithms.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-03-21
|
||||
//Modified: 06-25-22
|
||||
//This class contains algorithms for vectors that I've found it useful to keep around
|
||||
/*
|
||||
Copyright (C) 2022 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/>.
|
||||
*/
|
||||
package com.mattrixwv;
|
||||
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
|
||||
public class ArrayAlgorithms{
|
||||
private ArrayAlgorithms(){}
|
||||
//This function returns the sum of all elements in the list
|
||||
public static int getSum(Iterable<Integer> nums){
|
||||
//Setup the variables
|
||||
int sum = 0;
|
||||
|
||||
//Loop through every element in the list and add them together
|
||||
for(int num : nums){
|
||||
sum += num;
|
||||
}
|
||||
|
||||
//Return the sum of all elements
|
||||
return sum;
|
||||
}
|
||||
public static long getLongSum(Iterable<Long> nums){
|
||||
//Setup the variables
|
||||
long sum = 0;
|
||||
|
||||
//Loop through every element in the list and add them together
|
||||
for(long num : nums){
|
||||
sum += num;
|
||||
}
|
||||
|
||||
//Return the sum of all elements
|
||||
return sum;
|
||||
}
|
||||
public static BigInteger getBigSum(Iterable<BigInteger> nums){
|
||||
//Setup the variables
|
||||
BigInteger sum = BigInteger.ZERO;
|
||||
|
||||
//Loop through every element in the list and add them together
|
||||
for(BigInteger num : nums){
|
||||
sum = sum.add(num);
|
||||
}
|
||||
|
||||
//Return the sum of all elements
|
||||
return sum;
|
||||
}
|
||||
//This function returns the product of all elements in the list
|
||||
public static int getProd(Iterable<Integer> nums){
|
||||
//If a blank list was passed tot he fuction return 0 as the product
|
||||
if(!nums.iterator().hasNext()){
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Setup the variables
|
||||
int product = 1; //Start at 1 because x * 1 = x
|
||||
|
||||
//Loop through every element in the list and multiply them together
|
||||
for(int num : nums){
|
||||
product *= num;
|
||||
}
|
||||
|
||||
//Return the product of all elements
|
||||
return product;
|
||||
}
|
||||
public static long getLongProd(Iterable<Long> nums){
|
||||
//If a blank list was passed tot he fuction return 0 as the product
|
||||
if(!nums.iterator().hasNext()){
|
||||
return 0L;
|
||||
}
|
||||
|
||||
//Setup the variables
|
||||
long product = 1L; //Start at 1 because x * 1 = x
|
||||
|
||||
//Loop through every element in the list and multiply them together
|
||||
for(long num : nums){
|
||||
product *= num;
|
||||
}
|
||||
|
||||
//Return the product of all elements
|
||||
return product;
|
||||
}
|
||||
public static BigInteger getBigProd(Iterable<BigInteger> nums){
|
||||
//If a blank list was passed tot he fuction return 0 as the product
|
||||
if(!nums.iterator().hasNext()){
|
||||
return BigInteger.valueOf(0);
|
||||
}
|
||||
|
||||
//Setup the variables
|
||||
BigInteger product = BigInteger.valueOf(1); //Start at 1 because x * 1 = x
|
||||
|
||||
//Loop through every element in the list and multiply them together
|
||||
for(BigInteger num : nums){
|
||||
product = product.multiply(num);
|
||||
}
|
||||
|
||||
//Return the product of all elements
|
||||
return product;
|
||||
}
|
||||
//Print a list
|
||||
public static <T> String printList(Iterable<T> list){
|
||||
StringJoiner returnString = new StringJoiner(", ", "[", "]");
|
||||
for(T obj : list){
|
||||
returnString.add(obj.toString());
|
||||
}
|
||||
return returnString.toString();
|
||||
}
|
||||
//Convert lists
|
||||
public static List<Integer> longToInt(List<Long> originalList){
|
||||
ArrayList<Integer> newList = new ArrayList<>(originalList.size());
|
||||
for(Long num : originalList){
|
||||
newList.add(num.intValue());
|
||||
}
|
||||
return newList;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user