Numbers whose count of divisors is prime: Difference between revisions
Content added Content deleted
(Added Wren) |
(Added Go) |
||
Line 92: | Line 92: | ||
73441 76729 78961 80089 83521 85849 94249 96721 97969 |
73441 76729 78961 80089 83521 85849 94249 96721 97969 |
||
Count: 79 |
Count: 79 |
||
</pre> |
|||
=={{header|Go}}== |
|||
{{libheader|Go-rcu}} |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"math" |
|||
"rcu" |
|||
) |
|||
func countDivisors(n int) int { |
|||
count := 0 |
|||
i := 1 |
|||
k := 1 |
|||
if n%2 == 1 { |
|||
k = 2 |
|||
} |
|||
for ; i <= int(math.Sqrt(float64(n))); i += k { |
|||
if n%i == 0 { |
|||
count++ |
|||
j := n / i |
|||
if j != i { |
|||
count++ |
|||
} |
|||
} |
|||
} |
|||
return count |
|||
} |
|||
func main() { |
|||
const limit = 1e5 |
|||
var results []int |
|||
for i := 3; i < limit; i++ { |
|||
n := countDivisors(i) |
|||
if n > 2 && rcu.IsPrime(n) { |
|||
results = append(results, i) |
|||
} |
|||
} |
|||
climit := rcu.Commatize(limit) |
|||
fmt.Printf("Positive integers under %7s whose number of divisors is an odd prime:\n", climit) |
|||
under1000 := 0 |
|||
for i, n := range results { |
|||
fmt.Printf("%7s", rcu.Commatize(n)) |
|||
if (i+1)%10 == 0 { |
|||
fmt.Println() |
|||
} |
|||
if n < 1000 { |
|||
under1000++ |
|||
} |
|||
} |
|||
fmt.Printf("\n\nFound %d such integers (%d under 1,000).\n", len(results), under1000) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Positive integers under 100,000 whose number of divisors is an odd prime: |
|||
4 9 16 25 49 64 81 121 169 289 |
|||
361 529 625 729 841 961 1,024 1,369 1,681 1,849 |
|||
2,209 2,401 2,809 3,481 3,721 4,096 4,489 5,041 5,329 6,241 |
|||
6,889 7,921 9,409 10,201 10,609 11,449 11,881 12,769 14,641 15,625 |
|||
16,129 17,161 18,769 19,321 22,201 22,801 24,649 26,569 27,889 28,561 |
|||
29,929 32,041 32,761 36,481 37,249 38,809 39,601 44,521 49,729 51,529 |
|||
52,441 54,289 57,121 58,081 59,049 63,001 65,536 66,049 69,169 72,361 |
|||
73,441 76,729 78,961 80,089 83,521 85,849 94,249 96,721 97,969 |
|||
Found 79 such integers (16 under 1,000). |
|||
</pre> |
</pre> |
||