swift

find-nth-perfect-number()

Parameters: n: Int

Input 'n' is the position in the sequence

Returns: Returns the nth perfect number as Integer

This Swift function helps you locate the nth perfect number in the sequence of perfect numbers, i.e. numbers whose divisors sum to the number itself.

Loops
Conditionals
Variables
Medium dificulty

Crafting a Swift function to find the Nth Perfect Number

Hello there, Programmer! Today, we'll dive into an intricate exercise of producing the perfect number. If you are wondering, a 'perfect number' is a positive integer that equals the sum of its positive divisors excluding itself. For example, 6 is a perfect number because 1,2 and 3 are its divisors and 1+2+3 equals 6 itself. We will craft a Swift function which will work seamlessly to identify the nth perfect number for us. Hold your coding gears and let's jump right into this fascinating journey of programming.

Step 1: Understand the Problem

The requirement is to create a function in Swift that will find the nth perfect number. A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. Our task is to create a function that finds the nth perfect number.

var perfectNumbers = [Int]()

Step 2: Writing the Function to Check a Perfect Number

The first thing we'll do is to write a function which checks whether given number is perfect or not. We can do this by creating a for loop from 1 to half of the number (since no divisor can be greater than half of the number), and adding divisor to sum if it divides the number completely (remainder is zero).

func isPerfectNumber(_ n: Int) -> Bool {
    var sum = 0
    for i in 1..<(n / 2) + 1 {
        if n % i == 0 {
            sum += i
        }
    }
    return sum == n
}

Step 3: Collecting the Perfect Numbers

Next, we'll loop through the natural numbers from 1 and for each number, we'll check if it is a perfect number using the function we wrote in step 2. If it is, we'll store it in our array of perfect numbers.

var n = 1
while perfectNumbers.count < 4 {
    if isPerfectNumber(n) {
        perfectNumbers.append(n)
    }
    n += 1
}

Step 4: Returning the nth Perfect Number

Finally, we'll build a function findNthPerfectNumber() that will take an integer as input (n), and return the nth perfect number by returning the nth element from our perfect numbers array.

func findNthPerfectNumber(_ n: Int) -> Int {
    return perfectNumbers[n-1]
}

Conclusion

The solution to our problem involved understanding what a perfect number is, and how to establish whether a number fits this criteria. Once this was established, it was a matter of looping through the natural numbers to locate these perfect numbers and adding them to an array. Finally, to solve our initial problem, we created a function that returned the nth perfect number from our array.

Here is the full implementation:

func isPerfectNumber(_ n: Int) -> Bool {
    var sum = 0
    for i in 1..<(n / 2) + 1 {
        if n % i == 0 {
            sum += i
        }
    }
    return sum == n
}

var perfectNumbers = [Int]()
var n = 1
while perfectNumbers.count < 4 {
    if isPerfectNumber(n) {
        perfectNumbers.append(n)
    }
    n += 1
}

func findNthPerfectNumber(_ n: Int) -> Int {
    return perfectNumbers[n-1]
}

Learn function in:

Nth Perfect Number

Find the nth perfect number in a mathematical sequence

Learn more

Mathematical principle

Perfect numbers are positive integers that are equal to the sum of their proper positive divisors, excluding the number itself. For instance, `6` is a perfect number because its divisors `1`, `2` and `3` add up to `6`. This sequence begins `6`, `28`, `496`, `8128`, ... In this function, we're finding the nth number of this sequence.

Learn more