The post Project Euler 162 Solution appeared first on Dreamshire.
]]>In the hexadecimal number system numbers are represented using 16 different digits:
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
The hexadecimal number AF when written in the decimal number system equals 10×16+15=175.
In the 3-digit hexadecimal numbers 10A, 1A0, A10, and A01 the digits 0,1 and A are all present.
Like numbers written in base ten we write hexadecimal numbers without leading zeroes.
How many hexadecimal numbers containing at most sixteen hexadecimal digits exist with all of the digits 0,1, and A present at least once?
Give your answer as a hexadecimal number.
(A,B,C,D,E and F in upper case, without any leading or trailing code that marks the number as hexadecimal and without leading zeroes , e.g. 1A3F and not: 1a3f and not 0x1a3f and not $1A3F and not #1A3F and not 0000001A3F)
This is a typical application of the inclusion-exclusion principle. It is helpful in answering questions where the number of elements of a set are required but not the elements themselves. We just need to know the number of elements in two sets and subtract the number in the intersection.
The equation, after combining some terms looks like: 15 x 16^{(n-1)} + 41 x 14^{(n-1)} – (43 x 15^{(n-1)} + 13^{n}) for each possible hex number containing n digits {3..16}.
Since the first digit is non zero the total possibilities without consideration for the constraints is 15 x 16^{(n-1)}. Then remove those with no {0, 1, A}. But we need to add back in those that have no 1 or 0, no 1 or A, no 0 or A. Then remove the duplicates that, again, have no {0, 1, A}. There are 258 4 digit hex numbers that follow this pattern.
The post Project Euler 162 Solution appeared first on Dreamshire.
]]>The post Project Euler 163 Solution appeared first on Dreamshire.
]]>Project Euler 163: Consider an equilateral triangle in which straight lines are drawn from each vertex to the middle of the opposite side, such as in the size 1 triangle in the sketch below.
Sixteen triangles of either different shape or size or orientation or location can now be observed in that triangle. Using size 1 triangles as building blocks, larger triangles can be formed, such as the size 2 triangle in the above sketch. One-hundred and four triangles of either different shape or size or orientation or location can now be observed in that size 2 triangle.
It can be observed that the size 2 triangle contains 4 size 1 triangle building blocks. A size 3 triangle would contain 9 size 1 triangle building blocks and a size n triangle would thus contain n^{2} size 1 triangle building blocks.
If we denote T(n) as the number of triangles present in a triangle of size n, then
T(1) = 16
T(2) = 104
Find T(36).
The post Project Euler 163 Solution appeared first on Dreamshire.
]]>The post Project Euler 172 Solution appeared first on Dreamshire.
]]>How many 18-digit numbers n (without leading zeros) are there such that no digit occurs more than three times in n?
from Euler import binomial
digits, base, max_r = 18, 10, 3
def nd(d,b):
if b>1:
return sum(nd(d-r, b-1)*binomial(d,r) for r in xrange(min(d+1, max_r+1)))
return d <= max_r
print "There are", nd(digits, base) * (base-1)/base,
print digits, "digit numbers such that no digit occurs more than", max_r,
print "times in base", base
Use this link to get the Project Euler 172 Solution Python source.The post Project Euler 172 Solution appeared first on Dreamshire.
]]>The post Project Euler 173 Solution appeared first on Dreamshire.
]]>We shall define a square lamina to be a square outline with a square "hole" so that the shape possesses vertical and horizontal symmetry. For example, using exactly thirty-two square tiles we can form two different square laminae:
With one-hundred tiles, and not necessarily using all of the tiles at one time, it is possible to form forty-one different square laminae.
Using up to one million tiles how many different square laminae can be formed?
Ran a brute force for the first 10,000 tiles and found the series. So the next step is to form a generating function to calculate the answer, but, alas, this is unlikely to ever happen.
The post Project Euler 173 Solution appeared first on Dreamshire.
]]>The post Project Euler 174 Solution appeared first on Dreamshire.
]]>We shall define a square lamina to be a square outline with a square "hole" so that the shape possesses vertical and horizontal symmetry.
Given eight tiles it is possible to form a lamina in only one way: 3×3 square with a 1×1 hole in the middle. However, using thirty-two tiles it is possible to form two distinct laminae.
If t represents the number of tiles used, we shall say that t = 8 is type L(1) and t = 32 is type L(2).
Let N(n) be the number of t ≤ 1000000 such that t is type L(n); for example, N(15) = 832.
What is ∑ N(n) for 1 ≤ n ≤ 10?
Wanting a solution that could solve both problem 173 and 174 we decided that in a couple of minutes we could put something together that would run quickly. Instead of determining factors for each iteration we just modeled the building of the lamina which is faster than a solution counting even factors. In fact when the scale increases to, say 10,000,000 this program will outperform most others.
One drawback is the use of memory, but for the sake of development time it was of little consequence.
#Project Euler Problem 174
L = 1000000
N = [0]*(L+1)
for h in range(1, L//4):
nt = h*4 + 4
sumx = nt
while sumx <= L:
N[sumx] += 1
nt = nt + 8
sumx += nt
print "Project Euler 174 Solution =", sum(1 <= n <= 10 for n in N)
Use this link to get the Project Euler 174 Solution Python 2.7 source.The post Project Euler 174 Solution appeared first on Dreamshire.
]]>The post Project Euler 179 Solution appeared first on Dreamshire.
]]>Find the number of integers 1 < n < 10^{7}, for which n and n + 1 have the same number of positive divisors. For example, 14 has the positive divisors 1, 2, 7, 14 while 15 has 1, 3, 5, 15.
Let’s take a subset of this problem and define the range to include the integers 1 < n ≤ 25. A simple way to find the number of divisors is to increment an array element for every n integers by one for each multiple of the divisors, not including 1 or itself. We need only check divisors from 2 to int(n/2).
That is, add 1 to each multiple of 2: 4, 6, 8, … then 3: 6, 9, 12, … then 4: 8, 12, 16, … and so on until we reach 12. Here’s a table to show the results:
n | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | # Divisors |
2 | 0 | |||||||||||
3 | 0 | |||||||||||
4 | 1 | 1 | ||||||||||
5 | 0 | |||||||||||
6 | 1 | 1 | 2 | |||||||||
7 | 0 | |||||||||||
8 | 1 | 1 | 2 | |||||||||
9 | 1 | 1 | ||||||||||
10 | 1 | 1 | 2 | |||||||||
11 | 0 | |||||||||||
12 | 1 | 1 | 1 | 1 | 4 | |||||||
13 | 0 | |||||||||||
14 | 1 | 1 | 2 | |||||||||
15 | 1 | 1 | 2 | |||||||||
16 | 1 | 1 | 1 | 3 | ||||||||
17 | 0 | |||||||||||
18 | 1 | 1 | 1 | 1 | 4 | |||||||
19 | 0 | |||||||||||
20 | 1 | 1 | 1 | 1 | 4 | |||||||
21 | 1 | 1 | 2 | |||||||||
22 | 1 | 1 | 2 | |||||||||
23 | 0 | |||||||||||
24 | 1 | 1 | 1 | 1 | 1 | 1 | 6 | |||||
25 | 1 | 1 |
Note how you can identify the prime numbers as those with zero divisors (ignoring 1 and itself).
Now, check adjacent array elements for equality and you’ll have your answer. In this simple case of L=25, the answer would be 3. We’ve highlighted the results above.
L = 10**7
n = [0]*(L)
for i in range(2, L//2):
for j in range(i*2, L, i):
n[j] += 1
print "Consecutive divisors =", sum(n[i] == n[i - 1] for i in range(3, L))
Use this link to get the Project Euler 179 Solution pypy source.The post Project Euler 179 Solution appeared first on Dreamshire.
]]>The post Project Euler 182 Solution appeared first on Dreamshire.
]]>The RSA encryption is based on the following procedure:
Generate two distinct primes p and q.
Compute n=pq and φ=(p-1)(q-1).
Find an integer e, 1<e<φ, such that gcd(e,φ)=1.
A message in this system is a number in the interval [0,n-1].
A text to be encrypted is then somehow converted to messages (numbers in the interval [0,n-1]).
To encrypt the text, for each message, m, c=m^{e} mod n is calculated.
To decrypt the text, the following procedure is needed: calculate d such that ed=1 mod φ, then for each encrypted message, c, calculate m=c^{d} mod n.
There exist values of e and m such that m^{e} mod n=m.
We call messages m for which m^{e} mod n=m unconcealed messages.
An issue when choosing e is that there should not be too many unconcealed messages.
For instance, let p=19 and q=37.
Then n=19*37=703 and φ=18*36=648.
If we choose e=181, then, although gcd(181,648)=1 it turns out that all possible messages
m (0≤m≤n-1) are unconcealed when calculating m^{e} mod n.
For any valid choice of e there exist some unconcealed messages.
It’s important that the number of unconcealed messages is at a minimum.
Choose p=1009 and q=3643.
Find the sum of all values of e, 1<e<φ(1009,3643) and gcd(e,φ)=1, so that the number of unconcealed messages for this value of e is at a minimum.
At the heart of this problem is the simple formula: [gcd(e-1,p-1)+1][gcd(e-1,q-1)+1] as outlined in many texts such as Recent Advances in RCA Cryptography in the chapter Properties of the RSA cryptosystem. Specifically Lemma 5.1 on page 69 shows a nice proof of this equation.
So encoding this into a program is straightforward and simple. Just sum all values of e where gcd(e,phi) = 1 and gcd(e-1,q-1) and gcd(e-1,p-1) are at a minimum (equal 2).
from Euler import gcd
p, q, s, e = 1009, 3643, 0, 3
phi = (p-1) * (q-1)
while (e < phi):
if gcd(e, phi)==1 and gcd(e-1, q-1)==2 and gcd(e-1, p-1)==2:
s += e
e += 4
print "Project Euler 182 Solution =", s
Use this link to get the Project Euler 182 Solution Python 2.7 source.gcd
is listed in Common Functions and Routines for Project Euler.The post Project Euler 182 Solution appeared first on Dreamshire.
]]>The post Project Euler 183 Solution appeared first on Dreamshire.
]]>Let N be a positive integer and let N be split into k equal parts, r = N/k, so that N = r + r + … + r.
Let P be the product of these parts, P = r × r × … × r = r^{k}.
For example, if 11 is split into five equal parts, 11 = 2.2 + 2.2 + 2.2 + 2.2 + 2.2, then P = 2.2^{5} = 51.53632.
Let M(N) = P_{max} for a given value of N.
It turns out that the maximum for N = 11 is found by splitting eleven into four equal parts which leads to P_{max} = (11/4)^{4}; that is, M(11) = 14641/256 = 57.19140625, which is a terminating decimal.
However, for N = 8 the maximum is achieved by splitting it into three equal parts, so M(8) = 512/27, which is a non-terminating decimal.
Let D(N) = N if M(N) is a non-terminating decimal and D(N) = -N if M(N) is a terminating decimal.
For example, ΣD(N) for 5 ≤ N ≤ 100 is 2438.
Find ΣD(N) for 5 ≤ N ≤ 10000.
The post Project Euler 183 Solution appeared first on Dreamshire.
]]>The post Project Euler 188 Solution appeared first on Dreamshire.
]]>The hyperexponentiation or tetration of a number a by a positive integer b, denoted by a↑↑b or ^{b}a, is recursively defined by:
a↑↑1 = a,
a↑↑(k+1) = a^{(a↑↑k)}.
Thus we have e.g. 3↑↑2 = 3^{3} = 27, hence 3↑↑3 = 3^{27} = 7625597484987 and 3↑↑4 is roughly 10^{3.6383346400240996*10^12}.
Find the last 8 digits of 1777↑↑1855.
Using the optional third parameter of Python’s pow(x, y [,z]) function which calculates the modulo z of x to the power y (computed more efficiently than pow(x, y) % z) we loop through the pow function b times to calculate the tetration of a↑↑b. Modding the result by some power of 10, 10^{8} in this case, we can reduce this unwieldy number to its last 8 digits.
We can exit the loop early, reducing the number of iterations, when the modded result begins to repeat.
The post Project Euler 188 Solution appeared first on Dreamshire.
]]>The post Project Euler 190 Solution appeared first on Dreamshire.
]]>Project Euler 190: Let S_{m} = (x_{1}, x_{2}, … , x_{m}) be the m-tuple of positive real numbers with x_{1} + x_{2} + … + x_{m} = m for which P_{m} = x_{1} * x_{2}^{2} * … * x_{m}^{m} is maximised.
For example, it can be verified that [P_{10}] = 4112 ([ ] is the integer part function).
Find Σ[P_{m}] for 2 ≤ m ≤ 15.
The post Project Euler 190 Solution appeared first on Dreamshire.
]]>