Narcissistic decimal number: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|Go}}: Add Go) |
||
Line 541: | Line 541: | ||
<pre> |
<pre> |
||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315] |
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315] |
||
</pre> |
|||
=={{header|Go}}== |
|||
Nothing fancy as it runs in a fraction of a second as-is. |
|||
<lang go>package main |
|||
import "fmt" |
|||
func narc(n int) []int { |
|||
power := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} |
|||
limit := 10 |
|||
result := make([]int, 0, n) |
|||
for x := 0; len(result) < n; x++ { |
|||
if x >= limit { |
|||
for i := range power { |
|||
power[i] *= i // i^m |
|||
} |
|||
limit *= 10 |
|||
} |
|||
sum := 0 |
|||
for xx := x; xx > 0; xx /= 10 { |
|||
sum += power[xx%10] |
|||
} |
|||
if sum == x { |
|||
result = append(result, x) |
|||
} |
|||
} |
|||
return result |
|||
} |
|||
func main() { |
|||
fmt.Println(narc(25)) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
[0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315] |
|||
</pre> |
</pre> |
||