Jump to content

Sequence: smallest number greater than previous term with exactly n divisors: Difference between revisions

Added Swift solution
(Realize in F#)
(Added Swift solution)
Line 997:
<pre>
[1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624]
</pre>
 
=={{header|Swift}}==
Includes an optimization borrowed from the Pascal example above.
<lang swift>// See https://en.wikipedia.org/wiki/Divisor_function
func divisorCount(number: Int) -> Int {
var n = number
var total = 1
// Deal with powers of 2 first
while n % 2 == 0 {
total += 1
n /= 2
}
// Odd prime factors up to the square root
var p = 3
while p * p <= n {
var count = 1
while n % p == 0 {
count += 1
n /= p
}
total *= count
p += 2
}
// If n > 1 then it's prime
if n > 1 {
total *= 2
}
return total
}
 
let limit = 32
var n = 1
var next = 1
while next <= limit {
if next == divisorCount(number: n) {
print(n, terminator: " ")
next += 1
if next > 4 && divisorCount(number: next) == 2 {
n = 1 << (next - 1) - 1;
}
}
n += 1
}
print()</lang>
 
{{out}}
<pre>
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 4632 65536 65572 262144 262192 263169 269312 4194304 4194306 4477456 4493312 4498641 4498752 268435456 268437200 1073741824 1073741830
</pre>
 
1,777

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.