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.
{{trans|Wren}}
<lang go>package main
 
Line 108:
 
func sumDivisors(n int) int {
sum := 01
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 - n
}
 
func sieve(n int) []bool {
n++
s := make([]bool, n*4+1) // all false by default
for i := 06; i <= n; i++ {
s[sd := sumDivisors(i)] = true
if n%dsd =<= 0n {
returns[sd] false= true
i := 1 }
}
return s
}
 
func isPrimeprimeSieve(nlimit int) []bool {
if n < 2 {limit++
// True denotes returncomposite, false denotes prime.
c := make([]bool, limit) // all false by default
}
if n%2c[0] == 0 {true
c[1] return n == 2true
// no need to bother with even numbers over 2 for this task
}
ifp := n%3 ==// 0Start {from 3.
for return n == 3{
} p2 := p * p
d : if p2 >= 5limit {
for d*d <= n { break
if n%d == 0 {
return false
}
dfor i := p2; i < limit; i += 2 * p {
if n%d == 0 {c[i] = true
return false}
for {
i p += k2
if !c[p] {
d += 4 break
}
}
d += 4
}
return truec
}
 
Line 176 ⟶ 179:
 
func main() {
limitc := 100000primeSieve(1000000)
slimit := sieve(14 * limit)1000000
s := sieve(63 * limit)
untouchable := []int{2, 5}
for n := 6; n <= limit; n += 2 {
if !s[n] && !isPrime(c[n-1)] && !isPrime(c[n-3)] {
untouchable = append(untouchable, n)
}
Line 193 ⟶ 197:
count++
}
fmt.Printf("\n\n%6s7s untouchable numbers were found <= 2,000\n", commatize(count))
p := 10
count = 0
Line 201 ⟶ 205:
cc := commatize(count - 1)
cp := commatize(p)
fmt.Printf("%6s7s untouchable numbers were found <= %7s9s\n", cc, cp)
p = p * 10
if p == limit {
Line 210 ⟶ 214:
cu := commatize(len(untouchable))
cl := commatize(limit)
fmt.Printf("%6s7s untouchable numbers were found <= %s\n", cu, cl)
}</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
1,212 untouchable numbers were found <= 10,000
13,863 untouchable numbers were found <= 100,000
150,232 untouchable numbers were found <= 1,000,000
</pre>
 
9,482

edits