Own digits power sum: Difference between revisions
Content added Content deleted
(New post.) |
m (Minor code improvement.) |
||
Line 1,011: | Line 1,011: | ||
} |
} |
||
long |
long number = (long) Math.pow(10, n - 1); |
||
long |
long maximum = number * 10; |
||
int lastDigit = 0; |
int lastDigit = 0; |
||
int |
int digitSum = 0; |
||
List<Integer> digits; |
|||
while ( |
while ( number < maximum ) { |
||
if ( lastDigit == 0 ) { |
if ( lastDigit == 0 ) { |
||
digitSum = String.valueOf(number).chars() |
|||
.map(Character::getNumericValue) |
|||
.map( i -> powers.get(i) ) |
|||
.sum(); |
|||
} else if ( lastDigit == 1 ) { |
} else if ( lastDigit == 1 ) { |
||
digitSum += 1; |
|||
} else { |
} else { |
||
digitSum += powers.get(lastDigit) - powers.get(lastDigit - 1); |
|||
} |
} |
||
if ( |
if ( digitSum == number ) { |
||
System.out.println( |
System.out.println(number); |
||
if ( lastDigit == 0 ) { |
if ( lastDigit == 0 ) { |
||
System.out.println( |
System.out.println(number + 1); |
||
} |
} |
||
number += 10 - lastDigit; |
|||
lastDigit = 0; |
lastDigit = 0; |
||
} else if ( |
} else if ( digitSum > number ) { |
||
number += 10 - lastDigit; |
|||
lastDigit = 0; |
lastDigit = 0; |
||
} else if ( lastDigit < 9 ) { |
} else if ( lastDigit < 9 ) { |
||
number += 1; |
|||
lastDigit += 1; |
lastDigit += 1; |
||
} else { |
} else { |
||
number += 1; |
|||
lastDigit = 0; |
lastDigit = 0; |
||
} |
} |