You are viewing a single comment's thread. Return to all comments →
Modern solution using java 8 streams:
Long sum = 0L; for (char digit : n.toCharArray()) { sum += Character.getNumericValue(digit) * k; } while(sum.toString().length() > 1) { List<String> remainingDigits= String.valueOf(sum) .chars() .mapToObj(c -> String.valueOf((char) c)) .collect(Collectors.toList()); sum = remainingDigits.stream().mapToLong(Long::parseLong).reduce(0L, Long::sum); } return sum.intValue(); }
Seems like cookies are disabled on this browser, please enable them to open this website
Recursive Digit Sum
You are viewing a single comment's thread. Return to all comments →
Modern solution using java 8 streams: