/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/maths/SumOfDigits.java
45 строк
1 KB
Piotr Idzik
Cleanup `SumOfDigits` and its tests (#4994)
06 янв 2024, 00:05
Не верифицирован
06 янв 2024, 00:05
8930ab5
Код
Авторство
О чём код?
package com.thealgorithms.maths; public final class SumOfDigits { private SumOfDigits() { } /** * Calculate the sum of digits of a number * * @param number the number contains digits * @return sum of digits of given {@code number} */ public static int sumOfDigits(int number) { final int base = 10; number = Math.abs(number); int sum = 0; while (number != 0) { sum += number % base; number /= base; } return sum; } /** * Calculate the sum of digits of a number using recursion * * @param number the number contains digits * @return sum of digits of given {@code number} */ public static int sumOfDigitsRecursion(int number) { final int base = 10; number = Math.abs(number); return number < base ? number : number % base + sumOfDigitsRecursion(number / base); } /** * Calculate the sum of digits of a number using char array * * @param number the number contains digits * @return sum of digits of given {@code number} */ public static int sumOfDigitsFast(final int number) { return String.valueOf(Math.abs(number)).chars().map(c -> c - '0').reduce(0, Integer::sum); } }