Hacked By Dr.S4mom

### Why buy cialis on the internet is really beneficial for you?

So you’ve decided to order cialis and do not know where to start? We can give you some advice. First, ask your doctor for advice in order to properly determine the dosage, when you do that, you need to decide for yourself exactly where you will be buying the drug. You can buy cialis online, or you can just buy it at the pharmacy. Buy cialis online has a number of advantages, one of which is price. The cost of the Internet will always be lower than in stores, and when combined with the free shipping, it will be the best choice. Besides the price there are a number of advantages over conventional pharmacies, one of which is anonymity. Also, you can always check the online store on reliability, read reviews about it and the opinion of other buyers. Read more.

Hi there!

So today, I thought I’d post a solution for Project Euler’s Pandigital Products Problem (No. 32).

In case you don’t know what Project Euler ist, its basically a website with mathematical challenges that can/should be solved by writing code in order to compute the solution. Doing these challenges is a fun way of combining mathematical thinking and coding. I recommend checking out project Euler here.

Many if not most people who work on these problems try to find the computationally most efficient solution. However I believe that there also lies some virtue in trying to find a feasible solution as quickly as possible. Thats what I am going to attempt to do here.

**The Problem**

Lets take a look at the original problem definition from the Project Euler website:

1 2 3 4 5 6 7 |
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. |

To paraphrase: We are asked to find all numbers a, b and c such that a*b = c and that the concatenation of a, b and c contains each digit (1 to 9) exactly once. The solution is then the sum of all unique values of c that we can find.

**A (simple) Solution using Python:**

From the problem it is clear that the concatenation of a, b and c must be 9 characters long. How about if we compute all permutations of the sequence of all digits “123456789”?

1 2 3 4 5 6 |
from itertools import permutations digits = "123456789" for permutation in [''.join(perm) for perm in permutations(digits)]: print permutation |

Output:

1 2 3 4 5 6 7 |
123456789 123456798 123456879 123456897 123456978 123456987 ... |

And now just check if there is a way to split each permutation into three parts such that it represents a concatenation of a, b and c with the desired property that a*b = c.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from itertools import permutations digits = "123456789" for permutation in [''.join(perm) for perm in permutations(digits)]: for i in range(1, 9): # use i as the position of the first cut for j in range(i+1, 9): # use j as the position of the second cut # split the permutation into parts a,b and c a = int(permutation[:i]) b = int(permutation[i:j]) c = int(permutation[j:]) if a * b == c: # we found a product where the concatenation of a, b, and c is pandigital print "%d * %d = %d" % (a, b, c) |

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
12 * 483 = 5796 138 * 42 = 5796 157 * 28 = 4396 159 * 48 = 7632 1738 * 4 = 6952 18 * 297 = 5346 186 * 39 = 7254 1963 * 4 = 7852 198 * 27 = 5346 27 * 198 = 5346 28 * 157 = 4396 297 * 18 = 5346 39 * 186 = 7254 4 * 1738 = 6952 4 * 1963 = 7852 42 * 138 = 5796 48 * 159 = 7632 483 * 12 = 5796 |

Thats nice, we got all possible combinations of a,b and c such that a*b=c and that the concatenation of a, b and c is pandigital. Notice how some values of c occur multiple times in the output, i.e. 12*483 = 5796 and also 138*42 = 5796.

The next step is to sum up all different values of c, in order to find the solution to the original problem. I am using a python set to collect all products, because sets filter out duplicate entries automatically. After going through the iterations the values in the set just need to be summed up to give us the correct answer.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from itertools import permutations digits = "123456789" products = set() for permutation in [''.join(perm) for perm in permutations(digits)]: for i in range(1, 9): # use i as the position of the first cut for j in range(i+1, 9): # use j as the position of the second cut # split the permutation into parts a,b and c a = int(permutation[:i]) b = int(permutation[i:j]) c = int(permutation[j:]) if a * b == c: # we found a product where the concaternation of a, b, and c is pandigital products.add(c) print "Solution:", sum(products) |

Output:

1 |
Solution: 45228 |

**Performance improvements:**

The above approach is totally brute force but the idea is simple and it was fast to code. With a little bit of tweaking it can be made just a bit more efficient.

Looking at the equation a * b = c it becomes clear that:

- A factor (a or b) cannot have more digits than the product (c) has.

Thus we can limit the range of i for the possible positions of the first cut:

1for i in range(1, n/2+1): - For the same reason we can also limit the range of j for the position of the second cut:

1for j in range(i+1, n/2+i):

Last, we can exploit the ordering of the generated permutations that we saw in the first output. The permutation of “123456789” builds up gradually starting at the end of the string. And when we loop over all permutations and encounter the first permutation starting with the digit “5” we know that the product (c) went trough all possible values and we won’t find any new ones. This allows us to skip half of the generated permutations.

**Final Solution:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from itertools import permutations digits = "123456789" products = set() n = len(digits) for permutation in [''.join(perm) for perm in permutations(digits)]: if permutation[0] == '5': break for i in range(1, n/2+1): # n/2 because: a factor can not have more digits than the product for j in range(i+1, n/2+i): # +n/2 because: a factor can not have more digits than the product a = int(permutation[:i]) b = int(permutation[i:j]) c = int(permutation[j:]) if a * b == c: print "%d * %d = %d" % (a, b, c) products.add(c) print "Solution:", sum(products) |

Final Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
12 * 483 = 5796 138 * 42 = 5796 157 * 28 = 4396 159 * 48 = 7632 18 * 297 = 5346 186 * 39 = 7254 198 * 27 = 5346 27 * 198 = 5346 28 * 157 = 4396 297 * 18 = 5346 39 * 186 = 7254 4 * 1738 = 6952 4 * 1963 = 7852 42 * 138 = 5796 48 * 159 = 7632 483 * 12 = 5796 Solution: 45228 |

Find more of my Project Euler Solutions on GitHub.

Material taken from projecteuler.net is licensed under Attribution-NonCommercial-ShareAlike 2.0 UK: England & Wales. More info here.

## Hacked By GeNErAL

**Greetz : Kuroi’SH, RxR, K3L0T3X **

```
```

`\!/Just for Fun ~Hacked By GeNErAL\!/`

Hacked By GeNErAL! !