Upside-down numbers: Difference between revisions

Added Go
m (Uups ...undo removing header Python)
(Added Go)
Line 29:
;* [[oeis:A299539|OEIS:A299539 - Upside-down numbers]]
 
 
=={{header|Go}}==
{{trans|Python}}
{{libheader|Go-rcu}}
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"rcu"
)
 
func genUpsideDown(limit int) chan int {
ch := make(chan int)
wrappings := [][2]int{
{1, 9}, {2, 8}, {3, 7}, {4, 6}, {5, 5},
{6, 4}, {7, 3}, {8, 2}, {9, 1},
}
evens := []int{19, 28, 37, 46, 55, 64, 73, 82, 91}
odds := []int{5}
oddIndex := 0
evenIndex := 0
ndigits := 1
pow := 100
count := 0
go func() {
for count < limit {
if ndigits%2 == 1 {
if len(odds) > oddIndex {
ch <- odds[oddIndex]
count++
oddIndex++
} else {
// build next odds, but switch to evens
var nextOdds []int
for _, w := range wrappings {
for _, i := range odds {
nextOdds = append(nextOdds, w[0]*pow+i*10+w[1])
}
}
odds = nextOdds
ndigits++
pow *= 10
oddIndex = 0
}
} else {
if len(evens) > evenIndex {
ch <- evens[evenIndex]
count++
evenIndex++
} else {
// build next evens, but switch to odds
var nextEvens []int
for _, w := range wrappings {
for _, i := range evens {
nextEvens = append(nextEvens, w[0]*pow+i*10+w[1])
}
}
evens = nextEvens
ndigits++
pow *= 10
evenIndex = 0
}
}
}
close(ch)
}()
return ch
}
 
func main() {
const limit = 50_000_000
count := 0
var ud50s []int
pow := 50
for n := range genUpsideDown(limit) {
count++
if count < 50 {
ud50s = append(ud50s, n)
} else if count == 50 {
ud50s = append(ud50s, n)
fmt.Println("First 50 upside down numbers:")
rcu.PrintTable(ud50s, 10, 5, true)
fmt.Println()
pow = 500
} else if count == pow {
fmt.Printf("%sth : %s\n", rcu.Commatize(pow), rcu.Commatize(n))
pow *= 10
}
}
}</syntaxhighlight>
 
{{out}}
<pre>
First 50 upside down numbers:
5 19 28 37 46 55 64 73 82 91
159 258 357 456 555 654 753 852 951 1,199
1,289 1,379 1,469 1,559 1,649 1,739 1,829 1,919 2,198 2,288
2,378 2,468 2,558 2,648 2,738 2,828 2,918 3,197 3,287 3,377
3,467 3,557 3,647 3,737 3,827 3,917 4,196 4,286 4,376 4,466
 
500th : 494,616
5,000th : 56,546,545
50,000th : 6,441,469,664
500,000th : 729,664,644,183
5,000,000th : 82,485,246,852,682
50,000,000th : 9,285,587,463,255,281
</pre>
 
=={{header|Pascal}}==
Line 139 ⟶ 246:
User time: 0.310 s CPU share: 99.30 %
</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">""" rosettacode.org task Upside-down_numbers """
9,476

edits