Project Euler #52: Permuted multiples

  • + 0 comments

    JAva Code

    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        static int[] digits = new int[10];
    
        static boolean hasSameDigits(int num1, int num2) {
            for (int i = 0; i < 10; i++) {
                digits[i] = 0;
            }
            while (num1 != 0) {
                digits[num1 % 10]++;
                num1 /= 10;
            }
            while (num2 != 0) {
                digits[num2 % 10]--;
                num2 /= 10;
            }
            for (int i = 0; i < 10; i++) {
                if (digits[i] != 0) {
                    return false;
                }
            }
            return true;
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            for (int i = 1; i <= n; i++) {
                int flag = 0;
                for (int j = 2; j <= k; j++) {
                    if (!hasSameDigits(i, j * i)) {
                        break;
                    } else {
                        flag++;
                    }
                }
                if (flag == k - 1) {
                    for (int z = 1; z <= k; z++) {
                        System.out.print(i * z + " ");
                    }
                    System.out.println();
                }
            }
        }
    }