Disarium numbers: Difference between revisions
Content added Content deleted
(Moved VTL-2 to the write place) |
imported>Brain Soup No edit summary |
||
Line 2,254: | Line 2,254: | ||
{{out}} |
{{out}} |
||
<pre> 0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798</pre> |
<pre> 0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798</pre> |
||
=={{header|Odin}}== |
|||
<syntaxhighlight lang="Go"> |
|||
package disarium |
|||
import "core:fmt" |
|||
import "core:math" |
|||
/* main block start */ |
|||
main :: proc() { |
|||
fmt.print("\nThe first 18 Disarium numbers are:") |
|||
count, i: int |
|||
for count < 19 { |
|||
if is_disarium(i) { |
|||
fmt.print(" ", i) |
|||
count += 1 |
|||
} |
|||
i += 1 |
|||
} |
|||
fmt.println("") |
|||
} /* main block end */ |
|||
/* proc definitions */ |
|||
power :: proc(base, exponent: int) -> int { |
|||
result := 1 |
|||
for _ in 1 ..= exponent { |
|||
result *= base |
|||
} |
|||
return result |
|||
} |
|||
is_disarium :: proc(num: int) -> bool { |
|||
n := num |
|||
sum := 0 |
|||
len := n <= 9 ? 1 : cast(int)math.floor_f64(math.log10_f64(auto_cast n) + 1) |
|||
for n > 0 { |
|||
sum += power(n % 10, len) |
|||
n /= 10 |
|||
len -= 1 |
|||
} |
|||
return num == sum |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The first 18 Disarium numbers are: 0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798 |
|||
</pre> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |