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 288 Solution appeared first on Dreamshire.
]]>For any prime p the number N(p,q) is defined by
N(p,q) = ∑_{n=0 to q} T_{n}*p^{n}
with T_{n} generated by the following random number generator:
S_{0} = 290797
S_{n+1} = S_{n}^{2} mod 50515093
T_{n} = S_{n} mod p
Let Nfac(p,q) be the factorial of N(p,q).
Let NF(p,q) be the number of factors p in Nfac(p,q).
You are given that NF(3,10000) mod 3^{20}=624955285.
Find NF(61,10^{7}) mod 61^{10}
The post Project Euler 288 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.
The post Project Euler 231 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 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 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.”
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.
]]>The post Project Euler 250 Solution appeared first on Dreamshire.
]]>Find the number of non-empty subsets of {1^{1}, 2^{2}, 3^{3},…, 250250^{250250}}, the sum of whose elements is divisible by 250. Enter the rightmost 16 digits as your answer.
What I thought was a novel and unique DP solution turned out to be one of the most popular ways to solve this problem. It’s solution is very similar to problems 31 and 76.
Setting up the frequency array of modulus powers was an early observation. It has an interesting pattern that could be used to further optimize the program, but adding the code to do so may obscure the simple premise used below.
See the frequency table is in the comments section.
Also, you have to subtract one from the final result to exclude the empty set. It sums to 0 and is divisible by 250. Not accounting for this will cause much confusion, frustration and anger.
freq, t, tx = [0]*m, [0]*m, [0]*m
. . .
for k in range(m):
tx[k] = (t[k] + t[(k-i)]) % 10**16
t, tx = tx, t
freq, t = [0]*m, [0]*m
. . .
t = [(t[k] + t[(k-i)]) % 10**16 for k in range(m)]
Frequency (freq) array:
[25025, 1001, 0, 1001, 1001, 0, 3003, 1002, 0, 1001, 0, 1001, 0, 1001, 1000, 0, 3002, 1002, 0, 1001, 0,
1001, 0, 1001, 1000, 0, 3002, 1001, 0, 1001, 0, 1001, 0, 1000, 1000, 0, 3002, 1001, 0, 1001, 0, 1001,
0, 1001, 1000, 0, 3003, 1002, 0, 1001, 0, 1001, 0, 1002, 1002, 0, 3003, 1001, 0, 1001, 0, 1001, 0,
1001, 1001, 0, 3004, 1001, 0, 1001, 0, 1001, 0, 1000, 1001, 0, 3003, 1001, 0, 1001, 0, 1001, 0, 1001,
1001, 0, 3004, 1001, 0, 1001, 0, 1001, 0, 1001, 1001, 0, 3003, 1001, 0, 1001, 0, 1001, 0, 1002, 1001,
0, 3003, 1000, 0, 1001, 0, 1001, 0, 1002, 1002, 0, 3002, 1000, 0, 1001, 0, 1001, 0, 1001, 1001, 25025,
3003, 1001, 0, 1001, 0, 1001, 0, 1002, 1002, 0, 3002, 1000, 0, 1001, 0, 1001, 0, 1002, 1001, 0, 3003,
1000, 0, 1001, 0, 1001, 0, 1001, 1001, 0, 3003, 1001, 0, 1001, 0, 1001, 0, 1001, 1000, 0, 3003, 1001,
0, 1001, 0, 1001, 0, 1001, 1001, 0, 3003, 1002, 0, 1001, 0, 1001, 0, 1001, 1000, 0, 3003, 1001, 0,
1001, 0, 1001, 0, 1001, 1001, 0, 3002, 1000, 0, 1001, 0, 1001, 0, 1000, 1001, 0, 3004, 1001, 0, 1001,
0, 1001, 0, 1001, 1002, 0, 3004, 1002, 0, 1001, 0, 1001, 0, 1001, 1002, 0, 3004, 1001, 0, 1001, 0,
1001, 0, 1000, 1002, 0, 3004, 1001, 0, 1001, 0, 1001, 0, 1000, 1001, 0, 3003, 1001, 0, 1001]
Full Answer:
1913632767165333770252234310750163069834160429303722710242211912885821718320699719386481255946529840771
3258880554002523809000550181004722164691990791796216105528723914179082619897309829010183163626973355855
5999306330315600310099410619568592185583809345129002163399193048347346020632534085343254677339405056184
7326428202599457670106733005406613103334980070826842988423932453124198773054240892462448194753422597637
6555562298413900685441068866107561528290456061485273224655196316704209155036705345243851360338784496710
2702168073260334966060683376208542265749847926446026321759054761268936741149541380503413974000836131117
3214555396194146231774532331102407509287029942492977551763290783785267079641982184476608545105167745590
2390241353210723566485746027488888458569654959430268044630917086876645201216270720136185137433124560024
4947237368359497976815295054111032824969593334891995366826402669957540356158979566968433103910065837234
7600252380629942243992227333522004241293221027559893329018350365802362213819847196012583447620759990629
0500807343271503733998277415625719154124582562365219998050976471531150922171863821067274781684862358971
6877531079379698868451514069003423205128173741148232662970193131629240486253267005757414501962383708763
4789370282599541846772457881907410198573338156795014690730557060377219108717480651566762078196494232718
4441019702657518888262582004442297863308082520897408190969125434496636680390937830047369772525876500386
6695072692075426517044629260507081158000321869747531690336924001456388953753220118461473200202913003821
3308573405644391091766581424660875716741854196533005965673813314809931088245808990069485886556688851346
4366843244471303419027893034670915768823776259993094129652301345812142548060209xxxx
replace the last four x’s with the last four digits of your answer.
The post Project Euler 250 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.
The post Project Euler 225 Solution appeared first on Dreamshire.
]]>