Untouchable numbers: Difference between revisions
→{{header|Go}}: Updated version.
(Realize in F#) |
(→{{header|Go}}: Updated version.) |
||
Line 102:
</pre>
=={{header|Go}}==
This was originally based on the Wren example but has been modified somewhat to find untouchable numbers up to 1 million rather than 100,000 in a reasonable time. On my machine, the former (with a sieve factor of 63) took 31 minutes 9 seconds and the latter (with a sieve factor of 14) took 6.2 seconds.
<lang go>package main
Line 108:
func sumDivisors(n int) int {
sum :=
i := 1▼
k := 2
if n%2 == 0 {
k = 1
}
for i := 1 + k; i*i <= n; i += k {
if n%i == 0 {
sum += i
Line 122 ⟶ 121:
}
}
i += k▼
}
return sum
}
func sieve(n int) []bool {
n++
s := make([]bool, n
for i :=
}
return s
}
func
// True denotes
c := make([]bool, limit) // all false by default
c[1]
// no need to bother with even numbers over 2 for this task
for
▲ if n%d == 0 {
▲ return false
}
for {
if !c[p] {
}
}
▲ d += 4
}
return
}
Line 176 ⟶ 179:
func main() {
s := sieve(63 * limit)
untouchable := []int{2, 5}
for n := 6; n <= limit; n += 2 {
if !s[n] &&
untouchable = append(untouchable, n)
}
Line 193 ⟶ 197:
count++
}
fmt.Printf("\n\n%
p := 10
count = 0
Line 201 ⟶ 205:
cc := commatize(count - 1)
cp := commatize(p)
fmt.Printf("%
p = p * 10
if p == limit {
Line 210 ⟶ 214:
cu := commatize(len(untouchable))
cl := commatize(limit)
fmt.Printf("%
}</lang>
Line 237 ⟶ 241:
1,958 1,960 1,962 1,972 1,986 1,992
196 untouchable numbers were found <= 2,000
2 untouchable numbers were found <= 10
5 untouchable numbers were found <= 100
89 untouchable numbers were found <= 1,000
13,863 untouchable numbers were found <= 100,000
150,232 untouchable numbers were found <= 1,000,000
</pre>
|