The post Project Euler 203 Solution appeared first on Dreamshire.
]]>The binomial coefficients ^{n}C_{k} can be arranged in triangular form, Pascal’s triangle, like this:
It can be seen that the first eight rows of Pascal’s triangle contain twelve distinct numbers: 1, 2, 3, 4, 5, 6, 7, 10, 15, 20, 21 and 35.
A positive integer n is called square-free if no square of a prime divides n.
Of the twelve distinct numbers in the first eight rows of Pascal’s triangle, all except 4 and 20 are square-free.
The sum of the distinct square-free numbers in the first eight rows is 105.
Find the sum of the distinct square-free numbers in the first 51 rows of Pascal’s triangle.
Only a few primes are required. For example, 51! is the biggest numerator (_{n}C_{k} = n! / k! (n-k)! ) and the largest prime factor required is ≤ √51 which is 7. You can add a few more to try bigger problems.
So we fill a set with distinct numbers from Pascal's triangle and iterate through the 614 binomial coefficients and track which ones are square-free, sum them up and print the results. I only wish my biometrics problems were this obvious.
The post Project Euler 203 Solution appeared first on Dreamshire.
]]>The post Project Euler 205 Solution appeared first on Dreamshire.
]]>Project Euler 205: Peter has nine four-sided (pyramidal) dice, each with faces numbered 1, 2, 3, 4.
Colin has six six-sided (cubic) dice, each with faces numbered 1, 2, 3, 4, 5, 6.
Peter and Colin roll their dice and compare totals: the highest total wins. The result is a draw if the totals are equal.
What is the probability that Pyramidal Pete beats Cubic Colin? Give your answer rounded to seven decimal places in the form 0.abcdefg
Type | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
9 four-sided dice | 0 | 0 | 0 | 1 | 9 | 45 | 165 | 486 | 1206 | 2598 | 4950 | 8451 | 13051 | 18351 | 23607 | 27876 | 30276 |
6 six-sided dice | 1 | 6 | 21 | 56 | 126 | 252 | 456 | 756 | 1161 | 1666 | 2247 | 2856 | 3431 | 3906 | 4221 | 4332 | 4221 |
Type | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
9 four-sided dice | 30276 | 27876 | 23607 | 18351 | 13051 | 8451 | 4950 | 2598 | 1206 | 486 | 165 | 45 | 9 | 1 |
6 six-sided dice | 3906 | 3431 | 2856 | 2247 | 1666 | 1161 | 756 | 456 | 252 | 126 | 56 | 21 | 6 | 1 |
After calculating the table above all that remains is finding the ratio of winning combinations to the total possible combinations. So the denominator will be (4^9)(6^6) and the numerator the total number of ways Pete out sums Colin (Pete’s index one more than Colin’s). Draws are inherently ignored, which was fortuitous.
Project Euler 205
The post Project Euler 205 Solution appeared first on Dreamshire.
]]>The post Project Euler 206 Solution appeared first on Dreamshire.
]]>Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0,
where each “_” is a single digit.
The square of any number, n, will end in 00 if the last digit of n is zero. So, our square root ends in a zero and the square has the form 1_2_3_4_5_6_7_8_900.
We can reduce our search to 1_2_3_4_5_6_7_8_9 and multiply the square root (when we find it) by 10. Now, the only way to end a square with 9 is to square a number ending with 3 or 7. OK, so our square root ends with 3 or 7, an odd number, and we can skip even-numbered n from consideration.
Let’s begin our search with the maximum possible number, 19293949596979899 and take its square root. Starting with an odd number we count down by 2 until the square fits our pattern.
The post Project Euler 206 Solution appeared first on Dreamshire.
]]>The post Project Euler 207 Solution appeared first on Dreamshire.
]]>For some positive integers k, there exists an integer partition of the form 4^{t} = 2^{t} + k,
where 4^{t}, 2^{t}, and k are all positive integers and t is a real number.
The first two such partitions are 4^{1} = 2^{1} + 2 and 4^{1.5849625…} = 2^{1.5849625…} + 6.
Partitions where t is also an integer are called perfect.
For any m ≥ 1 let P(m) be the proportion of such partitions that are perfect with k ≤ m.
Thus P(6) = 1/2.
In the following table are listed some values of P(m)
P(5) = 1/1
P(10) = 1/2
P(15) = 2/3
P(20) = 1/2
P(25) = 1/2
P(30) = 2/5
…
P(180) = 1/4
P(185) = 3/13
Find the smallest m for which P(m) < 1/12345
The post Project Euler 207 Solution appeared first on Dreamshire.
]]>The post Project Euler 219 Solution appeared first on Dreamshire.
]]>Let A and B be bit strings (sequences of 0’s and 1’s).
If A is equal to the leftmost length(A) bits of B, then A is said to be a prefix of B.
For example, 00110 is a prefix of 001101001, but not of 00111 or 100110.
A prefix-free code of size n is a collection of n distinct bit strings such that no string is a prefix of any other. For example, this is a prefix-free code of size 6:
0000, 0001, 001, 01, 10, 11
Now suppose that it costs one penny to transmit a ‘0’ bit, but four pence to transmit a ‘1’.
Then the total cost of the prefix-free code shown above is 35 pence, which happens to be the cheapest possible for the skewed pricing scheme in question.
In short, we write Cost(6) = 35.
What is Cost(10^{9}) ?
The post Project Euler 219 Solution appeared first on Dreamshire.
]]>The post Project Euler 225 Solution appeared first on Dreamshire.
]]>
The sequence 1, 1, 1, 3, 5, 9, 17, 31, 57, 105, 193, 355, 653, 1201 …
is defined by T_{1} = T_{2} = T_{3} = 1 and T_{n} = T_{n-1} + T_{n-2} + T_{n-3}.
It can be shown that 27 does not divide any terms of this sequence.
In fact, 27 is the first odd number with this property.
Find the 124^{th} odd number that does not divide any terms of the above sequence.
Knowing this series is periodic we have to expand it modulo N until it begins to repeat again to (1,1,1) before having any zero calculation to accept it as not being able to divide our sequence. A zero calculation would indicate that N can divide a term and would be excluded from our list. This process is finite but could take many iterations.
def prob225(n):
c, i = 0, 27
while c < n:
t1, t2, t3 = 1, 1, 3
while t3>0 and t1 * t2 * t3 != 1:
t1, t2, t3 = t2, t3, (t1 + t2 + t3) % i
if t3>0: c+=1
i+=2
return i-2
print 'Project Euler 225 Solution = ', prob225(124)
Use this link to get the Project Euler 225 Solution Python 2.7 source.The post Project Euler 225 Solution appeared first on Dreamshire.
]]>The post Project Euler 231 Solution appeared first on Dreamshire.
]]>The binomial coefficient ^{10}C_{3} = 120. ,
120 = 2^{3} ×3 ×5 = 2 × 2 × 2 × 3 × 5, and 2 + 2 + 2 + 3 + 5 = 14.
So the sum of the terms in the prime factorisation of ^{10}C_{3} is 14.
Find the sum of the terms in the prime factorisation of ^{20000000}C_{15000000}.
Calculating the binomial in this problem and factoring it would take too long so another method had to be discovered.
The paper “Prime Factors of Binomial Coefficients” by Samin Riasat explains Legendre's theorem for finding the prime factors and their exponents for factorials and this extends nicely to binomial coefficients.
An application of Legendre’s theorem is to find how many zeros are there at the end of a factorial.
Let’s say you want to find the number of zeros at the end of 1000! You just need to find how many times the factors 2 AND 5 (because 2 × 5 = 10) appear in the prime factorization of 1000!. Since 2 factors many more numbers without 5 but is always required with 5 in order to count the zeros we just concern ourselves with 5. To paraphrase the paper:
More generally:
This method was used to solve SPOJ 11 Trailing zeros in a factorial.
Below: Legendre’s extension used in the program’s loop to calculate the prime’s exponent, if it’s a factor. Evaluates to either 1 or 0 and then summed:
I wanted to further improve performance and studied Kummer’s method for calculating the exponents of the prime factors but it didn’t seem like it would make it any faster.
from Euler import prime_sieve
n, r, s = 20000000, 15000000, 0
for p in prime_sieve(n):
pj = p
while pj <= n:
s += p * (n//pj - r//pj - (n - r)//pj)
pj *= p
print "Project Euler 231 Solution =", s
Use this link to get the Project Euler 231 Solution Python 2.7 source.The post Project Euler 231 Solution appeared first on Dreamshire.
]]>The post Project Euler 242 Solution appeared first on Dreamshire.
]]>Given the set {1,2,…,n}, we define f(n,k) as the number of its k-element subsets with an odd sum of elements. For example, f(5,3) = 4, since the set {1,2,3,4,5} has four 3-element subsets having an odd sum of elements, i.e.: {1,2,4}, {1,3,5}, {2,3,4} and {2,4,5}.
When all three values n, k and f(n,k) are odd, we say that they make
an odd-triplet [n,k,f(n,k)].
There are exactly five odd-triplets with n ≤ 10, namely:
[1,1,f(1,1) = 1], [5,1,f(5,1) = 3], [5,5,f(5,5) = 1], [9,1,f(9,1) = 5] and [9,9,f(9,9) = 1].
How many odd-triplets are there with n ≤ 10^{12} ?
It turns out that the “odd triplet” is simply part of the Sierpinski triangle with a formula to calculate the number of triplets less than a given n.
Another way to create the Sierpinski triangle is via Pascals triangle. If you color the odd numbers in Pascal’s triangle and shade out all the other spaces, you will see Sierpinski’s triangle.
The post Project Euler 242 Solution appeared first on Dreamshire.
]]>The post Project Euler 243 Solution appeared first on Dreamshire.
]]>A positive fraction whose numerator is less than its denominator is called a proper fraction.
For any denominator, d, there will be d−1 proper fractions; for example, with d = 12:
^{1}/_{12} , ^{2}/_{12} , ^{3}/_{12} , ^{4}/_{12} , ^{5}/_{12} , ^{6}/_{12} , ^{7}/_{12} , ^{8}/_{12} , ^{9}/_{12} , ^{10}/_{12} , ^{11}/_{12} .
We shall call a fraction that cannot be cancelled down a resilient fraction.
Furthermore we shall define the resilience of a denominator, R(d), to be the ratio of its proper fractions that are resilient; for example, R(12) = ^{4}/_{11} .
In fact, d = 12 is the smallest denominator having a resilience R(d) < ^{4}/_{10} .
Find the smallest denominator d, having a resilience R(d) < ^{15499}/_{94744} .
As you look at the list below some patterns become obvious.
1) All prime numbers, p, have an R(p) of 1.
2) The first and last fraction are always resilient. Always.
3) All fractions with a prime numerator that is not a factor of the denominator are resilient. So, for example, 5/8 is resilient where as 5/10 is not.
R(2) = 1/1 : 1/2
R(3) = 2/2 : 1/3 , 2/3
R(4) = 2/3 : 1/4 , 2/4 , 3/4
R(5) = 4/4 : 1/5 , 2/5 , 3/5 , 4/5
R(6) = 2/5 : 1/6 , 2/6 , 3/6 , 4/6 , 5/6
R(7) = 6/6 : 1/7 , 2/7 , 3/7 , 4/7 , 5/7 , 6/7
R(8) = 4/7 : 1/8 , 2/8 , 3/8 , 4/8 , 5/8 , 6/8 , 7/8
R(9) = 6/8 : 1/9 , 2/9 , 3/9 , 4/9 , 5/9 , 6/9 , 7/9 , 8/9
R(10) = 4/9 : 1/10 , 2/10 , 3/10 , 4/10 , 5/10 , 6/10 , 7/10 , 8/10 , 9/10
R(11) = 10/10 : 1/11 , 2/11 , 3/11 , 4/11 , 5/11 , 6/11 , 7/11 , 8/11 , 9/11 , 10/11
R(12) = 4/11 : 1/12 , 2/12 , 3/12 , 4/12 , 5/12 , 6/12 , 7/12 , 8/12 , 9/12 , 10/12 , 11/12 .
This is the same pattern that presented themselves in problems 69 and 70 where we were minimizing and maximizing n/φ(n). Here we limit φ(d)/(d-1) to 15499/94744.
This reduces the problem, as before, to multiplying sequences of primes. Which primes becomes a trial and error but multiplying over 23 was too much. The range was some multiple of the the product of the prime number from 2 to 23.
Mathworld had a great explanation about the totient function and proved how this would work using the formula below:
So, the easiest way to solve these type of problems would be to multiply as many sequential primes together until φ(d)/(d-1) exceeds the ratio given. Then back down one prime and try smaller primes.
Here’s an example: For this problem we multiply sequential primes up to 29 which exceeds the ratio. We back down one prime to 23 and call the new product (2 x 3 x 5 x … x 23) p’. We now multiply p” by composite numbers (<29): 4(2x2), 6(2x3), 8(2x2x2), 9(3x3) until it exceed the ratio. We remove the last multiplication and the answer is served. [solution tid="abdc2ed36c" t_height="325"] [comments] [/comments]
The post Project Euler 243 Solution appeared first on Dreamshire.
]]>The post Project Euler 249 Solution appeared first on Dreamshire.
]]>Let S = {2, 3, 5, …, 4999} be the set of prime numbers less than 5000.
Find the number of subsets of S, the sum of whose elements is a prime number.
Enter the rightmost 16 digits as your answer.
This problem is easy to conceptualize but difficult to get to run under one minute. I stuck with Python and there is room for optimization but it may still be a challenge. I’ll get back to it someday, which is programmer speak for “It works and I’ve got much better things to do.”
from Euler import prime_sieve, is_prime
primes = prime_sieve(5000)
t = [1] + [0] * sum(primes)
sp = 0
for p in primes:
sp += p
for j in range(sp, p-1, -1):
t[j] = (t[j] + t[j-p])
print "Project Euler 249 Solution =", (sum(t[p] for p in range(sp) if is_prime(p)) % 10**16)
Use this link to get the Project Euler 249 Solution Pypy source.18089131734384088361396707178609119521538648663874965565252
81728260738457913373744890199439933572129728883223063765155
24683070889481179665713632474523500808965429134707479096569
03114415927526256425xxxx
replace the last four x’s with the last four digits of your answer.
The post Project Euler 249 Solution appeared first on Dreamshire.
]]>