Ulam numbers: Difference between revisions

Added Go
(Added Wren)
(Added Go)
Line 11:
;References:
*   [[oeis:A002858|OEIS Ulam numbers]]
 
=={{header|Go}}==
{{trans|Wren}}
<lang go>package main
 
import "fmt"
 
func ulam(n int) int {
ulams := []int{1, 2}
set := map[int]bool{1: true, 2: true}
i := 3
for {
count := 0
for j := 0; j < len(ulams); j++ {
_, ok := set[i-ulams[j]]
if ok && ulams[j] != (i-ulams[j]) {
count++
}
if count > 2 {
break
}
}
if count == 2 {
ulams = append(ulams, i)
set[i] = true
if len(ulams) == n {
break
}
}
i++
}
return ulams[n-1]
}
 
func main() {
for n := 10; n <= 10000; n *= 10 {
fmt.Println("The", n, "\bth Ulam number is", ulam(n))
}
}</lang>
 
{{out}}
<pre>
The 10th Ulam number is 18
The 100th Ulam number is 690
The 1000th Ulam number is 12294
The 10000th Ulam number is 132788
</pre>
 
=={{header|Haskell}}==
9,492

edits