Check whether a number is Tech Number or not
Last Updated :
23 Mar, 2024
Given a number, the task is to check if it is a Tech number or not.
Note: A Tech number is a number that has an even number of digits and if the number is split into two equal halves(From the middle), then the square of the sum of these halves is equal to the number itself.
Examples:
Input: n = 81
Output: True
Explanation: 8 + 1 = 9 and 92 = 81
Input: n = 2025
Output: True
Explanation: 20 + 25 = 45 and 452 = 2025
Input: n = 1521
Output: False
Explanation: 15 + 21 = 36 and 362 = 1296 which is not equal to 1521
Approach: Let us understand how to check whether a number is a tech number or not.
- Check if the number of digits is even. If it’s not even, the number cannot be a Tech number.
- Split the number into two equal halves and fin
- Compare the squared sum with the original number. If they are equal, then the number is a tech number; otherwise, it’s not.
Below is the implementation of the above approach:
Java
// Java program to check if a
// number is Tech number or not
public class TechNumber {
// Function to count the
// number of digits in a number
static int countDigits(int n)
{
int count = 0;
while (n > 0) {
n /= 10;
count++;
}
return count;
}
// Returns true if n is a tech
// number, else false
static boolean isTech(int n)
{
// Count the number of digits
int count = countDigits(n);
// Return false if digits are odd
if (count % 2 != 0)
return false;
// Split the number in two
// equal halve and store their sum
int half = count / 2;
int first_half = n / (int)Math.pow(10, half);
int second_half = n % (int)Math.pow(10, half);
int sum = first_half + second_half;
// Check if the square of the sum
// is equal to the original number
if ((sum * sum) == n)
return true;
else
return false;
}
// Driver code
public static void main(String[] args)
{
if (isTech(81))
System.out.println("True");
else
System.out.println("False");
if (isTech(2025))
System.out.println("True");
else
System.out.println("False");
if (isTech(1521))
System.out.println("True");
else
System.out.println("False");
}
}
// This code is contributed by
// Abhishek Kumar
C#
using System;
namespace TechNumberExample
{
class Program
{
// Function to count the number of digits in a number
static int CountDigits(int n)
{
int count = 0;
while (n > 0)
{
n /= 10;
count++;
}
return count;
}
// Function to check if a number is a Tech number
static bool IsTech(int n)
{
int count = CountDigits(n);
// Return false if the number of digits is odd
if (count % 2 != 0)
return false;
int half = count / 2;
int firstHalf = n / (int)Math.Pow(10, half);
int secondHalf = n % (int)Math.Pow(10, half);
int sum = firstHalf + secondHalf;
// Check if the square of the sum is equal to the original number
if ((sum * sum) == n)
return true;
else
return false;
}
static void Main(string[] args)
{
// Test cases
if (IsTech(81))
Console.WriteLine("True");
else
Console.WriteLine("False");
if (IsTech(2025))
Console.WriteLine("True");
else
Console.WriteLine("False");
if (IsTech(1521))
Console.WriteLine("True");
else
Console.WriteLine("False");
}
}
}
Javascript
// Javascript program to check if
//a number is Tech number or not
// Function to count the number
//of digits in a number
function countDigits(n)
{
let count = 0;
while (n > 0)
{
n = Math.floor(n / 10);
count++;
}
return count;
}
// Returns true if n is a tech
//number, else false
function isTech(n)
{
// Count the number of digits
const count = countDigits(n);
// Return false if digits are odd
if (count % 2 !== 0)
return false;
// Split the number in two equal halves
//and store their sum
const half = Math.floor(count / 2);
const first_half = Math.floor(n / 10 ** half);
const second_half = n % 10 ** half;
const sum = first_half + second_half;
// Check if the square of the sum
//is equal to the original number
if (sum * sum === n)
return true;
else
return false;
}
// Driver code
if (isTech(81))
console.log("True");
else
console.log("False");
if (isTech(2025))
console.log("True");
else
console.log("False");
if (isTech(1521))
console.log("True");
else
console.log("False");
// This code is contributed by
// Abhishek Kumar
C++14
// C++ program to check if a
// number is Tech number or not
#include <bits/stdc++.h>
using namespace std;
// Function to count the number
// of digits in a number
int countDigits(int n)
{
int count = 0;
while (n > 0) {
n /= 10;
count++;
}
return count;
}
// Returns true if n is a tech number, else false
bool istech(int n)
{
// Count the number of digits
int count = countDigits(n);
// Return false if digits are odd
if (count % 2 != 0)
return false;
// Split the number in two equal
// halves and store their sum
int half = count / 2;
int first_half = n / pow(10, half);
int second_half = n % int(pow(10, half));
int sum = first_half + second_half;
// Check if the square of the sum
// is equal to the original number
if ((sum * sum) == n)
return true;
else
return false;
}
// Driver code
int main()
{
if (istech(81))
cout << "True" << endl;
else
cout << "False" << endl;
if (istech(2025))
cout << "True" << endl;
else
cout << "False" << endl;
if (istech(1521))
cout << "True" << endl;
else
cout << "False" << endl;
return 0;
}
Python3
import math
# Function to count the number of digits in a number
def count_digits(n):
count = 0
while n > 0:
n //= 10
count += 1
return count
# Returns true if n is a tech number, else false
def is_tech(n):
# Count the number of digits
count = count_digits(n)
# Return false if digits are odd
if count % 2 != 0:
return False
# Split the number in two equal halves and store their sum
half = count // 2
first_half = n // 10 ** half
second_half = n % (10 ** half)
sum_val = first_half + second_half
# Check if the square of the sum is equal to the original number
if sum_val ** 2 == n:
return True
else:
return False
# Driver code
if is_tech(81):
print("True")
else:
print("False")
if is_tech(2025):
print("True")
else:
print("False")
if is_tech(1521):
print("True")
else:
print("False")
Time Complexity: O(log n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...