/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/maths/LucasSeries.java
38 строк
971 B
Piotr Idzik
style: enable `HideUtilityClassConstructor` in checkstyle (#5147)
08 май 2024, 09:58
Не верифицирован
08 май 2024, 09:58
d3bb691
Код
Авторство
О чём код?
package com.thealgorithms.maths; /** * https://en.wikipedia.org/wiki/Lucas_number */ public final class LucasSeries { private LucasSeries() { } /** * Calculate nth number of Lucas Series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76, * 123, ....) using recursion * * @param n nth * @return nth number of Lucas Series */ public static int lucasSeries(int n) { return n == 1 ? 2 : n == 2 ? 1 : lucasSeries(n - 1) + lucasSeries(n - 2); } /** * Calculate nth number of Lucas Series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76, * 123, ....) using iteration * * @param n nth * @return nth number of lucas series */ public static int lucasSeriesIteration(int n) { int previous = 2; int current = 1; for (int i = 1; i < n; i++) { int next = previous + current; previous = current; current = next; } return previous; } }