Erdős-Nicolas numbers: Difference between revisions

Added Go
m (→‎Version 2: Added libheader.)
(Added Go)
Line 294:
}
}
}</syntaxhighlight>
 
{{out}}
<pre>
24 equals the sum of its first 6 divisors
2016 equals the sum of its first 31 divisors
8190 equals the sum of its first 43 divisors
42336 equals the sum of its first 66 divisors
45864 equals the sum of its first 66 divisors
714240 equals the sum of its first 113 divisors
392448 equals the sum of its first 68 divisors
1571328 equals the sum of its first 115 divisors
61900800 equals the sum of its first 280 divisors
91963648 equals the sum of its first 142 divisors
</pre>
 
=={{header|Go}}==
{{trans|C++}}
This runs in about 30 seconds which, surprisingly, is faster than C++ (43 seconds) - usually Go is a good bit slower.
<syntaxhighlight lang="go">package main
 
import "fmt"
 
func main() {
const maxNumber = 100000000
dsum := make([]int, maxNumber+1)
dcount := make([]int, maxNumber+1)
for i := 0; i <= maxNumber; i++ {
dsum[i] = 1
dcount[i] = 1
}
for i := 2; i <= maxNumber; i++ {
for j := i + i; j <= maxNumber; j += i {
if dsum[j] == j {
fmt.Printf("%8d equals the sum of its first %d divisors\n", j, dcount[j])
}
dsum[j] += i
dcount[j]++
}
}
}</syntaxhighlight>
 
9,476

edits