Tetradic Number
Last Updated :
19 Jul, 2023
A Tetradic Number (sometimes called a four-way number) is a number that remains the same when flipped back-front, mirrored up-down, or flipped up-down.
In other words, a Tetradic Number is a palindromic number containing only 0, 1 and 8 as digits in the number.
The first few Tetradic Number are:
0, 1, 8, 11, 88, 101, 111, 181, 808, 818, 888, 1001, 1111, 1881, ….
Check if N is an Tetradic number
Given a number N, the task is to check if N is an Tetradic Number or not.
Examples:
Input: N = 101
Output: Yes
Explanation:
101 is palindrome number and it contains only 0, 1 and 8 as digits in the number.
Input: N = 1221
Output: No
Approach: The idea is to check if the number is palindrome or not. If the number is palindrome then check for the digits in the number. If all digits lies in the set (0, 1, 8), then the number is a Tetradic number.
For Example:
For N = 101
// As N is a palindromic number
// All the digits in the number is
// from the set {0, 1, 8}
// Therefore, N is a Tetradic number
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isContaindigit( int n)
{
string temp = to_string(n);
for ( char i : temp) {
if (i != '0' && i != '1' && i != '8' )
return false ;
}
return true ;
}
bool ispalindrome( int n)
{
string temp = to_string(n);
string temp1 = to_string(n);
reverse(temp1.begin(), temp1.end());
return (temp == temp1);
}
bool isTetradic( int n)
{
return ispalindrome(n) && isContaindigit(n);
}
int main()
{
int N = 101;
if (isTetradic(N))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
import java.util.*;
class GFG {
public static String Reverse(String s)
{
StringBuilder sb = new StringBuilder();
sb.append(s);
sb.reverse();
return new String(sb);
}
static boolean isContaindigit( int n)
{
String temp = String.valueOf(n);
for ( char i : temp.toCharArray()) {
if (i != '0' && i != '1' && i != '8' )
return false ;
}
return true ;
}
static boolean ispalindrome( int n)
{
String temp = String.valueOf(n);
String temp1 = String.valueOf(n);
temp1 = Reverse(temp1);
return (temp.equals(temp1));
}
static boolean isTetradic( int n)
{
return ispalindrome(n) && isContaindigit(n);
}
public static void main(String[] args)
{
int N = 101 ;
if (isTetradic(N))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isContaindigit(n):
temp = str (n)
for i in temp:
if i not in [ '0' , '1' , '8' ]:
return False
return True
def ispalindrome(n):
temp = str (n)
if temp = = temp[:: - 1 ]:
return True
return False
def isTetradic(n):
if ispalindrome(n):
if isContaindigit(n):
return True
return False
N = 101
if (isTetradic(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Text;
using System.Collections.Generic;
class GFG {
public static string Reverse( string s)
{
char [] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string (charArray);
}
static bool isContaindigit( int n)
{
string temp = Convert.ToString(n);
foreach ( char i in temp)
{
if (i != '0' && i != '1' && i != '8' )
return false ;
}
return true ;
}
static bool ispalindrome( int n)
{
string temp = Convert.ToString(n);
string temp1 = Convert.ToString(n);
temp1 = Reverse(temp1);
return (temp.Equals(temp1));
}
static bool isTetradic( int n)
{
return ispalindrome(n) && isContaindigit(n);
}
public static void Main( string [] args)
{
int N = 101;
if (isTetradic(N))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
function isContaindigit(n)
{
let temp = n.toString()
for (let i of temp.split( "" ))
if (![ '0' , '1' , '8' ].includes(i))
return false
return true
}
function ispalindrome(n)
{
let temp = n.toString()
return (temp == temp.split( "" ).reverse().join( "" ))
}
function isTetradic(n)
{
return ispalindrome(n) && isContaindigit(n)
}
let N = 101
if (isTetradic(N))
console.log( "Yes" )
else
console.log( "No" )
|
Time Complexity: O(N)
Space Complexity: O(N)
Optimized Approach:
This code uses a while loop to reverse the digits of N and store them in a separate variable rev. It also checks if each digit of N is 0, 1, or 8. If all the digits are 0, 1, or 8 and N is equal to its reversed form, the number is considered a tetradic number and the function returns Yes. Otherwise, the function returns No.
C++
#include <bits/stdc++.h>
using namespace std;
#include <cmath>
bool isTetradic( int n) {
int num = n;
int rev = 0;
while (num > 0) {
int digit = num % 10;
if (digit != 0 && digit != 1 && digit != 8) {
return false ;
}
rev = rev * 10 + digit;
num /= 10;
}
return n == rev;
}
int main() {
int N = 101;
if (isTetradic(N)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
|
Java
import java.io.*;
public class Tetradic {
public static boolean isTetradic( int n) {
int num = n;
int rev = 0 ;
while (num > 0 ) {
int digit = num % 10 ;
if (digit != 0 && digit != 1 && digit != 8 ) {
return false ;
}
rev = rev * 10 + digit;
num /= 10 ;
}
return n == rev;
}
public static void main(String[] args) {
int N = 101 ;
if (isTetradic(N)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
def isTetradic(n):
num = n
rev = 0
while num > 0 :
digit = num % 10
if digit ! = 0 and digit ! = 1 and digit ! = 8 :
return False
rev = rev * 10 + digit
num / / = 10
return n = = rev
N = 101
if isTetradic(N):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
namespace TetradicNumber
{
class Program
{
static bool IsTetradic( int n)
{
int num = n;
int rev = 0;
while (num > 0)
{
int digit = num % 10;
if (digit != 0 && digit != 1 && digit != 8)
{
return false ;
}
rev = rev * 10 + digit;
num /= 10;
}
return n == rev;
}
static void Main( string [] args)
{
int N = 101;
if (IsTetradic(N))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
}
|
Javascript
function isTetradic(n) {
let num = n;
let rev = 0;
while (num > 0) {
let digit = num % 10;
if (digit !== 0 && digit !== 1 && digit !== 8) {
return false ;
}
rev = rev * 10 + digit;
num = Math.floor(num / 10);
}
return n === rev;
}
let N = 101;
if (isTetradic(N)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time Complexity: O(log(N))
Auxiliary Space: O(1)
Approach3: using Vectors:
This approach uses a vector to store the digits of the input number. Here’s how the approach works:
- The isTetradic function takes an integer n as input and initializes an empty vector called digits.
- The function iteratively extracts the last digit of the input number using the modulus operator and checks if it is equal to 0, 1, or 8. If the digit is not in this set, the function returns false immediately because the number is not Tetradic.
- The function then adds the digit to the digits vector and removes the last digit from the number using integer division.
- Once all digits have been extracted, the function creates a copy of the digits vector called reversedDigits and reverses it using the reverse function.
- Finally, the function checks if the original digits vector is equal to the reversed reversedDigits vector. If they are equal, the function returns true, indicating that the input number is Tetradic. Otherwise, it returns false.
- The advantage of using a vector to store the digits is that it allows us to easily reverse the digits and compare them to the original digits. It also avoids the overhead of converting the number to a string and provides a more memory-efficient alternative to storing the reversed number in a separate variable.
Here is the code of above approach:
C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool isTetradic( int n)
{
vector< int > digits;
while (n > 0) {
int digit = n % 10;
if (digit != 0 && digit != 1 && digit != 8)
return false ;
digits.push_back(digit);
n /= 10;
}
vector< int > reversedDigits(digits);
reverse(reversedDigits.begin(), reversedDigits.end());
return (digits == reversedDigits);
}
int main()
{
int N = 101;
if (isTetradic(N))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class GFG {
public static boolean isTetradic( int n) {
List<Integer> digits = new ArrayList<>();
while (n > 0 ) {
int digit = n % 10 ;
if (digit != 0 && digit != 1 && digit != 8 )
return false ;
digits.add(digit);
n /= 10 ;
}
List<Integer> reversedDigits = new ArrayList<>(digits);
Collections.reverse(reversedDigits);
return digits.equals(reversedDigits);
}
public static void main(String[] args) {
int N = 101 ;
if (isTetradic(N))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isTetradic(n):
digits = []
while n > 0 :
digit = n % 10
if digit not in [ 0 , 1 , 8 ]:
return False
digits.append(digit)
n / / = 10
reversedDigits = digits[:: - 1 ]
return digits = = reversedDigits
N = 101
if isTetradic(N):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
public static bool IsTetradic( int n)
{
List< int > digits = new List< int >();
while (n > 0)
{
int digit = n % 10;
if (digit != 0 && digit != 1 && digit != 8)
return false ;
digits.Add(digit);
n /= 10;
}
List < int > reversedDigits = Enumerable.Reverse(digits).ToList();
return digits.Intersect(reversedDigits).Any();
}
public static void Main( string [] args)
{
int N = 101;
if (IsTetradic(N))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
function IsTetradic(n) {
let digits = [];
while (n > 0) {
let digit = n % 10;
if (digit !== 0 && digit !== 1 && digit !== 8)
return false ;
digits.push(digit);
n = Math.floor(n / 10);
}
let reversedDigits = digits.slice().reverse();
return digits.filter(value => reversedDigits.includes(value)).length > 0;
}
const N = 101;
if (IsTetradic(N))
console.log( "Yes" );
else
console.log( "No" );
|
Time Complexity: O(log N), where N is the input number
Auxiliary Space: O(LogN)
Share your thoughts in the comments
Please Login to comment...