Count in octal: Difference between revisions
Content added Content deleted
(More meaningful termination, as you can have >32 bit ints on a "32 bit" computer.) |
(Go solution) |
||
Line 115: | Line 115: | ||
end do |
end do |
||
end program</lang> |
end program</lang> |
||
=={{header|Go}}== |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"math" |
|||
) |
|||
func main() { |
|||
for i := int8(0); ; i++ { |
|||
fmt.Printf("%o\n", i) |
|||
if i == math.MaxInt8 { |
|||
break |
|||
} |
|||
} |
|||
}</lang> |
|||
Output: |
|||
<pre> |
|||
0 |
|||
1 |
|||
2 |
|||
3 |
|||
4 |
|||
5 |
|||
6 |
|||
7 |
|||
10 |
|||
11 |
|||
12 |
|||
... |
|||
175 |
|||
176 |
|||
177 |
|||
</pre> |
|||
Note that to use a different integer type, code must be changed in two places. Go has no way to query a type for its maximum value. Example: |
|||
<lang go>func main() { |
|||
for i := uint16(0); ; i++ { // type specified here |
|||
fmt.Printf("%o\n", i) |
|||
if i == math.MaxUint16 { // maximum value for type specified here |
|||
break |
|||
} |
|||
} |
|||
}</lang> |
|||
Output: |
|||
<pre> |
|||
... |
|||
177775 |
|||
177776 |
|||
177777 |
|||
</pre> |
|||
Big integers have no maximum value, but the Go runtime will panic when memory allocation fails. The deferred recover here allows the program to terminate silently should the program run until this happens. |
|||
<lang go>import ( |
|||
"big" |
|||
"fmt" |
|||
) |
|||
func main() { |
|||
defer func() { |
|||
recover() |
|||
}() |
|||
one := big.NewInt(1) |
|||
for i := big.NewInt(0); ; i.Add(i, one) { |
|||
fmt.Printf("%o\n", i) |
|||
} |
|||
}</lang> |
|||
Output: |
|||
<pre> |
|||
0 |
|||
1 |
|||
2 |
|||
3 |
|||
4 |
|||
5 |
|||
6 |
|||
7 |
|||
10 |
|||
11 |
|||
12 |
|||
13 |
|||
14 |
|||
... |
|||
</pre> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |