Blum integer: Difference between revisions

Content added Content deleted
(→‎{{header|C}}: Updated in line with Wren example - run time now under 1 second.)
(→‎{{header|Go}}: Updated in line with Wren example of which it is a translation.)
Line 112: Line 112:
"rcu"
"rcu"
)
)

var inc = []int{4, 2, 4, 2, 4, 6, 2, 6}

// Assumes n is odd.
func firstPrimeFactor(n int) int {
if n == 1 {
return 1
}
if n%3 == 0 {
return 3
}
if n%5 == 0 {
return 5
}
for k, i := 7, 0; k*k <= n; {
if n%k == 0 {
return k
} else {
k += inc[i]
i = (i + 1) % 8
}
}
return n
}


func main() {
func main() {
Line 119: Line 143:
i := 1
i := 1
for {
for {
f := rcu.PrimeFactors(i)
p := firstPrimeFactor(i)
if len(f) == 2 && f[0] != f[1] && f[0]%4 == 3 && f[1]%4 == 3 {
if p%4 == 3 {
if bc < 50 {
q := i / p
blum[bc] = i
if q != p && q%4 == 3 && rcu.IsPrime(q) {
}
if bc < 50 {
counts[i%10/3]++
blum[bc] = i
bc++
}
if bc == 50 {
counts[i%10/3]++
fmt.Println("First 50 Blum integers:")
bc++
rcu.PrintTable(blum, 10, 3, false)
if bc == 50 {
fmt.Println()
fmt.Println("First 50 Blum integers:")
} else if bc == 26828 || bc%100000 == 0 {
rcu.PrintTable(blum, 10, 3, false)
fmt.Printf("The %7sth Blum integer is: %9s\n", rcu.Commatize(bc), rcu.Commatize(i))
fmt.Println()
if bc == 400000 {
} else if bc == 26828 || bc%100000 == 0 {
fmt.Println("\n% distribution of the first 400,000 Blum integers:")
fmt.Printf("The %7sth Blum integer is: %9s\n", rcu.Commatize(bc), rcu.Commatize(i))
digits := []int {1, 3, 7, 9}
if bc == 400000 {
for j := 0; j < 4; j++ {
fmt.Println("\n% distribution of the first 400,000 Blum integers:")
fmt.Printf(" %6.3f%% end in %d\n", float64(counts[j])/4000, digits[j])
digits := []int{1, 3, 7, 9}
for j := 0; j < 4; j++ {
fmt.Printf(" %6.3f%% end in %d\n", float64(counts[j])/4000, digits[j])
}
return
}
}
return
}
}
}
}