projecteuler---->problem=32----Pandigital products

Problem 32

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit
number, 15234, is 1 through 5 pandigital.

The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.

Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.

HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.

def is_pandigital(*args, **kwargs):
    #将三个数转化成String并排序
    num = sorted(''.join(str(arg) for arg in args))
    print num
    print "kwargs = ",kwargs
    try:
        if kwargs['length'] and len(num) != kwargs['length']:
            return False
    except KeyError:
        pass

    for i in range(len(num)):
        if str(i+1) != str(num[i]):
            return False
    return True

def main():
    pandigitals = set()
    total = 0
    for multiplicand in range(1, 5000):
        for multiplier in range(1, 100):
            product = multiplicand * multiplier
            if is_pandigital(multiplicand, multiplier, product, length=9):
                pandigitals.add(product)
    print sum(pandigitals)
if __name__ == "__main__":
    main()
时间: 2024-09-30 13:29:42

projecteuler---->problem=32----Pandigital products的相关文章

Project Euler:Problem 32 Pandigital products

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital. The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing mult

Project-Euler problem 1-50

最近闲的做了下Project Euler 上的题目,前面50题都比较简单,简单总结下.代码一般是Python和C/C++的 用Python 做这些题目简直是酸爽啊 一下代码可能不一定是我的,因为不知道论坛里面的回复不是永久的,所以我的代码有的丢了,可能找个和我的意思相近的代码.题目翻译是从 欧拉计划 | Project Euler 中文翻译站上面Copy 的表告我. Problem 1  Multiples of 3 and 5 10以下的自然数中,属于3和5的倍数的有3,5,6和9,它们之和是

Project Euler:Problem 41 Pandigital prime

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. What is the largest n-digit pandigital prime that exists? #include <iostream> #incl

Project Euler:Problem 38 Pandigital multiples

Take the number 192 and multiply it by each of 1, 2, and 3: 192 × 1 = 192 192 × 2 = 384 192 × 3 = 576 By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3) The same c

EularProject 32: 数字1-9排列构成乘法等式

Pandigital products Problem 32 We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital. The product 7254 is unusual, as the identity, 3

九章算法面试题32 小球排序

九章算法官网-原文网址 http://www.jiuzhang.com/problem/32/ 题目 有红黄蓝三色的小球若干排成一列,这些小球进行排序,请使用尽量少的空间和时间. 解答 假设顺序为红色黄色蓝色.用两根指针从头开始遍历,第一根指针遇到非红色时停下,如果第二根指针找到第一根指针之后的第一个红色停下,交换两根指针所指颜色.重复上述过程.直到第二根指针找不到任何红色.此时第一根指针到最后都是黄色或蓝色.以黄色为标准继续往后做相同的操作,则可以把黄色和蓝色排好序.在遍历的过程中,由于第二根

CodeForces 32C Flea

题目链接:http://codeforces.com/problemset/problem/32/C 本文链接:http://www.cnblogs.com/Ash-ly/p/5513436.html 题意: 给你一个M*N的方格,有一个青蛙每次只能跳S步,问能够跳最多次数的起点有多少个. 思路: 首先,第一个格子肯定是可以作为起点的,那么往下能跳的点数为(M - 1) / S +1,往右能跳的点数为(N - 1) / S +1,这些点又都可以各自作为起点,所以假设以第一个格子为起点那么起点的个

Total Commander 8.52 Beta 1

Total Commander 8.52 Beta 1http://www.ghisler.com/852_b1.php 10.08.15 Release Total Commander 8.52 beta 1 (32/64) 05.08.15 Fixed: Windows 10: Loading drive buttonbar hanging on some devices (e.g. Surface Pro 3) when SD-Card was in internal card reade

杭电 HDU 1033 Edge

Edge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2251    Accepted Submission(s): 1439 Problem Description For products that are wrapped in small packings it is necessary that the sheet of p