Project Euler #32: Pandigital products

Sort by

recency

|

28 Discussions

|

  • + 0 comments
    from itertools import permutations
    
    def calculate_product(item):
        x = (len(item) // 2) + (len(item) % 2)
        return int(''.join(item[0:1])) * int(''.join(item[1:x]))
    
    def main():
        max_number = int(input("Enter the maximum number: "))
        number = [str(x) for x in range(1, max_number + 1)]
        p_numbers = permutations(number)
        unique_products = set()
    
        for item in p_numbers:
            product = calculate_product(item)
            if product == int(''.join(item[x:])) and len(item) > 1:
                unique_products.add(int(''.join(item[x:])))
    
        print(sum(unique_products))
    
    if __name__ == "__main__":
        main()
    
  • + 0 comments

    Python

    from itertools import permutations
    number = [str(x) for x in range(1,int(input())+1)]
    p_numbers = permutations(number)
    s = set()
    for item in p_numbers:
        x = (len(item)//2)+(len(item)%2)
        if int(''.join(item[0:1])) * int(''.join(item[1:x])) == int(''.join(item[x:])):
            s.add(int(''.join(item[x:])))
        if len(item)>6 and int(''.join(item[0:2])) * int(''.join(item[2:x])) == int(''.join(item[x:])):
            s.add(int(''.join(item[x:])))
    print(sum(s))
    
  • + 0 comments

    O(1) solution

    r = {4: 12,5: 52,6: 162,7:0,8:13458,9:45228} 
    print(r[int(input().strip())])
    
  • + 0 comments

    Passed All Test Cases. I don't how, just i tried range loops.

    N = int(input())
    
    org = [i for i in range(1, N+1)]
    
    first = 0
    second = 0
    last = 0
    
    
    ans = set()
    for i in range(1, 10**2):
        for j in range(i, 10**4):
            if i == j:
                continue
            
            prd = i * j
    
            st = str(i) + str(j) + str(prd)
            st = list(map(int, st))
            st = sorted(st)
    
            if st == org:
                ans.add(prd)
                #print(prd)
    
    print(sum(ans))
    
  • + 0 comments

    You need two nested loops from 1 to 10000 . Then for each i and j , check i, j and i*j.

    c++ Solution :

    #include <bits/stdc++.h>
    using namespace std;
    #define all(v) (v).begin(), (v).end()
    #define debug(x) cout << #x << " = " << x << endl
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef pair<ll, ll> pll;
    inline ll Mod(ll x, ll mod) { return x % mod >= 0 ? x % mod : x % mod + mod; }
    
    bool is_panddigital(ll a, ll b, int n)
    {
        bool mark[10] = {0};
        ll y = a * b;
        while (a)
        {
            int u = a % 10;
            a /= 10;
            if (mark[u] || u > n || u == 0)
            {
                return 0;
            }
            mark[u] = 1;
        }
        while (b)
        {
            int u = b % 10;
            b /= 10;
            if (mark[u] || u > n || u == 0)
            {
                return 0;
            }
            mark[u] = 1;
        }
        while (y)
        {
            int u = y % 10;
            y /= 10;
            if (mark[u] || u > n || u == 0)
            {
                return 0;
            }
            mark[u] = 1;
        }
        for (int i = 1; i <= n; i++)
        {
            if (!mark[i])
                return 0;
        }
        return 1;
    }
    
    int main()
    {
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        set<int> ans;
        int n;
        cin >> n;
        for (int i = 1; i <= 10000; i++)
        {
            for (int j = 1; j <= 10000; j++)
            {
                if (is_panddigital(i, j, n))
                    ans.insert(i * j);
            }
        }
        ll k = 0;
        for (int i : ans)
            k += i;
        cout << k;
    }