Multiplicatively perfect numbers: Difference between revisions
Content deleted Content added
→{{header|C}}: Oops, even better :) |
→{{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 |
|||
⚫ | |||
⚫ | |||
} |
|||
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 |
|||
⚫ | |||
⚫ | |||
j := n / i |
|||
⚫ | |||
⚫ | |||
} |
|||
} |
|||
} |
|||
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 = |
divs = Divisors(i) |
||
} else { |
} else { |
||
divs = []int{1, 1} |
divs = []int{1, 1} |
||
} |
} |
||
if len(divs) == 2 && divs[0]*divs[1] == i { |
|||
count++ |
|||
if i < 500 { |
|||
fmt.Printf("%3d ", i) |
|||
if count%10 == 0 { |
|||
fmt.Println() |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
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 % |
fmt.Printf("Counts under %9s: MPNs = %7s Semi-primes = %7s\n", slimit, scount, st) |
||
if limit == |
if limit == 5000000 { |
||
break |
break |
||
} |
} |