How to resolve the algorithm Tau number step by step in the Swift programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Tau number step by step in the Swift programming language
Table of Contents
Problem Statement
A Tau number is a positive integer divisible by the count of its positive divisors.
Show the first 100 Tau numbers. The numbers shall be generated during run-time (i.e. the code may not contain string literals, sets/arrays of integers, or alike).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Tau number step by step in the Swift programming language
Source code in the swift programming language
import Foundation
// 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 & 1) == 0 {
total += 1
n >>= 1
}
// 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 = 100
print("The first \(limit) tau numbers are:")
var count = 0
var n = 1
while count < limit {
if n % divisorCount(number: n) == 0 {
print(String(format: "%5d", n), terminator: "")
count += 1
if count % 10 == 0 {
print()
}
}
n += 1
}
You may also check:How to resolve the algorithm Phrase reversals step by step in the Wren programming language
You may also check:How to resolve the algorithm LZW compression step by step in the BaCon programming language
You may also check:How to resolve the algorithm Middle three digits step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Stable marriage problem step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Discordian date step by step in the Euphoria programming language