Skip to content

Instantly share code, notes, and snippets.

@dampdigits
Last active December 8, 2023 07:33
Show Gist options
  • Save dampdigits/abefc4c97287c6d91a3ea113de03d739 to your computer and use it in GitHub Desktop.
Save dampdigits/abefc4c97287c6d91a3ea113de03d739 to your computer and use it in GitHub Desktop.
Java program to verify a Tech Number

Tech Number

Definition

  • Postive integer with even number of digits
  • Can be split into halves
  • the square of the sum of the 2 halves must be equal to the original integer
  • example: 2025 -> 20, 25 -> 20+25=45 -> (45)² = 2025

Author

@dampdigits

class TechNumber {
public static void main(String args[]) {
for (int i = 0; i < args.length; i++)
if (isTechNumber(Integer.parseInt(args[i])))
System.out.println(i + " is a Tech Number");
}
static boolean isTechNumber(int num) {
String str = Integer.toString(num);
// Check for even number of digits
if (str.length()%2 != 0)
return false;
// Split into 2 numbers
String str1 str.substring(0, str.length() / 2);
String str2 str.substring(str.length() / 2);
// Find square of sum of Split numbers
int square = (int) Math.pow(Integer.parseInt(str1) + Integer.parseInt(str2), 2);
if (square == num)
return true;
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment