Fascinating Number in Java

What is a Fascinating Number?

Fascinating numbers are 3 digit numbers with a unique property. When a 3 digit number is concatenated with the number multiplied by 2 and the number multiplied by 3, sometimes we get a number which contains all the digits from 1 to 9 exactly once. There could be any number of zeros, but they are ignored. Such a 3 digit number is known as a fascinating number.

Let us look at an example. Consider the number 273. Following are the multiples of 2 and 3 of the number 273.

  • 1 x 273 = 273
  • 2 x 273 = 546
  • 3 x 273 = 819

Concatenating all the numbers, we get the final number 273546819. This is a fascinating number since it contains all the digits 1 to 9 exactly once!

Problem: Find whether a given number is a fascinating number using Java

The following Java program checks whether a given 3 digit number is a fascinating number. The Java program concatenates the given number with its multiples of 2 and 3. Then it checks whether the resulting number contains 1 to 9 exactly once. There could be any number of zeros and they are ignored.

import java.util.Scanner;

// Java program to check for fascinating numbers
public class CheckFascinatingNumber {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please enter a 3 digit number: ");
        int number = scanner.nextInt();

        if (number < 100 || number > 999) {
            System.out.println(number + " is not a valid 3 digit number!");
        } else {
            if (isFascinatingNumber(number)) {
                System.out.println(number + " is a fascinating number!");
            } else {
                System.out.println(number + " is NOT a fascinating number!");
            }
        }

        scanner.close();
    }

    // Checks whether the input number is a fascinating number
    public static boolean isFascinatingNumber(int input) {
        String sVal = "" + input + input * 2 + input * 3;

        // check existence of 1 to 9 exactly once!
        for (int i = 1; i <= 9; i++) {
            int pos = sVal.indexOf(i + "");
            // is digit missing?
            if (pos < 0) {
                return false;
            } else {
                // Is there a duplicate digit?
                if (sVal.substring(pos+1).indexOf(i + "") >= 0) {
                    return false;
                }
            }

        }
        System.out.println(sVal);
        return true;
    }

}

Following is the sample output from the above program,

java CheckFascinatingNumber
Please enter a 3 digit number: 273
273546819
273 is a fascinating number!

Problem: Find all fascinating numbers using Java

The following Java program uses the logic written in the above program to find all 3 digit fascinating numbers. The program iterates from 100 to 999 looking for fascinating numbers.

// Java example source code for fascinating number
// Generates all 3 digit fascinating numbers using Java
public class FascinatingNumberGenerator {

    public static void main(String[] args) {
        System.out.println("Generating all 3 digit fascinating numbers!");
        for(int i=100;i<=999;i++) {
            if(isFascinatingNumber(i)) {
                System.out.print(i+", ");
            }
        }
    }
    
    // Checks whether the input number is a fascinating number
    public static boolean isFascinatingNumber(int input) {
        String sVal = "" + input + input * 2 + input * 3;

        // check existence of 1 to 9 exactly once!
        for (int i = 1; i <= 9; i++) {
            int pos = sVal.indexOf(i + "");
            // is digit missing?
            if (pos < 0) {
                return false;
            } else {
                // Is there a duplicate digit?
                if (sVal.substring(pos+1).indexOf(i + "") >= 0) {
                    return false;
                }
            }

        }
        return true;
    }
}

Following is the output of the above program. It shows that there are exactly four 3 digit fascinating numbers!

java FascinatingNumberGenerator
Generating all 3 digit fascinating numbers!
192, 219, 273, 327,