![JAR search and dependency download from the Maven repository](/logo.png)
ru.greatbit.utils.math.Sequence Maven / Gradle / Ivy
package ru.greatbit.utils.math;
import ru.greatbit.utils.exceptions.MathException;
import java.util.LinkedList;
import java.util.List;
/**
* Created by azee on 5/15/14.
*/
public class Sequence {
/**
* Get factorial of defined depth
* @param depth
* @return
*/
public static long factorial(int depth) throws MathException {
if (depth <= 0){
throw new MathException("Factorial is not supported for values less or equal to 0");
}
if (depth == 1){
return 1;
}
return factorial(depth - 1) * depth;
}
/**
* Get a sequence of factorial numbers of defined depth
* @param depth
* @return
*/
public static List factorialSequence(int depth) throws MathException {
List result = new LinkedList();
if (depth <= 0){
throw new MathException("Factorial is not supported for values less or equal to 0");
}
factorialSequence(depth, result);
return result;
}
/**
* Fill factorial sequence recursively
* @param depth
* @param values
*/
private static void factorialSequence(int depth, List values){
if (depth == 1){
values.add(1L);
return;
}
factorialSequence(depth - 1, values);
values.add(values.get(values.size() - 1) * depth);
}
/**
* Get fibonacchi number of defined depth
* @param depth
* @return
*/
public static long fibonacci(int depth) throws MathException {
if (depth <= 0) {
throw new MathException("Fibonacci is not supported for values less or equal to 0");
} else if (depth == 1 || depth == 2) {
return 1;
} else {
return fibonacci(depth - 1) + fibonacci(depth - 2);
}
}
/**
* Get a sequence of fibonacci numbers of defined depth
* @param depth
* @return
* @throws MathException
*/
public static List fibonacciSequence(int depth) throws MathException {
List result = new LinkedList();
if (depth <= 0){
throw new MathException("Factorial is not supported for values less or equal to 0");
}
fibonacciSequence(depth, result);
return result;
}
/**
* Fill fibonacci sequence recursively
* @param depth
* @param result
*/
private static void fibonacciSequence(int depth, List result) {
if (depth == 1) {
result.add(1L);
return;
}
if (depth == 2) {
result.add(1L);
result.add(1L);
return;
}
fibonacciSequence(depth - 1, result);
result.add(result.get(result.size() - 1) + result.get(result.size() - 2));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy