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 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 371 Solution appeared first on Dreamshire.
]]>Oregon licence plates consist of three letters followed by a three digit number (each digit can be from [0..9]).
While driving to work Seth plays the following game:
Whenever the numbers of two licence plates seen on his trip add to 1000 that’s a win.
E.g. MIC-012 and HAN-988 is a win and RYU-500 and SET-500 too. (as long as he sees them in the same trip).
Find the expected number of plates he needs to see for a win.
Give your answer rounded to 8 decimal places behind the decimal point.
Note: We assume that each licence plate seen is equally likely to have any three digit number on it.
The post Project Euler 371 Solution appeared first on Dreamshire.
]]>The post Project Euler 140 Solution appeared first on Dreamshire.
]]>Consider the infinite polynomial series A_{G}(x) = xG_{1} + x^{2}G_{2} + x^{3}G_{3} + …, where G_{k} is the kth term of the second order recurrence relation G_{k} = G_{k−1} + G_{k−2}, G_{1} = 1 and G_{2} = 4; that is, 1, 4, 5, 9, 14, 23, … .
For this problem we shall be concerned with values of x for which A_{G}(x) is a positive integer.
The corresponding values of x for the first five natural numbers are shown below.
We shall call A_{G}(x) a golden nugget if x is rational, because they become increasingly rarer; for example, the 20th golden nugget is 211345365.
Find the sum of the first thirty golden nuggets.
The post Project Euler 140 Solution appeared first on Dreamshire.
]]>The post Project Euler 327 Solution appeared first on Dreamshire.
]]>A series of three rooms are connected to each other by automatic doors.
Each door is operated by a security card. Once you enter a room the door automatically closes and that security card cannot be used again. A machine at the start will dispense an unlimited number of cards, but each room (including the starting room) contains scanners and if they detect that you are holding more than three security cards or if they detect an unattended security card on the floor, then all the doors will become permanently locked. However, each room contains a box where you may safely store any number of security cards for use at a later stage.
If you simply tried to travel through the rooms one at a time then as you entered room 3 you would have used all three cards and would be trapped in that room forever!
However, if you make use of the storage boxes, then escape is possible. For example, you could enter room 1 using your first card, place one card in the storage box, and use your third card to exit the room back to the start. Then after collecting three more cards from the dispensing machine you could use one to enter room 1 and collect the card you placed in the box a moment ago. You now have three cards again and will be able to travel through the remaining three doors. This method allows you to travel through all three rooms using six security cards in total.
It is possible to travel through six rooms using a total of 123 security cards while carrying a maximum of 3 cards.
Let C be the maximum number of cards which can be carried at any time.
Let R be the number of rooms to travel through.
Let M(C,R) be the minimum number of cards required from the dispensing machine to travel through R rooms carrying up to a maximum of C cards at any time.
For example, M(3,6)=123 and M(4,6)=23.
And, ΣM(C,6)=146 for 3 ≤ C ≤ 4.
You are given that ΣM(C,10)=10382 for 3 ≤ C ≤ 10.
Find ΣM(C,30) for 3 ≤ C ≤ 40.
The post Project Euler 327 Solution appeared first on Dreamshire.
]]>The post Project Euler 147 Solution appeared first on Dreamshire.
]]>In a 3×2 cross-hatched grid, a total of 37 different rectangles could be situated within that grid as indicated in the sketch.
There are 5 grids smaller than 3×2, vertical and horizontal dimensions being important, i.e. 1×1, 2×1, 3×1, 1×2 and 2×2. If each of them is cross-hatched, the following number of different rectangles could be situated within those smaller grids:
1×1: 1
2×1: 4
3×1: 8
1×2: 4
2×2: 18
Adding those to the 37 of the 3×2 grid, a total of 72 different rectangles could be situated within 3×2 and smaller grids.
How many different rectangles could be situated within 47×43 and smaller grids?
1. Count the number of horizontal and vertical rectangles in the grid.
2. Count the number of diagonal rectangles in the grid. Since m ≥ n, you can safely exchange m and n if this isn’t already the case.
3. Add the two together for a total for each iteration of m and n. The sum of this series is the answer.
Example: for a 2×3 grid you have 18 horizontal and vertical rectangles and 19 diagonal rectangles for a total of 37.
The post Project Euler 147 Solution appeared first on Dreamshire.
]]>The post Project Euler 102 Solution appeared first on Dreamshire.
]]>Three distinct points are plotted at random on a Cartesian plane, for which -1000 ≤ x, y ≤ 1000, such that a triangle is formed.
Consider the following two triangles:
It can be verified that triangle ABC contains the origin, whereas triangle XYZ does not.
Using triangles.txt (right click and ‘Save Link/Target As…’), a 27K text file containing the co-ordinates of one thousand "random" triangles, find the number of triangles for which the interior contains the origin.
NOTE: The first two examples in the file represent the triangles in the example given above.
There are a few ways to solve this problem. With having the point to check be the origin (0,0), then the easiest way is to check that the signs of the cross products are all positive or negative. This solution could be easily modified to check points other than the origin.
import urllib2
file_url = 'https://projecteuler.net/project/resources/p102_triangles.txt'
fh = urllib2.urlopen(file_url)
C = 0
for line in fh:
ax, ay, bx, by, cx, cy = map(int, line.split(','))
a = ax*by - ay*bx > 0
b = bx*cy - by*cx > 0
c = cx*ay - cy*ax > 0
C+= a==b==c
print "Number of triangles that contain the origin", C
Use this link to get the Project Euler 102 Solution Python 2.7 source.The post Project Euler 102 Solution appeared first on Dreamshire.
]]>The post Project Euler 95 Solution appeared first on Dreamshire.
]]>The proper divisors of a number are all the divisors excluding the number itself. For example, the proper divisors of 28 are 1, 2, 4, 7, and 14. As the sum of these divisors is equal to 28, we call it a perfect number.
Interestingly the sum of the proper divisors of 220 is 284 and the sum of the proper divisors of 284 is 220, forming a chain of two numbers. For this reason, 220 and 284 are called an amicable pair.
Perhaps less well known are longer chains. For example, starting with 12496, we form a chain of five numbers:
12496 → 14288 → 15472 → 14536 → 14264 (→ 12496 → …)
Since this chain returns to its starting point, it is called an amicable chain.
Find the smallest member of the longest amicable chain with no element exceeding one million.
A very straightforward approach that follows a logical flow similar to problem 21 by sieving the sum of divisors instead of using a function to calculate them.
1. Sieve a set of divisors for all numbers up to the limit. Uses a bunch of memory which is a drawback to solving larger problems. But I can mostly guess what the answer is for 10^{99} because things don’t really change much.
2. The easy part is to calculate successive members of the chain and slice it where the chain begins to repeat.
3. Collect the smallest member for each new increasing chain length.
4. Print out the results.
def pe95(L = 1000000):
d = [1] * L
for i in xrange(2, L//2):
for j in xrange(2*i, L, i):
d[j] += i
max_cl = 0
for i in xrange(2, L):
n, chain = i, []
while d[n] < L:
d[n], n = L+1, d[n]
try: k = chain.index(n)
except ValueError: chain.append(n)
else:
if len(chain[k:]) > max_cl:
max_cl, min_link = len(chain[k:]), min(chain[k:])
return min_link
print "Smallest member of the longest amicable chain", pe95()
Use this link to get the Project Euler 95 Solution Python 2.7 source.The post Project Euler 95 Solution appeared first on Dreamshire.
]]>The post Project Euler 93 Solution appeared first on Dreamshire.
]]>By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and making use of the four arithmetic operations (+, −, *, /) and brackets/parentheses, it is possible to form different positive integer targets.
For example,
8 = (4 * (1 + 3)) / 2
14 = 4 * (3 + 1 / 2)
19 = 4 * (2 + 3) − 1
36 = 3 * 4 * (2 + 1)
Note that concatenations of the digits, like 12 + 34, are not allowed.
Using the set, {1, 2, 3, 4}, it is possible to obtain thirty-one different target numbers of which 36 is the maximum, and each of the numbers 1 to 28 can be obtained before encountering the first non-expressible number.
Find the set of four distinct digits, a < b < c < d, for which the longest set of consecutive positive integers, 1 to n, can be obtained, giving your answer as a string: abcd.
Well, this is just a complicated conglomeration of combinations, permutations and products of expressions that quickly finds a maximum sequence size and the associated terms that yield that maximum.
1. Define combinations of terms using the integers from 1 to 9. There aren’t that many, just 126 sets from (1,2,3,4) to (6, 7, 8, 9).
2. Permute each combination. (1,2,3,4), (1,2,4,3), (1,4,2,3), etc.
3. Create an iterator of operators using the product method that look like (+++, ++*, ++-, ++/, +*+, etc.) and calculate each expression in such a way that we avoid a div by zero. Oh, BTW, use truediv instead of div, that cost me some time not knowing the difference.
4. Collect a maximum sequence size, and sequence length.
5. Fin. This was a bit difficult to get my head around at first, but just stated coding and it fell together. Yeah, there are only two equations required – not five. Graph the possibilities and check out the relationship between nodes. This works perfectly to solve this problem.
from operator import add, sub, mul, truediv
import itertools
def seq_length(s, c=1):
while c in s: c+= 1
return c-1
maxt, maxs = 0, 0
for terms in itertools.combinations(range(1, 10), 4):
s = set()
for n in itertools.permutations(terms):
for op in itertools.product([add, mul, sub, truediv], repeat=3):
x = op[0](op[1](n[0],n[1]),op[2](n[2],n[3])) # (a.b).(c.d)
if x%1 == 0 and x > 0: s.add(int(x))
x = op[0](op[1](op[2](n[0],n[1]),n[2]),n[3]) # ((a.b).c).d
if x%1 == 0 and x > 0: s.add(int(x))
if seq_length(s) > maxs: maxs, maxt = seq_length(s), terms
print "Terms that produce longest set of consecutive digits", ''.join(str(i) for i in maxt)
Use this link to get the Project Euler 93 Solution Python 2.7 source.The post Project Euler 93 Solution appeared first on Dreamshire.
]]>The post Project Euler 98 Solution appeared first on Dreamshire.
]]>By replacing each of the letters in the word CARE with 1, 2, 9, and 6 respectively, we form a square number: 1296 = 36^{2}. What is remarkable is that, by using the same digital substitutions, the anagram, RACE, also forms a square number: 9216 = 96^{2}. We shall call CARE (and RACE) a square anagram word pair and specify further that leading zeroes are not permitted, neither may a different letter have the same digital value as another letter.
Using words.txt (right click and ‘Save Link/Target As…’), a 16K text file containing nearly two-thousand common English words, find all the square anagram word pairs (a palindromic word is NOT considered to be an anagram of itself).
What is the largest square number formed by any member of such a pair?
NOTE: All anagrams formed must be contained in the given text file.
A simple and straightforward implementation that performs as follows:
1. Read words into a list from network resource. Trim first and last quote characters. Store each word as a tuple: the word and a list of the sorted letters in the word. Ignore words shorter than 5 letters (ignoring quotes).
2. Build anagram word pair list for the word list. Clearly, 3 and fewer letter words are non-contenders, nor are 4 letter words. We are given 96^{2}, and 97^{2}, 98^{2}, and 99^{2} wouldn’t work because they repeat a digit or contain the dreaded zero and unable to leave a maximum greater than the given 9216.
3. Find all combinations of values using the pattern set in word’s anagram. You could check duplicate patterns for the same words size and ignore those, but I didn’t do that. Also note that I’m convinced zero will never be a factor.
4. Collect maximum square values for an ultimate global maximum.
5. Print the maximum.
import urllib2, itertools
def sq(n):
x = int(''.join(y[letter_set[i]] for i in n))
return x if int(x**0.5)**2 == x else False
file_url = "https://projecteuler.net/project/resources/p098_words.txt"
words = [(w[1:-1], sorted(w[1:-1]))
for w in urllib2.urlopen(file_url).read().split(',') if len(w)>6]
word_pairs = []
while words:
w = words.pop()
word_pairs+= ((w[0], a[0]) for a in words if w[1] == a[1])
max_sq = 0
for w, a in word_pairs:
letter_set = {x:y for y, x in enumerate(set(w))}
for y in itertools.permutations('123456789', len(letter_set)):
if sq(w) and sq(a): max_sq = max(sq(w), sq(a), max_sq)
print "Largest square formed by any member of an anagram pair:", max_sq
Use this link to get the Project Euler 98 Solution Python 2.7 source.The post Project Euler 98 Solution appeared first on Dreamshire.
]]>