Posted on by Kalkicode
Code Number

Check if a number is a mystery number

The "Mystery Number" problem is a fun and interesting mathematical puzzle. Given a positive integer, we want to determine if it is a mystery number and find the two positive integers whose sum is equal to the given number, and their reverse numbers are equal to each other.

Problem Statement

A mystery number is a positive integer N that can be expressed as the sum of two positive integers X and Y (X > 0, Y > 0) such that the reverse of X is equal to Y, and vice versa. We need to check whether a given number N is a mystery number and, if so, find the values of X and Y.

Explanation with Examples

  1. Let's take the number 33 as an example:

    • We start with X = 1 and Y = 32 (33 - 1).
    • The reverse of 1 is 1, which is equal to Y. So, 33 is a mystery number with X = 1 and Y = 32.
  2. Consider the number 948:

    • We start with X = 1 and Y = 947 (948 - 1).
    • The reverse of 1 is 1, but it is not equal to Y (which is 749). So, we increment X to 2 and set Y = 946 (948 - 2).
    • The reverse of 2 is 2, which is not equal to Y. We continue this process until we find that X = 177 and Y = 771.
    • The reverse of 177 is 771, which is equal to Y. Therefore, 948 is a mystery number with X = 177 and Y = 771.

Pseudocode and Algorithm

Let's outline the pseudocode for solving the mystery number problem:

reverse_num(number):
    result = 0
    while number is not 0:
        result = result * 10 + (number % 10)
        number = number / 10
    return result

is_mystery_no(number):
    status = 0
    x = 1
    y = 0
    counter = 1
    while counter <= (number / 2) and status is 0:
        x = counter
        y = number - counter
        if x % 10 is not 0 and y % 10 is not 0:
            if reverse_num(x) is equal to y:
                status = 1
        counter = counter + 1
    if status is 1:
        print "[number] Is Mystery Number Of (x, y)"
    else:
        print "[number] Is Not Mystery Number"

Explanation

  • The reverse_num function takes an integer as input and returns its reverse number. For example, reverse_num(123) returns 321.

  • The is_mystery_no function checks whether the given number is a mystery number or not. It initializes status to 0, which will be set to 1 if we find a valid pair (x, y). It uses a counter to iterate through possible values of x, and calculates y accordingly.

  • Inside the loop, it checks whether both x and y are non-zero digits (last digit is not zero) to ensure that they are positive integers. Then, it checks whether the reverse of x is equal to y.

  • If a valid pair (x, y) is found, it sets status to 1 and breaks out of the loop. Finally, it prints the result indicating whether the given number is a mystery number or not, along with the values of x and y if applicable.

Code Solution

Here given code implementation process.

// C program
// Check if a number is a mystery number

#include <stdio.h>

int reverse_num(int number)
{
	int result = 0;
	while (number != 0)
	{
		result = result * 10 + (number % 10);
		number = number / 10;
	}
	//returning the reversal of number
	return result;
}

//Check whether given number is Mystery Number or not
void is_mystery_no(int number)
{
	int status = 0;
	int x = 1;
	int y = 0;
	int counter = 1;
	while (counter <= (number / 2) && status == 0)
	{
		x = counter;
		y = number - counter;
		// Note that x + y is always equal to given number
		// Check whether, given number are last digit is zero or not
		if (x % 10 != 0 && y % 10 != 0)
		{
			if (reverse_num(x) == y)
			{
				//When both numbers are equal
				status = 1;
			}
		}
		counter++;
	}
	if (status == 1)
	{
		printf("\n [%d] Is Mystery Number Of (%d,%d)", number, x, y);
	}
	else
	{
		printf("\n [%d] Is Not Mystery Number", number);
	}
}

int main()
{
	//Test case
	is_mystery_no(33);
	is_mystery_no(948);
	is_mystery_no(44);
	is_mystery_no(123);
	is_mystery_no(143);
	is_mystery_no(154);
	return 0;
}

Output

 [33] Is Mystery Number Of (12,21)
 [948] Is Mystery Number Of (177,771)
 [44] Is Mystery Number Of (13,31)
 [123] Is Not Mystery Number
 [143] Is Mystery Number Of (49,94)
 [154] Is Mystery Number Of (59,95)
// Java program 
// Check if a number is a mystery number

class MysteryNo
{
	public int reverse_num(int number)
	{
		int result = 0;
		while (number != 0)
		{
			result = result * 10 + (number % 10);
			number = number / 10;
		}
		//returning the reversal of number
		return result;
	}
	//Check whether given number is Mystery Number or not
	public void is_mystery_no(int number)
	{
		boolean status = false;
		int x = 1;
		int y = 0;
		int counter = 1;
		while (counter <= (number / 2) && status == false)
		{
			x = counter;
			y = number - counter;
			// Note that x + y is always equal to given number
			// Check whether, given number are last digit is zero or not
			if (x % 10 != 0 && y % 10 != 0)
			{
				if (reverse_num(x) == y)
				{
					//When both numbers are equal
					status = true;
				}
			}
			counter++;
		}
		if (status == true)
		{
			System.out.print("\n [" + number + "] Is Mystery Number Of (" + x + "," + y + ")");
		}
		else
		{
			System.out.print("\n [" + number + "] Is Not Mystery Number");
		}
	}
	public static void main(String[] args)
	{
		MysteryNo obj = new MysteryNo();
		//Test case
		obj.is_mystery_no(33);
		obj.is_mystery_no(948);
		obj.is_mystery_no(44);
		obj.is_mystery_no(123);
		obj.is_mystery_no(143);
		obj.is_mystery_no(154);
	}
}

Output

 [33] Is Mystery Number Of (12,21)
 [948] Is Mystery Number Of (177,771)
 [44] Is Mystery Number Of (13,31)
 [123] Is Not Mystery Number
 [143] Is Mystery Number Of (49,94)
 [154] Is Mystery Number Of (59,95)
//Include header file
#include <iostream>
using namespace std;

// C++ program 
// Check if a number is a mystery number

class MysteryNo
{
	public: int reverse_num(int number)
	{
		int result = 0;
		while (number != 0)
		{
			result = result *10 + (number % 10);
			number = number / 10;
		}
		//returning the reversal of number
		return result;
	}
	//Check whether given number is Mystery Number or not
	void is_mystery_no(int number)
	{
		bool status = false;
		int x = 1;
		int y = 0;
		int counter = 1;
		while (counter <= (number / 2) && status == false)
		{
			x = counter;
			y = number - counter;
			// Note that x + y is always equal to given number
			// Check whether, given number are last digit is zero or not
			if (x % 10 != 0 && y % 10 != 0)
			{
				if (this->reverse_num(x) == y)
				{
					//When both numbers are equal
					status = true;
				}
			}
			counter++;
		}
		if (status == true)
		{
			cout << "\n [" << number << "] Is Mystery Number Of (" << x << "," << y << ")";
		}
		else
		{
			cout << "\n [" << number << "] Is Not Mystery Number";
		}
	}
};
int main()
{
	MysteryNo obj = MysteryNo();
	//Test case
	obj.is_mystery_no(33);
	obj.is_mystery_no(948);
	obj.is_mystery_no(44);
	obj.is_mystery_no(123);
	obj.is_mystery_no(143);
	obj.is_mystery_no(154);
	return 0;
}

Output

 [33] Is Mystery Number Of (12,21)
 [948] Is Mystery Number Of (177,771)
 [44] Is Mystery Number Of (13,31)
 [123] Is Not Mystery Number
 [143] Is Mystery Number Of (49,94)
 [154] Is Mystery Number Of (59,95)
//Include namespace system
using System;

// C# program 
// Check if a number is a mystery number

class MysteryNo
{
	public int reverse_num(int number)
	{
		int result = 0;
		while (number != 0)
		{
			result = result * 10 + (number % 10);
			number = number / 10;
		}
		//returning the reversal of number
		return result;
	}
	//Check whether given number is Mystery Number or not
	public void is_mystery_no(int number)
	{
		Boolean status = false;
		int x = 1;
		int y = 0;
		int counter = 1;
		while (counter <= (number / 2) && status == false)
		{
			x = counter;
			y = number - counter;
			// Note that x + y is always equal to given number
			// Check whether, given number are last digit is zero or not
			if (x % 10 != 0 && y % 10 != 0)
			{
				if (reverse_num(x) == y)
				{
					//When both numbers are equal
					status = true;
				}
			}
			counter++;
		}
		if (status == true)
		{
			Console.Write("\n [" + number + "] Is Mystery Number Of (" + x + "," + y + ")");
		}
		else
		{
			Console.Write("\n [" + number + "] Is Not Mystery Number");
		}
	}
	public static void Main(String[] args)
	{
		MysteryNo obj = new MysteryNo();
		//Test case
		obj.is_mystery_no(33);
		obj.is_mystery_no(948);
		obj.is_mystery_no(44);
		obj.is_mystery_no(123);
		obj.is_mystery_no(143);
		obj.is_mystery_no(154);
	}
}

Output

 [33] Is Mystery Number Of (12,21)
 [948] Is Mystery Number Of (177,771)
 [44] Is Mystery Number Of (13,31)
 [123] Is Not Mystery Number
 [143] Is Mystery Number Of (49,94)
 [154] Is Mystery Number Of (59,95)
<?php
// Php program 
// Check if a number is a mystery number
class MysteryNo
{
	public	function reverse_num($number)
	{
		$result = 0;
		while ($number != 0)
		{
			$result = $result * 10 + ($number % 10);
			$number = intval($number / 10);
		}
		//returning the reversal of number
		return $result;
	}
	//Check whether given number is Mystery Number or not
	public	function is_mystery_no($number)
	{
		$status = false;
		$x = 1;
		$y = 0;
		$counter = 1;
		while ($counter <= (intval($number / 2)) && $status == false)
		{
			$x = $counter;
			$y = $number - $counter;
			// Note that x + y is always equal to given number
			// Check whether, given number are last digit is zero or not
			if ($x % 10 != 0 && $y % 10 != 0)
			{
				if ($this->reverse_num($x) == $y)
				{
					//When both numbers are equal
					$status = true;
				}
			}
			$counter++;
		}
		if ($status == true)
		{
			echo "\n [". $number ."] Is Mystery Number Of (". $x .",". $y .")";
		}
		else
		{
			echo "\n [". $number ."] Is Not Mystery Number";
		}
	}
}

function main()
{
	$obj = new MysteryNo();
	//Test case
	$obj->is_mystery_no(33);
	$obj->is_mystery_no(948);
	$obj->is_mystery_no(44);
	$obj->is_mystery_no(123);
	$obj->is_mystery_no(143);
	$obj->is_mystery_no(154);
}
main();

Output

 [33] Is Mystery Number Of (12,21)
 [948] Is Mystery Number Of (177,771)
 [44] Is Mystery Number Of (13,31)
 [123] Is Not Mystery Number
 [143] Is Mystery Number Of (49,94)
 [154] Is Mystery Number Of (59,95)
// Node Js program 
// Check if a number is a mystery number
class MysteryNo
{
	reverse_num(number)
	{
		var result = 0;
		while (number != 0)
		{
			result = result * 10 + (number % 10);
			number = parseInt(number / 10);
		}
		//returning the reversal of number
		return result;
	}
	//Check whether given number is Mystery Number or not
	is_mystery_no(number)
	{
		var status = false;
		var x = 1;
		var y = 0;
		var counter = 1;
		while (counter <= (parseInt(number / 2)) && status == false)
		{
			x = counter;
			y = number - counter;
			// Note that x + y is always equal to given number
			// Check whether, given number are last digit is zero or not
			if (x % 10 != 0 && y % 10 != 0)
			{
				if (this.reverse_num(x) == y)
				{
					//When both numbers are equal
					status = true;
				}
			}
			counter++;
		}
		if (status == true)
		{
			process.stdout.write("\n [" + number + "] Is Mystery Number Of (" + x + "," + y + ")");
		}
		else
		{
			process.stdout.write("\n [" + number + "] Is Not Mystery Number");
		}
	}
}

function main()
{
	var obj = new MysteryNo();
	//Test case
	obj.is_mystery_no(33);
	obj.is_mystery_no(948);
	obj.is_mystery_no(44);
	obj.is_mystery_no(123);
	obj.is_mystery_no(143);
	obj.is_mystery_no(154);
}
main();

Output

 [33] Is Mystery Number Of (12,21)
 [948] Is Mystery Number Of (177,771)
 [44] Is Mystery Number Of (13,31)
 [123] Is Not Mystery Number
 [143] Is Mystery Number Of (49,94)
 [154] Is Mystery Number Of (59,95)
#  Python 3 program 
#  Check if a number is a mystery number

class MysteryNo :
	def reverse_num(self, number) :
		result = 0
		while (number != 0) :
			result = result * 10 + (number % 10)
			number = int(number / 10)
		
		# returning the reversal of number
		return result
	
	# Check whether given number is Mystery Number or not
	def is_mystery_no(self, number) :
		status = False
		x = 1
		y = 0
		counter = 1
		while (counter <= (int(number / 2)) and status == False) :
			x = counter
			y = number - counter
			#  Note that x + y is always equal to given number
			#  Check whether, given number are last digit is zero or not
			if (x % 10 != 0 and y % 10 != 0) :
				if (self.reverse_num(x) == y) :
					# When both numbers are equal
					status = True
				
			
			counter += 1
		
		if (status == True) :
			print("\n [", number ,"] Is Mystery Number Of (", x ,",", y ,")", end = "")
		else :
			print("\n [", number ,"] Is Not Mystery Number", end = "")
		
	

def main() :
	obj = MysteryNo()
	# Test case
	obj.is_mystery_no(33)
	obj.is_mystery_no(948)
	obj.is_mystery_no(44)
	obj.is_mystery_no(123)
	obj.is_mystery_no(143)
	obj.is_mystery_no(154)

if __name__ == "__main__": main()

Output

 [ 33 ] Is Mystery Number Of ( 12 , 21 )
 [ 948 ] Is Mystery Number Of ( 177 , 771 )
 [ 44 ] Is Mystery Number Of ( 13 , 31 )
 [ 123 ] Is Not Mystery Number
 [ 143 ] Is Mystery Number Of ( 49 , 94 )
 [ 154 ] Is Mystery Number Of ( 59 , 95 )
#  Ruby program 
#  Check if a number is a mystery number

class MysteryNo 
	def reverse_num(number) 
		result = 0
		while (number != 0) 
			result = result * 10 + (number % 10)
			number = number / 10
		end

		# returning the reversal of number
		return result
	end

	# Check whether given number is Mystery Number or not
	def is_mystery_no(number) 
		status = false
		x = 1
		y = 0
		counter = 1
		while (counter <= (number / 2) && status == false) 
			x = counter
			y = number - counter
			#  Note that x + y is always equal to given number
			#  Check whether, given number are last digit is zero or not
			if (x % 10 != 0 && y % 10 != 0) 
				if (self.reverse_num(x) == y) 
					# When both numbers are equal
					status = true
				end

			end

			counter += 1
		end

		if (status == true) 
			print("\n [", number ,"] Is Mystery Number Of (", x ,",", y ,")")
		else 
			print("\n [", number ,"] Is Not Mystery Number")
		end

	end

end

def main() 
	obj = MysteryNo.new()
	# Test case
	obj.is_mystery_no(33)
	obj.is_mystery_no(948)
	obj.is_mystery_no(44)
	obj.is_mystery_no(123)
	obj.is_mystery_no(143)
	obj.is_mystery_no(154)
end

main()

Output

 [33] Is Mystery Number Of (12,21)
 [948] Is Mystery Number Of (177,771)
 [44] Is Mystery Number Of (13,31)
 [123] Is Not Mystery Number
 [143] Is Mystery Number Of (49,94)
 [154] Is Mystery Number Of (59,95)
// Scala program 
// Check if a number is a mystery number

class MysteryNo
{
	def reverse_num(n: Int): Int = {
      	var number: Int = n;
		var result: Int = 0;
		while (number != 0)
		{
			result = result * 10 + (number % 10);
			number = (number / 10).toInt;
		}
		//returning the reversal of number
		return result;
	}
	//Check whether given number is Mystery Number or not
	def is_mystery_no(number: Int): Unit = {
		var status: Boolean = false;
		var x: Int = 1;
		var y: Int = 0;
		var counter: Int = 1;
		while (counter <= ((number / 2).toInt) && status == false)
		{
			x = counter;
			y = number - counter;
			// Note that x + y is always equal to given number
			// Check whether, given number are last digit is zero or not
			if (x % 10 != 0 && y % 10 != 0)
			{
				if (reverse_num(x) == y)
				{
					//When both numbers are equal
					status = true;
				}
			}
			counter += 1;
		}
		if (status == true)
		{
			print("\n [" + number + "] Is Mystery Number Of (" + x + "," + y + ")");
		}
		else
		{
			print("\n [" + number + "] Is Not Mystery Number");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MysteryNo = new MysteryNo();
		//Test case
		obj.is_mystery_no(33);
		obj.is_mystery_no(948);
		obj.is_mystery_no(44);
		obj.is_mystery_no(123);
		obj.is_mystery_no(143);
		obj.is_mystery_no(154);
	}
}

Output

 [33] Is Mystery Number Of (12,21)
 [948] Is Mystery Number Of (177,771)
 [44] Is Mystery Number Of (13,31)
 [123] Is Not Mystery Number
 [143] Is Mystery Number Of (49,94)
 [154] Is Mystery Number Of (59,95)
// Swift 4 program 
// Check if a number is a mystery number
class MysteryNo
{
	func reverse_num(_ n: Int) -> Int
	{
      	var number: Int = n;
		var result: Int = 0;
		while (number != 0)
		{
			result = result * 10 + (number % 10);
			number = number / 10;
		}
		//returning the reversal of number
		return result;
	}
	//Check whether given number is Mystery Number or not
	func is_mystery_no(_ number: Int)
	{
		var status: Bool = false;
		var x: Int = 1;
		var y: Int = 0;
		var counter: Int = 1;
		while (counter <= (number / 2) && status == false)
		{
			x = counter;
			y = number - counter;
			// Note that x + y is always equal to given number
			// Check whether, given number are last digit is zero or not
			if (x % 10 != 0 && y % 10 != 0)
			{
				if (self.reverse_num(x) == y)
				{
					//When both numbers are equal
					status = true;
				}
			}
			counter += 1;
		}
		if (status == true)
		{
			print("\n [", number ,"]Is Mystery Number Of (", x ,",", y ,")", terminator: "");
		}
		else
		{
			print("\n [", number ,"]Is Not Mystery Number", terminator: "");
		}
	}
}
func main()
{
	let obj: MysteryNo = MysteryNo();
	//Test case
	obj.is_mystery_no(33);
	obj.is_mystery_no(948);
	obj.is_mystery_no(44);
	obj.is_mystery_no(123);
	obj.is_mystery_no(143);
	obj.is_mystery_no(154);
}
main();

Output

 [ 33 ]Is Mystery Number Of ( 12 , 21 )
 [ 948 ]Is Mystery Number Of ( 177 , 771 )
 [ 44 ]Is Mystery Number Of ( 13 , 31 )
 [ 123 ]Is Not Mystery Number
 [ 143 ]Is Mystery Number Of ( 49 , 94 )
 [ 154 ]Is Mystery Number Of ( 59 , 95 )

Time Complexity

The time complexity of the algorithm mainly depends on the reverse_num function, which has a time complexity of O(log n), where n is the number of digits in the input number. The is_mystery_no function has a loop that runs up to number/2 times, so its time complexity is O(number). Therefore, the overall time complexity of the code is O(number * log n), where n is the number of digits in the input number.

Output Explanation

The given C program calls the is_mystery_no function for several test cases:

  1. is_mystery_no(33): The function finds that 33 is a mystery number with x = 12 and y = 21. So, it prints "[33] Is Mystery Number Of (12, 21)".

  2. is_mystery_no(948): The function finds that 948 is a mystery number with x = 177 and y = 771. So, it prints "[948] Is Mystery Number Of (177, 771)".

  3. is_mystery_no(44): The function finds that 44 is a mystery number with x = 13 and y = 31. So, it prints "[44] Is Mystery Number Of (13, 31)".

  4. is_mystery_no(123): The function doesn't find a valid pair (x, y) for 123, so it prints "[123] Is Not Mystery Number".

  5. is_mystery_no(143): The function finds that 143 is a mystery number with x = 49 and y = 94. So, it prints "[143] Is Mystery Number Of (49, 94)".

  6. is_mystery_no(154): The function finds that 154 is a mystery number with x = 59 and y = 95. So, it prints "[154] Is Mystery Number Of (59, 95)".

The output confirms whether each test case is a mystery number or not, and if it is, it provides the corresponding values of x and y.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment