Tech number in Swift


If the given number contains an even number of digits and the digits of the number can be divided into two equal parts from the middle. After dividing the digits sum up the divided digits and find the square of the final sum. If the square is equal to the sum itself then the given number is a tech number otherwise not.

Example Demonstration

Input

3025

Output

Yes the given number is a tech number

Input

2341

Output

No the given number is not a tech number

Here, 3025 is a tech number because 30+25 = 55 => (55)2 = 3025. Whereas 2341 is not a tech number because 23+41 = 64 => (64)2 = 4096.

Algorithm

Step 1 − Get the number from the user.

Step 2 − Count the total number of digits present in the number.

Step 3 − Then check if the number contains an even number of digits or not.

Step 4 − If yes, then divide the digits of the number into two equal parts from the middle.

Step 5 − Then add both parts.

Step 6 − Find the square of the sum.

Step 7 − If the square of the sum is equal to the original number, then the number is a tech number. Otherwise not.

Step 8 − Display the output.

Now using the following Swift programs we can find the tech number.

Example 1: Checking whether the given number is a Tech number or not

In the following Swift program, we will check if the given number is a tech number or not. So for that, we will create a function which takes one parameter and return a boolean value which represents whether the given number is a tech number or not. This function first checks the total number of digits in the given number. If the digits are even numbers, then it divides the digits into two sets from the middle so that both sets contain the same number of digits. Then it adds both the set and then finds the square of the sum. If the square of the sum is equal to the original number then the number is a tech number otherwise not.

import Foundation
import Glibc

// Function to check if the number is tech number or not
func checkTechNumber(num: Int) -> Bool {
    var number = num
    var digits = 0
    
    while number > 0 {
        digits += 1
        number /= 10
    }
    
    // If number contain even digits
    // then check the given number is tech number or not
    if digits % 2 == 0 {
    
        number = num
        
        // Dividing digits of number into two set from the
        // middle with exact number of digits in each set
        let firstHalf = number % Int(pow(10, Double(digits / 2)))
        let secondHalf = number / Int(pow(10, Double(digits / 2)))
        
        // Adding both the set
        let sum = firstHalf + secondHalf
        
        // Fiding the square of the sum 
        let square = Int(pow(Double(sum), 2))
        
        // If the square is equal to the number then retrun true
        return num == square
    } else {
        return false
    }
}

let myNumber = 2025
if (checkTechNumber(num:myNumber) == true) {
    print("\(myNumber) is a tech number.")
} else {
    print("\(myNumber) is not a tech number.")
}

Output

2025 is a tech number.

Example 2: Finding tech numbers from 1 to N

In the following Swift program, we will find tech numbers from 1 to 10000000 or N. So for that, we run a for−in loop from 1 to 10000000 and check each number if it is a tech number or not. If the current number is a tech number, then display it on the screen. Otherwise, move to the next number.

import Foundation
import Glibc

print("Tech numbers between 1 to 10000000 are:")

for number in 1...10000000{

    // Dividing the digits of the number into two equal parts
    let firstHalf = number % 100
    let secondHalf = number / 100
    
    // Find the sum of the two parts
    let sum = firstHalf + secondHalf
    
    // Find the square of the sum 
    let square = Int(pow(Double(sum), 2))
    
    // Check if the square is equal to the number
    if number == square {
        print(number)
    }
}

Output

Tech numbers between 1 to 10000000 are:
1
2025
3025
9801
10000

Real−Life Usage

The real−life usage of tech numbers is:

  • Tech numbers are used to understand the programming logic.

  • It is used by beginners to understand the working of the loop.

Conclusion

So this is how we can check if the number is a tech number or not in Swift. It is useful for building logic in programming. Using the above method we can easily find tech numbers.

Updated on: 08-Sep-2023

66 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements