Multiplicatively perfect numbers: Difference between revisions

Content deleted Content added
PureFox (talk | contribs)
→‎{{header|C}}: Oops, even better :)
PureFox (talk | contribs)
→‎{{header|Go}}: Updated in line with C example of which it is a translation.
Line 222: Line 222:
=={{header|Go}}==
=={{header|Go}}==
{{trans|C}}
{{trans|C}}
{{libheader|Go-rcu}}
Run time around 9.2 seconds.
<syntaxhighlight lang="go">package main
<syntaxhighlight lang="go">package main


Line 228: Line 230:
"rcu"
"rcu"
)
)

// library method customized for this task
func Divisors(n int) []int {
var divisors []int
i := 1
k := 1
if n%2 == 1 {
k = 2
}
for ; i*i <= n; i += k {
if i > 1 && n%i == 0 { // exclude 1 and n
divisors = append(divisors, i)
if len(divisors) > 2 { // not eligible if has > 2 divisors
break
}
j := n / i
if j != i {
divisors = append(divisors, j)
}
}
}
return divisors
}


func main() {
func main() {
Line 240: Line 265:
for i := 0; ; i++ {
for i := 0; ; i++ {
if i != 1 {
if i != 1 {
divs = rcu.ProperDivisors(i)
divs = Divisors(i)
} else {
} else {
divs = []int{1, 1}
divs = []int{1, 1}
}
}
length := len(divs)
if len(divs) == 2 && divs[0]*divs[1] == i {
if length > 1 {
count++
prod := 1
if i < 500 {
for d := 1; d < length; d++ {
fmt.Printf("%3d ", i)
prod *= divs[d]
if count%10 == 0 {
}
fmt.Println()
if prod == i {
count++
if i < 500 {
fmt.Printf("%3d ", i)
if count%10 == 0 {
fmt.Println()
}
}
}
}
}
Line 279: Line 297:
scount := rcu.Commatize(count)
scount := rcu.Commatize(count)
st := rcu.Commatize(t)
st := rcu.Commatize(t)
fmt.Printf("Counts under %7s: MPNs = %7s Semi-primes = %7s\n", slimit, scount, st)
fmt.Printf("Counts under %9s: MPNs = %7s Semi-primes = %7s\n", slimit, scount, st)
if limit == 500000 {
if limit == 5000000 {
break
break
}
}