Project Euler #38: Pandigital multiples

Sort by

recency

|

37 Discussions

|

  • + 0 comments

    python 3

    def find_numbers(n, k):
        s = sorted(list(range(1, k + 1)))
    
        for i in range(2, n + 1):
            temp = ""
            j = 1
    
            while len(temp) < k:
                num = i * j
                temp += str(num)
                j += 1
    
            temp_digits = sorted(map(int, temp))
            
            if temp_digits == s and len(set(temp_digits)) == k:
                print(i)
    
    
    # Example usage
    a = list(map(int, input().split()))
    n, k = a[0], a[1]
    find_numbers(n, k)
    
  • + 0 comments

    100 points.

    digits={9: list('123456789'), 8: list('12345678')}
    n,k=map(int,input().strip().split())
    for i in range(2,n):
        value=i
        s=''
        for j in range(1,k+1):
            value=i*j
            s+=str(value)
            if list(s)!=list(dict.fromkeys(list(s))):
                break
            if sorted(list(s))==digits[k]:
                print(i)
                break
    
  • + 0 comments

    JAva code

    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
     
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
         static boolean isPandigital(int n, int k) {
            int i = 1;
            StringBuilder numbers = new StringBuilder();
    
            while (true) {
                numbers.append(n * i);
                if (numbers.length() >= k) {
                    break;
                }
                i++;
            }
    
            if (numbers.length() == k && isSubset(numbers.toString(), k)) {
                return true;
            }
            return false;
        }
    
        static boolean isSubset(String numbers, int k) {
            for (int a = 1; a <= k; a++) {
                if (!numbers.contains(Integer.toString(a))) {
                    return false;
                }
            }
            return true;
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String[] input = scanner.nextLine().split(" ");
            int n = Integer.parseInt(input[0]);
            int k = Integer.parseInt(input[1]);
    
            for (int i = 2; i < n; i++) {
                if (isPandigital(i, k)) {
                    System.out.println(i);
                }
            }
    
            scanner.close();
        }
    }
    
  • + 0 comments

    Here is my 100 points C# Solutions

    using System;
    
    class Solution {
        static bool IsPandigital(int n, int k) {
            int i = 1;
            string numbers = "";
            
            while (true) {
                numbers += (n * i).ToString();
                if (numbers.Length >= k) {
                    break;
                }
                i++;
            }
            
            if (numbers.Length == k && IsSubset(numbers, k)) {
                return true;
            }
            return false;
        }
    
        static bool IsSubset(string numbers, int k) {
            for (int a = 1; a <= k; a++) {
                if (!numbers.Contains(a.ToString())) {
                    return false;
                }
            }
            return true;
        }
    
        static void Main(string[] args) {
            string[] input = Console.ReadLine().Split();
            int n = int.Parse(input[0]);
            int k = int.Parse(input[1]);
    
            for (int i = 2; i < n; i++) {
                if (IsPandigital(i, k)) {
                    Console.WriteLine(i);
                }
            }
        }
    }
    
  • + 0 comments

    python 3

    def result():

    a = list(map(int,input().split()))
    n,k = a[0],a[1]
    s = [i+1 for i in range(k)]
    ans = []
    for i in range(2,n+1):
        temp = ""
        j = 1
        while len(temp)<=k :
            if len(temp) == k:
                temp = sorted([int(k) for k in temp])
                if temp == s :
                    ans.append(i)
                    break
            num = i*j
            temp += str(num)
            j += 1
    ans.sort()
    return ans